careym1989 Posted September 12, 2008 Share Posted September 12, 2008 I am creating code for a PHP page to display Candidates if they have been approved. Currently, I'm using if () statements. There's a field in the table entitled "approved" that is automatically submitted as 0 on the form. I'm creating a vetting process page to change it to 1 to display after I've confirmed with the candidate's campaign. This code is displaying the else statement only after I upload it. I don't see any errors with the code and the page isn't giving me parse errors. Help would be MUCH appreciated. Thanks a lot, fellow coders! <?php // BEGIN RECALL FROM DATABASE -- CANDIDATES mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()); mysql_select_db("XXX") or die(mysql_error()); $data = mysql_query("SELECT * FROM XXX ORDER BY id DESC") or die(mysql_error()); if ($approved == '1') { echo "<tr>"; echo "<td><font color=#000000 face=Arial size=2>$info[n]</font></td>"; echo "<td width=184><font color=#000000 face=Arial size=2>$info[cc]</font></td>"; echo "<td width=68><font color=#000000 face=Arial size=2>$info[st]</font></td>"; echo "<td><font color=#000000 face=Arial size=2>$info[ea]</font></td>"; echo "<td><font color=#000000 face=Arial size=2>$info[cd]</font></td>"; echo "<td><a href=$info[url]><font color=#000000 face=Arial size=2>$info[url]</font></a></td>"; echo "</tr>"; } else { echo "No candidates have taken the pledge."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123953-solved-using-if-statements-to-display-if-field-1-please-help/ Share on other sites More sharing options...
KevinM1 Posted September 12, 2008 Share Posted September 12, 2008 When you run a query, you must fetch the results. Right now, the value of your $approved variable is undefined because you didn't bind the results of your query to it. In other words: $data = mysql_query("SELECT * FROM XXX ORDER BY id DESC"); while($row = mysql_fetch_assoc($data)) { if($row['approved'] == 1) //more on this line below { //display candidate data } } Keep in mind that when using array notation (i.e. $row['approved']), the name within the quotes is supposed to be the column name in the DB you want to access. Quote Link to comment https://forums.phpfreaks.com/topic/123953-solved-using-if-statements-to-display-if-field-1-please-help/#findComment-639856 Share on other sites More sharing options...
careym1989 Posted September 12, 2008 Author Share Posted September 12, 2008 It doesn't display any records. I know there are several entries with $row['approved'] == 1. Thanks a lot for your help though, I really appreciate it. -Carey Quote Link to comment https://forums.phpfreaks.com/topic/123953-solved-using-if-statements-to-display-if-field-1-please-help/#findComment-639871 Share on other sites More sharing options...
KevinM1 Posted September 12, 2008 Share Posted September 12, 2008 It doesn't display any records. I know there are several entries with $row['approved'] == 1. Thanks a lot for your help though, I really appreciate it. -Carey Did you fix the other variables too? As in: echo "<tr>"; echo "<td><font color='#000000' face='Arial' size='2'>{$row['n']}</font></td>"; echo "<td width='184'><font color='#000000' face='Arial' size='2'>{$row['cc']}</font></td>"; echo "<td width='68'><font color='#000000' face='Arial' size='2'>{$row['st']}</font></td>"; echo "<td><font color='#000000' face='Arial' size='2'>{$row['ea']}</font></td>"; echo "<td><font color='#000000' face='Arial' size='2'>{$row['cd']}</font></td>"; echo "<td><a href='{$row['url']}'><font color='#000000' face='Arial' size='2'>{$row['url']}</font></a></td>"; echo "</tr>"; ?? I ask because your orginal code snippet is filled with variables that seemingly come out of nowhere and aren't related to the query results at all. Remember: it's your job to bind query results to a variable. That's what the following is for: $row = mysql_fetch_assoc($data) Quote Link to comment https://forums.phpfreaks.com/topic/123953-solved-using-if-statements-to-display-if-field-1-please-help/#findComment-639886 Share on other sites More sharing options...
careym1989 Posted September 12, 2008 Author Share Posted September 12, 2008 Thanks a lot -- it worked. I really appreciate it, Nightslyr. Regards, Carey Quote Link to comment https://forums.phpfreaks.com/topic/123953-solved-using-if-statements-to-display-if-field-1-please-help/#findComment-639893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.