Where loop with if statement

Using an if statement within a Where loop

Page 1 of 1

4 Replies - 1756 Views - Last Post: 02 May 2008 - 03:03 PM Rate Topic: -----

#1 Chupa85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-September 07

Where loop with if statement

Post icon  Posted 27 April 2008 - 01:16 PM

Here is my situation. I'm trying to populate a list box with values from a table called Sponsors. The fields I would like to display are either Company or First Name and Last Name. If a sponsor does not have a company name then I would like the list box to display the first and last name of the sponsor. If the sponsor has a company name I would like just the company name to be displayed. So far my code will display the list box but there are no values in it. I am sure that I have a database connection and I just checked and there is test data in teh database. Any help would be appreciated.


$sql = mysql_query("SELECT * FROM Sponsors"); 
		
		echo '<form action="AdminEditSponsor.php" method="post" />';
		echo '<select>';
		while($sql = mysql_fetch_array($sql))
		{
		if ($row['Company'] = "") 
			echo "<option value = ".$row['SponsorID'].">".$row['FirstName']." ".$row['LastName']." </option>";
		else
			echo "<option value = ".$row['SponsorID'].">".$row['Company']."</option>";
		}
	   echo '</select><br>';
	   echo '<input type="submit" value="Submit" />';



Is This A Good Question/Topic? 0
  • +

Replies To: Where loop with if statement

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4336
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Where loop with if statement

Posted 27 April 2008 - 01:24 PM

The problem is that you fetch the row into the wrong variable...

// Right here you want to put the value fetched into $row, not $sql
while($sql = mysql_fetch_array($sql))
{
		if ($row['Company'] = "")
			echo "<option value = ".$row['SponsorID'].">".$row['FirstName']." ".$row['LastName']." </option>";
		else
			echo "<option value = ".$row['SponsorID'].">".$row['Company']."</option>";
}



As you can see from my comment, you fetch the data from $sql but you want to put the row into $row which you use in the loop, not $sql.

The rest of the code in that loop appears to be ok, but one bit of advice I would give you is that you should always enclose your value attribute for an option tag in quotes so that it will take the whole value (in case it ever had a space in it).

It won't necessarily cause you problems here because you use apparently an integer value as the ID, but if you ever decide to use a string it would be a wise thing to do to include the quotes.

I recommend using quotes all the time as a good programming practice habit.

Hope everything works out for you now.

"At DIC we be drop down populating code ninjas... that or cause people to drop down as we populate their head with blunt force trauma!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Chupa85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 17-September 07

Re: Where loop with if statement

Posted 27 April 2008 - 01:58 PM

OK so here is the current situation. The options are loading the SponsorID so the list box will pull down. However, neither the Company nor First Name Last Name is displaying in the list box. Here is the code as it stands.

$sql = mysql_query("SELECT * FROM Sponsors"); 
		
		echo '<form action="AdminEditSponsor.php" method="post" />';
		echo '<select>';
		while($row = mysql_fetch_array($sql))
		{
		if ($row['Company'] = "") 
			echo "<option value = ".$row['SponsorID'].">".$row['FirstName']." ".$row['LastName']." </option>";
		else
			echo "<option value = ".$row['SponsorID'].">".$row['Company']."</option>";
		}
	   echo '</select><br>';
	   echo '<input type="submit" value="Submit" />';


Was This Post Helpful? 0
  • +
  • -

#4 carabalb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 28-April 08

Re: Where loop with if statement

Posted 28 April 2008 - 11:43 AM

Well I'm no expert, but I would suggest going through some basic troubleshooting steps, like making sure that the Value names in your database are matching the Value names in the code. Also, a good step would be to comment out the drop list code and simply echo the Name and Company Values that are retrieved from the Database.

This post has been edited by carabalb: 28 April 2008 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4336
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Where loop with if statement

Posted 02 May 2008 - 03:03 PM

Another error...

if ($row['Company'] = "") 



Double equals sign for comparisons, not assignment. You are essentially assigning an empty string to the company each go around. Probably why you don't see it.

:)
Was This Post Helpful? 1

Page 1 of 1