Reputation: 2216
I am using a loop to go through the result of query and display the data in a grid. I only want to display the delete button in certain rows. I can setup if else statements for that.
while($rows = mysql_fetch_array($result)){
//HTML code - I am not using echo here just plain html code written below
deleteButton();//I am calling the deleteButton function here.
//HTML code
}
I have a deleteButton function at the top of the page
function deleteButton()
{
echo "<form name='DeleteInfo' method='POST' action='delete.php'>";
echo "<input type='hidden' name='val1' value=$rows[col1]>";
echo "<input type='hidden' name='val2' value=$rows[col2]>";
echo "<input class='center' type='submit' value='Delete'/>";
echo "</form>";
}
I get an error undefined variable rows for col1 and col2. I am assuming it's a syntax problem.
I have also tried.
echo "<input type='hidden' name='val2' value=$rows['col2']>";
echo "<input type='hidden' name='val2' value='$rows[col2]'>";
This HTML code works
<input type="hidden" name="name1" value="<?php echo $rows['col3']; ?>">
EDIT: Resolved. It was out of scope. I have to use global $rows in the function. Thanks Marc.
Upvotes: 1
Views: 153
Reputation: 4039
As Steven pointed out, you have to pass $rows into the function as well.
Full example:
function deleteButton($row)
{
echo "<form name='DeleteInfo' method='POST' action='delete.php'>";
echo "<input type='hidden' name='val1' value={$rows['col1']}>";
echo "<input type='hidden' name='val2' value={$rows['col2']}>";
echo "<input class='center' type='submit' value='Delete'/>";
echo "</form>";
}
while($rows = mysql_fetch_array($result)){
// ...
deleteButton($rows);
// ...
}
You could wrap your array accessor in braces or use concatenation
echo "<input type='hidden' name='val2' value={$rows['col2']}>";
echo "<input type='hidden' name='val2' value=".$rows['col2'].">";
Upvotes: 1
Reputation: 6052
$rows is not accessable from that function, so what you can do is either pass it as an argument to that function, for make the $rows GLOBAL, by defining it outside any scope and putting the statement
global $rows;
in the functions where you want to access it.
Upvotes: -1
Reputation: 168655
Variables do not stay in scope from one function to the next. If you have a variable that you want to use in a function that you're calling, then you need to pass that variable into the function. (the other option is to make them global, but I'll ignore that, as it's not a good idea to overuse globals)
So where you call the function, you need to call it like this:
deleteButton($rows);
...and then in the function declaration, you need to add it there as well, like this:
function deleteButton($rows)
This is fairly basic stuff as far as PHP goes, or indeed programming in general. It would be good for you to get a handle on this kind of thing, make sure you understand it properly befoe you write too much more code.
Upvotes: 1
Reputation: 503
Your $rows variable doesn't exist in the scope of the deleteButton function. You could amend it like this:
function deleteButton($rows) {
// etc
}
and then call it as:
deleteButton($rows);
Upvotes: 4
Reputation: 3444
echo "<input type='hidden' name='val2' value=".$rows['col2'].">";
Upvotes: 0