Jump to content


Photo

Displaying images dynamically from a DB


  • Please log in to reply
7 replies to this topic

#1 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 21 April 2006 - 04:06 PM

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:

$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();

?>

Any idea why its displaying the same image everytime??


#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 21 April 2006 - 04:14 PM

I think you'll need a while loop in there


$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 />";

}

With just using the $result = mysql_query()

#3 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 21 April 2006 - 04:30 PM

I just copied and pasted that code in like this:

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

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

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 21 April 2006 - 04:33 PM

What's this for:
$sql="select * from product";
$sql ="select image, prodName ,price  from product where dept = 'ladies'";
I mean, you set $sql as something and just right away as something else?

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 21 April 2006 - 04:40 PM

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

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 21 April 2006 - 05:45 PM

No it doesnt affect the code.
What error do you get?

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 21 April 2006 - 08:41 PM

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.

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



#8 eves

eves
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 22 April 2006 - 03:37 PM

1. on your first post, there is something wrong with the line
$result = @mysql_query($query);
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:
//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>';
}


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

Hope that helps.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users