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

Link to comment
Share on other sites

{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'].

Link to comment
Share on other sites

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.