Help - Search - Members - Calendar
Full Version: Show Data From Mysql Only If There Is Data In The Table
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Ok very hard to explain :

I have a mysql table with 10 fields, i have a form with 10 fields, i have a display page with 10 fields.
So if i fill the 10 fields in my form, it will send the 10 infos to the database and my display page will show the 10 fields.
Problem is, if i fill only 4 fields in my form it will only send 4 info into the table and my display page will show 10 fields, 4 full 6 empty.

Basically i have a CMS that allows me to upload pics to my site, i can upload 10 pictures so my database has 10 fields, on my display page i have 10 lines repeating

CODE
<img src="pictures/". $row['pic'] .">
<img src="pictures/". $row['pic1'] .">
<img src="pictures/". $row['pic2'] .">
....


Problem is if i only uploaded 4 pics, my display page has the code for 10 pics, but no data can be retrieved for 10 so it is displaying a box with a broken image in it :/, what i want to basically do is only show as many images as i uploaded.

Is there like a code i can put between my IMG tags saying IF data than display the code... if NO data than don't display the IMG code ?

I am trying to find a way to it... but i am failing tongue.gif

I NEED HELP PLEASE !!!
Thanks :'(
Jetteh22
Not sure if this is exactly wat you mean or not but...

CODE
<?php

if($row['pic'] != "") // checking to see if the field is empty
{
echo ?>
<img src="pictures/<?php echo $row['pic'];?>"/>
<?php
}
?>


basically... it only displays that field if $row['XXXXX'] is NOT empty... A little sloppy, but that's how I code :\ It all makes sense to me in the end. Just change the $row['XXXX'] to whatever pic2, pic3, pic4 ect
swordz
QUOTE(Jetteh22 @ Nov 15 2008, 02:02 AM) *
Not sure if this is exactly wat you mean or not but...

CODE
<?php

if($row['pic'] != "") // checking to see if the field is empty
{
echo ?>
<img src="pictures/<?php echo $row['pic'];?>"/>
<?php
}
?>


basically... it only displays that field if $row['XXXXX'] is NOT empty... A little sloppy, but that's how I code :\ It all makes sense to me in the end. Just change the $row['XXXX'] to whatever pic2, pic3, pic4 ect


The if statement could be simply
CODE
if($row['pic'])
or possibly better
CODE
if(isset($row['pic']))
, but Jetteh22's way will work.

swordz
MrTouz
Thanks a lot, that is saving me some crazy time.
noxus
CODE
$result = mysql_query("select image from this_table where image !="" ");

while( list( $image ) = mysql_fetch_row( $result ) ) {
echo "<img src=\"$image\">";
}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.