Professional Web Applications Themes

newbie question with php and mysql - Macromedia Dynamic HTML

Hi Everyone. Can someone suggest an approach for me please. I have a dynamic site where i want to display 3 random 'featured' records at different positions. I cant just use rand on each of them , as there is a chance they will double up. I cant retrieve all 3 randomly, then display because they apper in different parts of the page. Thanks for any suggestions...

  1. #1

    Default newbie question with php and mysql

    Hi Everyone. Can someone suggest an approach for me please.
    I have a dynamic site where i want to display 3 random 'featured' records at
    different positions.

    I cant just use rand on each of them , as there is a chance they will double
    up.
    I cant retrieve all 3 randomly, then display because they apper in different
    parts of the page.

    Thanks for any suggestions


    levels Guest

  2. #2

    Default Re: newbie question with php and mysql

    You could query the database with a regular SELECT query. Then, get the
    number of rows returned using the mysql_num_rows() function. Next, iterate
    trough the rows advancing a random number of rows.

    Check this out...




    <?php

    $link = mysql_connect($host, $user, $pass);

    if ($link) {
    // using example fields
    $selQry = 'SELECT name, address, age FROM table_wheredatais WHERE 1 = 1';
    $results = mysql_query($selQry, $link) or die('Error with query: ' .
    mysql_error());

    // get the number of rows returned
    $numResults = mysql_num_rows($results);
    //divide by 3 to try and split the results up evenly
    $maxDist = round(($numResults / 3), 0);

    // get three random distances
    $distOne = rand(1, $maxDist);
    $distTwo = rand(1, $maxDist);
    $distThree = rand(1, $maxDist);

    $eol = "\n";

    // move forward the first distance...
    for ($intI=1; $intI < $distOne; $intI++) {
    $row = mysql_fetch_assoc($results);
    }
    // display what you have
    echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;

    // move forward the second distance...
    for ($intI=1; $intI <= $distTwo; $intI++) {
    $row = mysql_fetch_assoc($results);
    }
    // display what you have
    echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;

    // move forward the third distance...
    for ($intI=1; $intI <= $distThree; $intI++) {
    $row = mysql_fetch_assoc($results);
    }
    // display what you have
    echo $row['name'] . ' ' . $row['address'] . ' ' . $row['age'] . $eol;
    }
    ?>

    Crispy_One Guest

  3. #3

    Default Re: newbie question with php and mysql

    One quick thing to add is that you just as easily assign these three to strings and display them when needed...
    Crispy_One Guest

  4. #4

    Default Re: newbie question with php and mysql

    Brilliant! thanks Crispy, ill implement it now!
    levels Guest

  5. #5

    Default Re: newbie question with php and mysql

    cool :-)
    Crispy_One Guest

Similar Threads

  1. Creating datasource with mysql- newbie question
    By Hydrowizard in forum Coldfusion Database Access
    Replies: 1
    Last Post: January 13th, 08:17 PM
  2. newbie question: using DBI, DBI::mysql
    By sstark@nospam.com in forum PERL Modules
    Replies: 0
    Last Post: November 9th, 10:06 PM
  3. newbie Q: Using CSV instead of MySql
    By Citizen X in forum PHP Development
    Replies: 6
    Last Post: September 27th, 08:01 PM
  4. newbie question: reading from mysql
    By John in forum PHP Development
    Replies: 7
    Last Post: September 6th, 09:33 PM
  5. newbie php MySQL table question
    By Noel Ferguson in forum PHP Development
    Replies: 0
    Last Post: July 24th, 08:45 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139