Jump to content

using IF statements in mysql_fetch_array


zipperhead

Recommended Posts

Hello,

 

I'm hoping someone can help me with this.

 

I have a query that outputs an array and would like to display the results differently depending on what they are.

The query also uses a left join so some of the results will be empty, or null I suppose.

 

The results of the query look something like this...

 

$title_result['title']    $title_result['city']    $title_result['value']

                  title1                      city1                      8

                  title2                      city2                      3

                  title3                      city3                      8

                  title4                      city4                      no value (null?)

 

So the logic of what I'm trying to do is display a different echo statement depending on the results of the ['value'] in the array.

 

Something like this: (I know the code is incorrect)

while($title_result = mysql_fetch_array($result)){ 

if ($title_result['value']="8"){echo '.$title_result['title'].' and '.$title_result['city'].' have a high value of '.$title_result['value'].';}
if ($title_result['value']="3"){echo '.$title_result['title'].' and '.$title_result['city'].' have a low value of '.$title_result['value'].';}
if ($title_result['value']="null"){echo '.$title_result['title'].' and '.$title_result['city'].' have no value;}
}

 

I've been trying and just can't seem to get the proper syntax for the IF statements to work, get confused with all the ";}'. Perhaps someone can enlighten me on the proper code for this?  Or is there a better way to do it?

 

thanks,

 

ZH

{echo '.$title_result['title'].' and '.$title_result['city'].' have a high value of '.$title_result['value'].';}

 

Look at the echo statment. The first ' starts it and then you have a . which should be after ending it. You then echo the $title_result['title'] inside the single quotes and then have another dot(.). Your and is therefore out of single quotes and your variables in. You need to reverse this which can simply be done by removing the first and last dot and single quote. Try this:

 

{echo $title_result['title'].' and '.$title_result['city'].' have a high value of '.$title_result['value'];}

 

Just change the words to make the other rows as well. Notice now that the single quotes enclose the and and not the $title_result['title'].

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.