SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation ORDER BY rand() using a for or while loop

    I am trying to randomly select two images from my database. I am trying to grab the post variables from them so I can update the database after the form is submitted. I want to name the images: image_1 and image_2...so I can reference them in the post. Oh, and I don't want to use the id's from the database to reference the images.

    If I use a while loop, it works fine (I get the random images), but my images end up being named the same thing (name="image_1"), so I can't use grab the Post data for each one.

    If I use a for loop, I don't get random images, but I can use $i so that each image has a unique name.

    Here is some code:
    Code:
    <?php
    // Query that database
    $query = "SELECT * ";
    $query .= "FROM images ";
    $query .= "ORDER BY RAND() ";
    $query .= "LIMIT 2";
    $random = mysql_query($query, $connection);
    confirm_query($random);
    $random_image = mysql_fetch_array($random);
    
    //While Loop
    while($random_image = mysql_fetch_array($random)) {
    	echo "<input type=\"image\" name=\"image_1\" src=\"{$random_image['full_path']} \" /> ";
    }
    
    
    // or using the For Loop
    for($i=1; $i<3; $i++) {
    	echo "<input type=\"image\" name=\"image_{$i}\" src=\"{$random_image['full_path']} \" /> ";
    }
    
    ?>
    I am fairly new to PHP, any ideas on how I can make this work?

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $i 1;
    while(
    $random_image mysql_fetch_array($random)) {
        echo 
    "<input type=\"image\" name=\"image_1\" src=\"{$random_image['full_path']} \" /> ";
        
    $i++;
    }


    // or using the For Loop
    for($i=1$random_image mysql_fetch_array($random); $i++) {
        echo 
    "<input type=\"image\" name=\"image_{$i}\" src=\"{$random_image['full_path']} \" /> ";

    The for loop is kinda perfect for this case.
    http://www.php.net/for

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you dont really need to initialize $i you can jsut put $i++ where you want the number. it will just start at 0.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by galen View Post
    you dont really need to initialize $i you can jsut put $i++ where you want the number. it will just start at 0.

    except if it is used anywhere else in your PHP in which case not resetting it to zero will mean that it will start from whatever value it had the last time it was referenced. Safer to set its initial value and that way you are sure.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, but it's not good practice.

    For starters, you're more likely to run into bugs. For example, you used $i in another part of your code within the same scope, and $i had the value of 37. Now your gonna start incrementing at 37, which probably isn't what was desired.

    Also, php will emit an undefined variable notice if E_ALL error reporting is being used. It's very helpful to use E_ALL error reporting during development, and even in production mode if the code is written tightly.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips, I used the while loop code and it worked perfectly:

    Code:
    $i = 1;
    
    while($random_image = mysql_fetch_array($random)) {
    
        echo "<input type=\"image\" name=\"image_{$i}\" src=\"{$random_image['full_path']} \" /> ";
    
        $i++;
    
    }


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
  •