Log in

View Full Version : displaying images dynamically



kikilahooch
09-04-2006, 11:48 AM
Hi, I'm new to this site and php and I am trying to display images on my site dynamically. I want images on my site to change everytime the user refreshes the page. My site is a clothes site which has loads of different images stored in its database in a table called products. The products table stores all information stored on items of clothing, including the name of the image of the item, eg fen_skirt.jpg. I do not actually store the image in the database, just the name of it. The images are stored on my server in a folder called 'images'. To display out the images I append the image name to the path of my images folder. Below is an example of how I am displaying out my images:



<html><body>
<?php
include "db.php";


$sql ="select image from product where shopName = 'pure'";


$query = "SELECT * FROM product where shopName = 'pure' ";



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



if($result){
echo'
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
<tr>





</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'] . '">


</tr>';
}
echo'</table>';
}

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




</html></body>

this code is displaying out all of the images, whereas I just want it to display out one image at a time, but to have it constantly changing. How can I do this??

thetestingsite
09-04-2006, 02:00 PM
One thing that you could try is using the mtrand function, but the thing about that is the ids of each row in the db would have to be in order (1,2,3,...). The way that you could do the mtrand is


<?php
include "db.php";

$query = "SELECT * FROM product where shopName = 'pure' ";

$info = mysql_query($query, $conn) or die(mysql_error());

$num = mysql_num_rows($info);

$image = mtrand(1,$num);


$sql ="select image from product where shopName = 'pure' and id='$image'";

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

if($result){
echo'
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
<tr>

</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'] . '">


</tr>';
}
echo'</table>';
}

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




</html></body>

There may be other more complicated ways, but this is the way that I find the easiest.

kikilahooch
09-04-2006, 02:13 PM
Thank you! When I tried that code it said

Fatal error: Call to undefined function: mtrand() in /home/c/ciaracousins/public_html/dynamic.php on line 11

Do I need to define mtrand first or something?? I'm not familiar with it and know very little about php!

thetestingsite
09-04-2006, 02:17 PM
sorry...it was a typo. i meant to put mt_rand(). I'm currently typing in the dark and its fairly early in the morning so sorry about that. so just put the underscore ( _ ) between "mt" and "rand" and it should fix that.

kikilahooch
09-04-2006, 02:30 PM
lol, thanks, now its saying

Unknown column 'id' in 'where clause'

thetestingsite
09-04-2006, 02:35 PM
then it appears that you dont have a field in the database table called 'id', or at least not named 'id'. If you have access to the database, look at all the fields to see if maybe theres one that identifies the row id of the item. Other than that, you may have to add the id to each row, or export the table, drop it, create again, add the field id and set the key to primary, then make it auto_increment. Other than that, mt_rand may not work for you.

kikilahooch
09-04-2006, 02:49 PM
I do have a prodId but when I change it to that it just brings up a blank page. Any other suggestions??

kikilahooch
09-04-2006, 02:53 PM
Ah its working actually, I just needed to press the refresh button a few times. Thanks very much! :)

thetestingsite
09-04-2006, 02:56 PM
being that it is a "store" site, the prodId is probably the stock number or something to that effect, in order for the mt_rand() function to work properly the id must start at 1, and end at the total number of rows in the db. So, the only other thing I could say is try redoing the db with the added field that auto increments that way it will work flawlessly. Or, thinking about this a little bit harder, check the prodID numbers and see if they show some sort of a pattern. If they do, try changing the start and end number of mt_rand() and see if that does anything.

kikilahooch
09-04-2006, 03:23 PM
It was skipping because I was pulling images only from 1 of the shops and there is 4 shops in total, so there were gaps when it came to products from the other shops.

Is there any way that I can have more than one image showing, and all of them showing different images??

thetestingsite
09-04-2006, 04:46 PM
you could repeat the mt_rand for as many times as you would like, just make sure that each one has a different variable. For example


<?
$image1 = mt_rand(1,7);
$image2 = mt_rand(1,7);

$sql1 = "SELECT * FROM `images` WHERE `store`='store1' AND `id`='$image1'";

$sql2 = "SELECT * FROM `images` WHERE `store`='store2' AND `id`='$image2'";

//Then simply execute the sql queries and change to appropriate numbers.
?>


Enjoy!