1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP/MYSQL: How to create image galleries that rotate/change during each page refresh/load

Discussion in 'Databases' started by Az Tek, Oct 23, 2013.

  1. #1
    I am having a bit of trouble finding out how to do this so any help would be GREATLY appreciated, thanks in advance! (I have tried to make this as brief as question as possible.)

    I am building a website that will consist of only images on a single page. There are 300 Images on this page. The home page. Each one of these images is a link and can be clicked. I have a database and this database contains a table where the image URL and the link/target URL exists for each image. There are 2,000 image in the database and only 300 images positions on the page.

    Using PHP/MySQL how can I make it so that every time the page is loaded/refreshed new images are shown. So that it randomly grabs 300 images out of the database to be displayed on the page. Kinda like a rotating advert would work. Each time the page is loaded a new advertisement is shown.

    It doesn't have to show 300 different/unique images each time the page is refreshed. It just can't show any duplicate images at any one time on the page.

    Looking for a solutions, ideas, or links to tutorials that I can follow. Thanks!

    EDIT:

    Here is the code I currently have so far..

    <?php
    // Connects to the database.
    $connection = mysql_connect("localhost", "root", "");
    if (!$connection) {
    die("Connection Failed: " . mysql_error());
    }

    // Selects a database from the previously opened connection.
    $select = mysql_select_db("affiliate_links", $connection);
    if (!$select) {
    die("Connection Failed: " . mysql_error());
    }

    // Performs query to table 'affiliates' in previously opened database.
    $result = mysql_query("SELECT * FROM affiliates ORDER BY RAND() limit 1", $connection);
    if (!$result) {
    die("MySQL Query/Connection Failed: " . mysql_error());
    }

    while ($row = mysql_fetch_array($result)) {
    }
    ?>

    ** The following code is where I want each new image to appear on the page.

    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
    <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>

    As is, the code above(the echo $row statements) does not work. The only way I have been able to pull the data from the data base is by putting the echo statements right under the 'while loop' like this.

    while ($row = mysql_fetch_array($result)) {
    echo $row['target_url']." ".$row['thumb_url']."<br />";

    If I keep ORDER BY RAND() limit 1 in the code it will only show me 1 iteration. 1 row of the table. If I remove it I see all the links/image links where ever I echo it out right under the while loop.

    So basically what I need now: Be able to echo out the $row OUTSIDE of the while loop and it be displayed inside the html to complete the links. And I need it to beable to display a new image during each iteration of the loop. OR if there is a better way to do it than a while loop. Thanks!
     
    Solved! View solution.
    Last edited: Oct 23, 2013
    Az Tek, Oct 23, 2013 IP
  2. #2
    Set limit 300 if you want 300 images on your page:

    $result = mysql_query("SELECT * FROM affiliates ORDER BY RAND() limit 300", $connection);
    PHP:
    Try describe better what do you want and paste your script in code.
     
    jscg, Oct 24, 2013 IP
    sarahk likes this.
  3. Az Tek

    Az Tek Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    Thanks for the answer and sorry for my late reply. I used exactly what you have listed above. And will do for future posts, thanks. =)

    EDIT: I meant to edit not bump. Will make sure the format of the question is code in the future.
     
    Az Tek, Oct 26, 2013 IP