1

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.

2
  • If anyone is wondering deleteButton call is within PHP Tags. Commented Jun 30, 2011 at 16:11
  • 2
    In other words, pass $rows as a parameter to deleteButton. Commented Jun 30, 2011 at 16:13

5 Answers 5

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This works too. I forgot the pass the parameter. Setting up $rows as global will work but I think this is a better approach.
1

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.

Comments

1

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'].">";

2 Comments

Actually the manual page you linked states that the way he used it is correct.
@bazmegakapa: Too true, I jumped the gun a bit. I've corrected my answer.
0
echo "<input type='hidden' name='val2' value=".$rows['col2'].">";

Comments

-1

$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.

2 Comments

@Brian: I was just showing two(passing arguments, Global variables) of the various ways he could use to solve his problem.
I guess then you know that globals can become a very bad habit, hence the down-vote for teaching bad habits - if you had mentioned to use as a 'last resort' - would have been OK :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.