PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
$i++;
}
}
^
That code is being included.php, however it will not work. If I cut the code out of the function and just paste it into the page between <?php ?> it works fine.
Is this the wrong way about going about it?
The actually page being displayed has this in it::
The scope of the variable $user in your function is limited to that function. You might want to read up on variable scoping in PHP. A possibility is returning $user from the function and using this return value.
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
$i++;
}
echo "
<div id=\"selAccountName\">
<select name=\"accountSelection\">
<option value=$user[0]>$user[0]</option>
<option value=$user[1]>$user[1]</option>
<option value=$user[2]>$user[2]</option>
<option value=$user[3]>$user[3]</option>
</select>
</div>
";
}
That's a possibility too. The reason we talked about returning the result from the function is that generally you want to try to avoid mixing PHP with HTML as much as possible. Mixing the two is what is called spaghetti code and leads to your code and layout much more difficult to change afterwards. Also, suppose you would want to reuse your function but with a slightly different HTML output. You would have to write the same function again with the different HTML inside, meaning you have the same PHP code twice.