statement inside array

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
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

statement inside array

Post by sguy »

Code: Select all

while ($number_of_array = mysql_fetch_array($query)) {

	echo "<TD><a href=viewuser.php?name=$number_of_array[nickname] target=_blank>$number_of_array[nickname]</a></TD>\n";	
	echo "<TD>$number_of_array[gender]</TD>\n"; 
	echo "<TD>$age</TD>\n"; 
	echo "<TD>$number_of_array[location]</TD>\n"; 
	echo "<TD>$number_of_array[country]</TD>\n"; 
}
can i put the if else statement or while loop behind:
<a href=viewuser.php?name=$number_of_array[nickname] target=_blank>$number_of_array[nickname]</a>

if the value is equal to 1, then echo a picture

something like the picture attached...

Image

i've tried, but got error...

thanks.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

1 - What is the error message?

2 - What does that if statement you tried look like?

3 - You could try something like this:

Code: Select all

<?php
//...
if (!empty($number_of_array['picture'])) //[php_man]empty[/php_man]()
    echo '<img src="' . $number_of_array['picture'] . '" width="XX" height="XX" />';
//...
?>
4 - Quote the array indices like I did above.

-- Scorphus
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post by sguy »

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in c:\phpweb\members\all.php on line 120

Code: Select all

echo "<TD><a href=viewuser.php?name=$number_of_array[nickname] target=_blank>$number_of_array[nickname]</a>
	mysql_select_db($database, $conn);
		$result=mysql_query("select * from user_detail where nickname = '$nickname'") ;
                $number_of_array = mysql_num_rows($result);
		while ($number_of_array = mysql_fetch_array($result)){
		"$number_of_array[image]"; 
		if ($image == 1) {
		echo"<img src="image/camera.gif">;
		}
		}
	</TD>\n";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

missing a double quote on the first line there..
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

And also in the fourth from down to top. Corrections:

Code: Select all

echo "<TD><a href=viewuser.php?name=$number_of_array[nickname] target=_blank>$number_of_array[nickname]</a>";
	mysql_select_db($database, $conn);
		$result=mysql_query("select * from user_detail where nickname = '$nickname'") ;
                $number_of_array = mysql_num_rows($result);
		while ($number_of_array = mysql_fetch_array($result)){
		//"$number_of_array[image]"; //???
		if ($image == 1) {
		echo"<img src="image/camera.gif">";
		}
		}
	echo "</TD>\n";
- Where does $image (if ($image == 1)) come from?

- What do you mean by the line "$number_of_array[image]";

-- Scorphus
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post by sguy »

in my table has 1 column named "image", default is 0.
when i upload the picture, the 0 will set to 1.
then check the value, if the value is equal to 1, then show the camera.gif
this is what i meaned... :wink:
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Ahn, so I think it should be:

Code: Select all

echo "<TD><a href=viewuser.php?name=$number_of_array[nickname] target=_blank>$number_of_array[nickname]</a>";
	mysql_select_db($database, $conn);
		$result=mysql_query("select * from user_detail where nickname = '$nickname'") ;
                $number_of_array = mysql_num_rows($result);
		while ($number_of_array = mysql_fetch_array($result)){
		if ($number_of_array['image'] == 1) {
		echo"<img src="image/camera.gif">";
		}
		}
	echo "</TD>\n";
-- Scorphus
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post by sguy »

i've tried, it shows the camera.gif in every nickname although the image is equal to 0, then the other value of the column is empty.
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

What is the result of

print_r($number_of_array);

?
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post by sguy »

i'm constantly overwriting the variable names, tried many times...
now works oredi...
thanks u all guys...

Code: Select all

$res = mysql_query("SELECT * FROM user_detail");   
if(mysql_num_rows($res) > 0) {   
   while($row = mysql_fetch_array($res)) {   
echo"<tr>";  
      echo '<td><a href="viewuser.php?name='.$row['nickname'].'" target="_blank">'.$row['nickname'].'</a>';   
      if($row['image'] == 1) {    
         echo '<img src="image/camera.gif" alt="camera" />';   
      }   
      echo '</td>'."\n";
Post Reply