Forum Moderators: coopster

Message Too Old, No Replies

IF and ELSE statements for Mysql database

         

slimnutty5000

4:49 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



This goes in part with the other topic i posted, but i think it deserves its own thread.

I am building a product catalog in php, with data input the mysql DB. On my php page I have a table with 4 columns (item#,description,upc,image).
For the "image" column i want a small camera icon there that when clicked will show the large image of that item. I have input the image URLs into a field in the mysql DB named "imageLarge".
What i want is this:
1. Query the database, and if a URL(or any content) is found in the "imageLarge" field, then show my little camera icon.
2. ELSE if no URL or content is found in the "imageLarge" field(empty), then show nothing (blank space).

I hope that makes it super clear. I've been struggling with this part for the last two days searching the web and havent found the right way to do it.
Heres what i am using so far:


function insertIcon(){
$result = mysql_query("SELECT imageLarge FROM my_table") or die(mysql_error());
$image = mysql_fetch_field($result);
if (empty($image))
print "0";
else
print "<img src='http: //www.mywebsite.com/images/pic.gif'>";
}

Then i use this in the content:
<? insertIcon();?>

eelixduppy

7:50 pm on Nov 3, 2006 (gmt 0)



How about something like this:

function insertIcon(){
$result = mysql_query("SELECT imageLarge FROM my_table") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo (!empty($row['imageLarge']))? $row['imageLarge']: "No image";
}
}

I'm thinking though that you want to put some type of restriction on this. Maybe something like this:


function insertIcon($id){
$result = mysql_query("SELECT imageLarge FROM my_table WHERE id = '".$id."'") or die(mysql_error());
echo (!empty($row['imageLarge']))? $row['imageLarge']: "No image";
}

In which case you don't need the while loop, because only one item should be associated with an id number.

Answer your question?

slimnutty5000

8:33 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



I get this error:

Warning: Missing argument 1 for inserticon() in /data/10/1/23/64/.../htdocs/littletree.php on line 8
Unknown column 'id' in 'where clause'

Am i supposed to leave "id" as "id" or change it to something corresponding to my database?

jatar_k

8:44 pm on Nov 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



change it to the corresponding column in your database

eelixduppy

9:21 pm on Nov 3, 2006 (gmt 0)



Don't I feel silly. There's an error in my second example:

function insertIcon($id){
$result = mysql_query("SELECT imageLarge FROM my_table WHERE id = '".$id."'") or die(mysql_error());
[b]$row = mysql_fetch_array($result);[/b]
echo (!empty($row['imageLarge']))? $row['imageLarge']: "No image";
}

oops ;)

slimnutty5000

9:47 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



I still get the same error message:

Warning: Missing argument 1 for insertIcon() in /data/10/1/23/64/..../htdocs/littletree.php on line 8
No image

This error message shows in all the rows.
Something seems to be missing in the "insertIcon" syntax maybe?

eelixduppy

9:51 pm on Nov 3, 2006 (gmt 0)



You are missing the argument. The function is defined as such:

function insertIcon($id){

You have to specify $id if you are going to use this approach. Something along the lines of:


insertIcon(3220);

where the number within the parenthesis is the id number. This only applies if you are using my second solution.

slimnutty5000

10:24 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



ok i just noticed something. I dont see any reference to the icon image i want to be displayed. Where does that come in? for reference, my icon is located in "images/pic.gif". I tried adding this url in the insertIcon() parenthesis, both as url, and as img src, but didnt work.

slimnutty5000

11:18 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



Ok im going to try and explain this one more time to make it more clear:

If the query finds an image url in the imageLarge field (Database),
then show the camera icon(images/pic.gif).
If no image url is found in the imageLarge field, then display "no image"

I dont know how much more simpler i can make it then that.

dreamcatcher

11:28 pm on Nov 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




function insertIcon(){
$result = mysql_query("SELECT imageLarge FROM my_table") or die(mysql_error());

while ($row = mysql_fetch_array($result))
echo $row['imageLarge']? '<img src="http: //www.mywebsite.com/images/pic.gif">' : 'No image';
}

}

insertIcon();

Unless you need to pass a certain row into this function, then revert to eelixduppys posts.

dc

slimnutty5000

11:55 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



ok i think we're getting closer!
IT worked somewhat, but...it put all the outputs in a single cell
instead of each individual cell. I think its becuase of the "while". I already have a "while" going on in my table. (See below)

EX: <td>image,image,image, no image, no image.</td>
What i need is :
<td>image</td>
<td>image</td>
<td>image</td>
<td>no image</td>
etc...
(I really wish i can post a link to show an example of whats going on)

This is the code im using to make my array:


<?php do {?>
<tr bgcolor="#FFFFFF" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="MM_openBrWindow('<? echo $row_littletree['imageLarge'];?>','Image','location=yes,scrollbars=yes,resizable=yes,width=600,height=600')">
<td><?php echo $row_littletree['itemNumber'];?></td>
<td><?php echo $row_littletree['name'];?></td>
<td><?php echo $row_littletree['UPC'];?></td>
<td align="center"><? insertIcon();?></a></td>
</tr>
<?php } while ($row_littletree = mysql_fetch_assoc($littletree));?>

whoisgregg

12:36 am on Nov 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can save yourself all those database queries and get rid of the insertIcon function entirely...

<?php do {?> 
<tr bgcolor="#FFFFFF" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" onclick="MM_openBrWindow('<? echo $row_littletree['imageLarge'];?>','Image','location=yes,scrollbars=yes,resizable=yes,width=600,height=600')">
<td><?php echo $row_littletree['itemNumber'];?></td>
<td><?php echo $row_littletree['name'];?></td>
<td><?php echo $row_littletree['UPC'];?></td>
<td align="center">
<?php echo ( isset($row_littletree['imageLarge']) && !empty($row_littletree['imageLarge']) )? '<a href="'.$row_littletree['imageLarge'].'"><img src="http: //www.mywebsite.com/images/pic.gif"></a>' : 'No image';?>
</td>
</tr>
<?php } while ($row_littletree = mysql_fetch_assoc($littletree));?>

slimnutty5000

8:52 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



Amazing, you are a genius. That's exactly what i was looking for.
Thank you so much for the help.