Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: displaying images dynamically

  1. #1
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default displaying images dynamically

    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:

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

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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 Code:
    <?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($resultMYSQL_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.

  3. #3
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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.

  5. #5
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    lol, thanks, now its saying

    Unknown column 'id' in 'where clause'

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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.

  7. #7
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I do have a prodId but when I change it to that it just brings up a blank page. Any other suggestions??

  8. #8
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah its working actually, I just needed to press the refresh button a few times. Thanks very much!

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •