Jump to content

[SOLVED] Using if () statements to display if field = 1 --- Please Help


Recommended Posts

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

}

?>

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.

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.