Forum Moderators: coopster
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'>";
}
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";
}
Answer your question?
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 ;)
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.
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.
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
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));?>
<?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));?>