Jump to content

Displaying images dynamically from a DB


kikilahooch

Recommended Posts

I'm trying to display images from a database that are the result of a search. I am able to get an image to display but the problem is it keeps on showing the same image everytime. Here is my code:

[code]
$sql="select * from product";
$sql ="select image, prodName ,price  from product where dept = 'ladies'";

//db
$result = mysql_query($sql,$conn) or die(mysql_error());

//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) >= 1) {
    //if authorized, get the values of name, surname
    $image = mysql_result($result, 0, 'image');
    $prodName = mysql_result($result, 0, 'prodName');
    $price = mysql_result($result, 0, 'price');

}

$result = @mysql_query($query);
    if($result){
        echo'

            <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1>


            <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
                <tr>


                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td>
                
                
                </tr>';
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    echo'<tr>
                    

<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td>
<td align="center"><b>'.$row['prodName'].'</td></b>
<td align="center"><b>€'.$row['price'].'</td></b>
                


                    </tr>';
                        }
        echo'</table>';
    }
    
    else{
        echo'<h1> System Error </h1> table ';
        exit();
    }
    mysql_close();

?>
[/code]

Any idea why its displaying the same image everytime??
Link to comment
Share on other sites

I think you'll need a while loop in there

[code]

$result = mysql_query($sql, $conn) or die(mysql_error());

//Loop through the results
while ($items = mysql_fetch_array($result, MYSQL_ASSOC)) {

  // Display stuff
  echo "{$items['image']}<br />";

}
[/code]

With just using the $result = mysql_query()
Link to comment
Share on other sites

I just copied and pasted that code in like this:

[code]
$result = mysql_query($sql, $conn) or die(mysql_error());

//Loop through the results
while ($items = mysql_fetch_array($result, MYSQL_ASSOC)) {

  // Display stuff
  echo "{$items['image']}<br />";

}
    if($result){
        echo'
                
            <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1>

                        <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
                <tr>

                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td>
                
                

                
                </tr>';
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    echo'<tr>
                    
<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td>
<td align="center"><b>'.$row['prodName'].'</td></b>
<td align="center"><b>€'.$row['price'].'</td></b>
[/code]

But what that did was just th displayed name of the image like i had stored in my db. And in my table nothing showed up anymore. The field that is in my db only stores the name of the image, not the actual image itself. Any other suggestions or did i put the code in wrong??
Link to comment
Share on other sites

its not actually giving me back any errors. When i was using my own code it was displaying the product name and price of each product returned from the search which I wanted it to but it kept on showing me the same image in every row instead of different images. When I used sharkbaits for loop it just returned all of the image names, not displaying the actual image in the table which i want

using this code it will display the image, but keeps on showing the same one. So i think i need to use a loop here somewhere which is what sharkbait was saying but I'm not the best at this so I'm not able to do it.

[code]//<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td>
<td align="center"><b>'.$row['prodName'].'</td></b>
<td align="center"><b>€'.$row['price'].'</td></b>[/code]

Link to comment
Share on other sites

1. on your first post, there is something wrong with the line [code]$result = @mysql_query($query);[/code] can't find value for your $query variable

2. check for authorization first before performing anymore actions to avoid wasting resources

3. try this one out:
[code]
//check for authorization first
// if not authorized, redirect or display a message or something

$sql ="select image, prodName ,price  from product where dept = 'ladies'";
$result = mysql_query($sql,$conn) or die(mysql_error());
if (mysql_num_rows($result)>1)
{

echo'
            <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1>
            <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
                <tr>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td>
                <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td>                              
                </tr>';
     while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  echo'<tr>                
<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$row['image'].'" ></td><td align="center"><b>'.$row['prodName'].'</td></b>
<td align="center"><b>€'.$row['price'].'</td></b>
</tr>';
}
echo'</table>';
}

[/code]

didn't check for typos, you might want to double check that.

Hope that helps.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.