Jump to content

Archived

This topic is now archived and is closed to further replies.

kikilahooch

Displaying images dynamically from a DB

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??

Share this post


Link to post
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()

Share this post


Link to post
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??

Share this post


Link to post
Share on other sites
What's this for:
[code]$sql="select * from product";
$sql ="select image, prodName ,price  from product where dept = 'ladies'";[/code]
I mean, you set $sql as something and just right away as something else?

Orio.

Share this post


Link to post
Share on other sites
Thats just from me trying out different stuff and not tidying up the code yet. I dont think that affects what my problem though does it??

Share this post


Link to post
Share on other sites
No it doesnt affect the code.
What error do you get?

Orio.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.