Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts

    PHP Random Image Query

    Hi Guys,

    I need to create a random image to be generated on a home page I am doing.

    Basically I need to put three random images in three seperate boxes on the homepage.

    These images need to be based on a category id.

    1. Bi-Folding doors
    2. Windows and Doors
    3. Conservatories.

    The image (image1) is in a table called portfolio, the cat_id is in the portfolio table and linked with portfolio_cat table.

    I need the code for the following

    get image1 from portfolio table where its cat_id is = 1,2 or 3 and limit that to just one image to view.

    I have this piece of code which generates random images I just don't know how to change it to do what I need.

    Here is the code

    PHP Code:
    <?PHP
                mysql_select_db
    ("$database"$dbh);
                
    $sql "SELECT * FROM portfolio ORDER BY ????????? RAND() LIMIT 1";
                
    $query mysql_query($sql$dbh) or die ("Error in query: $sql" mysql_error());
            
          
          
    //Now we loop through each record returned.
          
    while ($row mysql_fetch_array($query))
                {
            
            
    //get values from database
            
    $id $row['??????']; //usually database record id 

            //now generate using above values - note html <option>
            
    echo $row['??????'];
          
          }
    //end of loop 



    ?>
    Can anybody help with this?

    I have a feeling the echo statement might need to change also so if anybody could do the code for that then that would be great.


    Cheers

    Dan
    Last edited by winnard2008; 08-14-2008 at 10:31 AM.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    PHP Code:
    $sql "select * from portfolio where cat_id = 1 order by RAND() limit 0,1"

  • #3
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for your reply.


    But that query does not include where i am pulling the image from


    would it not be

    PHP Code:

    $sql 
    "SELECT image1 FROM portfolio WHERE cat_id=1 ORDER BY RAND() limit 1"
    So if the above query is correct...how would I echo it into my html???

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm assuming your holding the IMAGE PATH, not the actual image in the database.


    PHP Code:
    $sql "SELECT image1 FROM portfolio WHERE cat_id=1 ORDER BY RAND() limit 1"
    $result mysql_query($sql) or die('Could not look up user data; '.mysql_error());
    $db_info mysql_fetch_assoc($result);
    echo 
    "<img src=\"".$db_info['image1']."\">"

  • #5
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yeah

    There is an image folder on the server.


    The database just links the path.

    Thanks for the help.

  • #6
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi guys


    thanks for all your help.

    Here is the final code that works great.

    PHP Code:

    <?php

    mysql_select_db
    ("$database"$dbh);

    $sql "SELECT image1 FROM portfolio WHERE cat_id=1 ORDER BY RAND() Limit 1";

    $query mysql_query($sql$dbh) or die ("Error in query: $sql" mysql_error());

    while(
    $row mysql_fetch_array($query)) 



        echo 
    "<img src=\"images/".$row['image1']."\">";

    // End of while loop
    ?>


  •  

    Posting Permissions

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