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

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!

Moderator: General Moderators

Post Reply
besbajah
Forum Newbie
Posts: 23
Joined: Fri May 20, 2005 1:00 pm
Location: Brazil

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

Post 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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Please notice that "have" is not a variable. You probably want:

Code: Select all

if ($row['have'] == 'yes')
{
  //
}
besbajah
Forum Newbie
Posts: 23
Joined: Fri May 20, 2005 1:00 pm
Location: Brazil

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 
  }
}
Post Reply