Forum Moderators: coopster
I have a small issue I can't seem to find on my own, probably because I am such a noob.
I have created a class to populate a pull down list from data retrieved from MySQL. There is a function in this calss that simply echo "SELECTED" if it finds that the form has submitted a selection already. The function seems to work correctly, except that the text is echoed before I wanted it to be.
Function snippet:
function fnSelected( $check, $match )
{
if( $check == $match )
echo "SELECTED";
return;
}
the calling code, also inside the same class:
while( $row = mysql_fetch_array( $query ))
{
echo "<option " . $this->fnSelected( $row[ $sqlRow ], $this->formCheckStr ) . ">" . $row[ $sqlRow] . "</option>";
}
The generated HTML should look sonething like this:
<option>notSelectedOption</option><option SELECTED>selectedOption</option>
But what I actually get returned is this:
<option >notSelectedOption</option>SELECTED<option >selectedOption</option>
It looks as though the function is called before it is actually called...very strange.
If anyone has any ideas on why this is happening or can point me in the right direction to find an answer, I would greatly appreciate it.
emeyer
Welcome to WebmasterWorld!
One way to think of it, that might help you understand what it's doing, is to think of the semi-colon as the "DO IT" command. So when you call the fnSelected function, if performs the echo "selected" command before the previous echo command is finished. There's actually a very easy way to solve your problem, and that's to have the fnSelected function return a string, rather than echo it. Like this:
function fnSelected( $check, $match )
{
if( $check == $match )
return "SELECTED";
}
If you call fnSelected anywhere else, your code might need to be changed, but this modification should work for what you have now.
Chad