1

The selected id will select the row and randomize. However, it could randomize the same elements. For example: 'I like to play the piano.' The output I'm expecting is randomized to eg. ' Play the like to I piano ' But what I receives sometimes turns out to be ' I piano piano like to piano' This words comes from the database(phpmyadmin). How do I make it such that the data will all appear but not repeat?

$strSQL = "SELECT * FROM sentences WHERE id 
ORDER BY RAND() LIMIT 1;";

$x = rand(1,4);
echo "$x";
$y = rand(1,4);
echo "$y";
$z = rand(1,4);
echo "$z";
$c = rand(1,4);
echo "$c";

$rs = mysql_query($strSQL);

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

    // Write the data of the person
    echo "<dt>Sentence:</dt><dd>" . $row["$x"] . " " . $row["$y"] . " " . $row["$z"] . " " . $row["$c"] ."</dd>";
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

1

You can do:

//create an array with numbers 1-4
$order = array(1,2,3,4);

//shuffle them in random order
shuffle($order);

$rs = mysql_query($strSQL);

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
    // Write the data of the person
    //Display all the array values from 0-3 (array index starts from 0)
    echo "<dt>Sentence:</dt><dd>" . $row[$order[0]] . " " . $row[$order[1]] . " " . $row[$order[2]] . " " . $row[$order[3]] ."</dd>";
}

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
AyB
  • 11,609
  • 4
  • 32
  • 47
  • 2
    [Another good tutorial series on PDO](https://www.youtube.com/watch?v=QtCdk459NFg) can be found here (phpacademy on youtube). – MisterBla May 27 '14 at 07:07
  • May i know how do i make the selected row not repeated using this code? – user3678617 May 27 '14 at 08:26
  • @user3678617 You mean each individual word or as a sentence? – AyB May 27 '14 at 08:28
  • For example: first sentence, which is id 1, contains of 'I like to play the piano' which will be store in the database as $order = array(1,2,3,4);. Second sentence, which will be id 2, 'I love to eat chicken wings'. How do I make like a game which the sentence does not repeat? – user3678617 May 27 '14 at 08:34
  • @user3678617 Make an empty array, check if the sentence does not exist in the array, if it doesn't display it and add it to the array. At the end, all the values in this array would be unique ones. – AyB May 27 '14 at 09:34
1

Try using the shuffle() function.

$strSQL = "SELECT * FROM sentences WHERE id 
ORDER BY RAND() LIMIT 1;";

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

    shuffle($row);

    // Write the data of the person
    echo "<dt>Sentence:</dt><dd>" . implode(' ', $row) . "</dd>";
}

Unrelated: mysql_* are deprecated as of php 5.5 and will be removed in the future. Use mysqli or PDO instead.

MisterBla
  • 2,355
  • 1
  • 18
  • 29