newbie question with php and mysql

Ask a Question related to Macromedia Dynamic HTML, Design and Development.

  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. Similar Questions and Discussions

    1. Creating datasource with mysql- newbie question
      Hi all, I have been using a program to connect to mysql database on my shared hosting. I am now setting up a site in dreamweaver and a datasource...
    2. newbie question: using DBI, DBI::mysql
      Could you please explain this further? I'm not sure what I'm supposed to do here. My /etc/my.cnf file reads: host=mysql.addr.com I've tried...
    3. newbie Q: Using CSV instead of MySql
      I have a bit of a problem, I am currently employed (as work study at a university) to maintain a rather large (5k files) site for the college I am...
    4. newbie question: reading from mysql
      Hi, I am trying to read some info from my mysql tables and display it. This is what I have now which works: $select = "SELECT `name` FROM...
    5. newbie php MySQL table question
      Thanks to you both guys. As for reading the manual - I have 4 in front of me and couldn't find the answer to my problem. Some things you do find,...
  3. #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

  4. #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

  5. #4

    Default Re: newbie question with php and mysql

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

  6. #5

    Default Re: newbie question with php and mysql

    cool :-)
    Crispy_One Guest

Posting Permissions

  • You may not post new threads
  • You may 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