If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > if else in an array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-29-10, 00:25
facarroll facarroll is offline
Registered User
 
Join Date: Oct 2010
Location: Jindabyne, Australia
Posts: 5
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 " ";} 
	}
?>
Reply With Quote
  #2 (permalink)  
Old 10-29-10, 03:14
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 10-29-10, 03:16
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
moved to the PHP forum, as its a PHP question rather than a MySQL problem
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 10-29-10, 06:44
facarroll facarroll is offline
Registered User
 
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.
Reply With Quote
  #5 (permalink)  
Old 10-29-10, 08:47
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 11-12-10, 13:13
koula koula is offline
Registered User
 
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).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On