Page 1 of 1

I don't know the right way to use IF THEN ELSE

Posted: Sun Jun 26, 2005 4:27 pm
by besbajah
Hi,
Can anyone help me with my query result output?

Firstly, here is a snippet from the query, and I have no problems: -

Code: Select all

$result = mysql_db_query("select Artist, ArtistImages as Have")
Secondly, here is the display output: -

Code: Select all

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
IF ( Have == 'yes' )
{
print("<a href='../Track Notes/{$row['HL_Artist']}'>{$row['Artist']}</a>");
}
ELSE
{
print("{$row['Artist']}"); 
echo "<br>";
}
}
?>
Now, the result works fine, but the IF statement doesn't do anything. It simply jumps to the Else statement and prints out the list of Artists. The problem is matching up the contents of the IF statement with something similar in the Query string itself.

I would like to know how to match up the
("select Artistimages as Have")
statement from the query with the
IF ( Have == 'yes' )
statement within the While loop.

Can anyone help me with that?

d11wtq | Please can you read the sticky about posting code in the forums? We have

Code: Select all

tags, and no such [mysql] tags [/color]

Posted: Sun Jun 26, 2005 5:14 pm
by timvw
Please notice that "have" is not a variable. You probably want:

Code: Select all

if ($row['have'] == 'yes')
{
  //
}

Posted: Mon Jun 27, 2005 7:17 am
by besbajah
Hi there,

Yes, your code is just the trick, and I understand the 'weakness' in my original statement.

Let me iterate the concept of the code.

First of all we run a query using the AS statement to put the results into variables (where necessary).

Secondly we run a WHILE loop putting the SQL variables into an Array which is assigned to a new PHP variable.

Thirdly we call the PHP variable as specific values using an IF ELSE statement so that we can print them out until the conditions of the While loop are met.

I forgot to call the variable and I just put HAVE with no assignment to anything. Looks a bit weird when you look at it now?

It's a learning curve, and thanks to your help I'm getting there, making the Internet a better, more dynamic place to use.

Cheers, until next time.
Bes.

Posted: Mon Jun 27, 2005 9:24 am
by timvw
I'm not absolutely sure i understand what you are trying to do...

Code: Select all

$whatever = false;
while (!$whatever && ($row = mysql_fetch_assoc($rs)) != null))
{
  // do stuff

  if (...) 
  {
    $whatever = true; // jump out the loop
    // could use break but i've read to many cautionary tales from Jackson 
  }
}