Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Location
    Jindabyne, Australia
    Posts
    5

    Unanswered: if else in an array

    I am new to php mysql so have had difficulty putting this question together. I have a JOIN query which works OK, and everything is fine except that the last echo does not work correctly.
    The database value of passState can be TRUE/FALSE so its value is always 1 or 0. The page is supposed to display the image filename2.png if the value is 1, or the string 'xxx' if its value is 0.
    What actually happens is either every row displays the image if ("{$row1['passState']}" == 1)and 'xxx' if set to 0.
    There are various TRUE/FALSE values in the database, so I think the syntax of the last line is wrong, but I just can't find it.
    Any suggestions?

    Code:
    <?php
    $query1 = mysql_query("SELECT DISTINCT comment, url_small, title, pdf, passState
    					  FROM topics
    					  INNER JOIN quiz
    					  ON topics.managerId = quiz.managerId 
    					  WHERE ( topics.$egroup = 1
    					  AND quiz.userId = '$userId' )
    					  ORDER BY topics.title
    					  ");
    
    	while ($row1 = mysql_fetch_array($query1))
    	{
    echo "<img src = '../{$row1['url_small']}' /><br />\n"; 
    echo "{$row1['title']} <br />\n"; 
    echo "<a href='../{$row1['title']} .php'>blah, blah</a><br />\n"; 
    echo "<a href='../{$row1['pdf']}' ><img src='filename1.jpg' /><br />\n"; 
    echo "{$row1['comment']} <br />\n";
    	if ("{$row1['passState']}" == 1) {echo "<img src='filename2.png' /><br />\n";} else {echo " ";} 
    	}
    ?>

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    test for true / false rather than the numeric value
    im also suspicious of the quote marks in the logical test

    eg
    PHP Code:
    if ({$row1['passState']} == true)
    {echo 
    "<img src='filename2.png' /><br />\n";
    } else
    {echo 
    " ";

    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    moved to the PHP forum, as its a PHP question rather than a MySQL problem
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Oct 2010
    Location
    Jindabyne, Australia
    Posts
    5
    Thanks for the help. I tried lots of combinations of true/false 1/2 after the == but I get either the image or the 'xxx' (not a mix of them) in all of the rows. The database column type for passState is tinyint. Could this be a problem?
    I do appreciate your help. Thanks.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    OK is passState a column in the db...... PHP does care about capitalisation
    have you removed the " " around the if statement

    PHP Code:
    echo "passState is:".$row1['passState']."\n";
    if (
    $row1['passState'] == 1)
    { echo 
    "<img src='filename2.png' /><br />\n";
    } else
    { echo 
    " ";

    btw you are better off definign the appropriate datatype in MySL
    if its boolean then select the BOOL or Boolean datatype,

    if its numeric use an appropriate numeric datattype (either an integer or 'real ' format)
    if its a date or time then use a date tatatype

    http://dev.mysql.com/doc/refman/5.0/...-overview.html
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Nov 2010
    Posts
    2
    You start the line like this

    Code:
    if ("{$row1['passState']}" == 1)
    take off the double quotes " " from if statment and try to write this piece like this:

    Code:
    if ($row1['passState'])
    Is not necessary to use the "==" operation if the variable is a boolean value (true or false).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •