-1

I have a table with a number of records. By dragging and dropping the text in the input, the user determines which text is for which input or image.

Normally all recessions are displayed in order and it is clear which answer is for which image. I want the column response data to be randomly placed in front of each image.

Note that I do not want every row to be displayed randomly, but I want the data in a column to be displayed randomly.

id answer image
1 test1 test1.jpg
2 test2 test2.jpg

I want Test1 Title to be displayed in front of test2 image and Test2 Title to be displayed in front of test1 image.

I used the following code but it did not change:

$gg1 = "SELECT * FROM groupanswer WHERE id='$nowid' ";
$gg2=mysqli_query($conn,$gg1);
while($gg3=mysqli_fetch_assoc($gg2)){
    $iii=$gg3['answer'];
    $aa = array($iii);
    $key = array_rand($aa);
}

I attached the photo:

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
pooya
  • 45
  • 9
  • I think https://dba.stackexchange.com is more appropriate place to ask your question. – cakyus Nov 10 '21 at 13:50
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 10 '21 at 15:15

1 Answers1

1

You can simply use two arrays for that. One will have id as indexes, and answer as values. This array will keep the original order. The other one array will have id as indexes as well, but image as values. And it will be ramdonly reordered.

$answers = [];
$images = [];
$query = "SELECT * FROM groupanswer WHERE id='" . (int) $nowid . "'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
    $answers[$row['id']] = $row['answer'];
    $images[$row['id']] = $row['image'];
}

$randomOrder = array_keys
$images = shuffle_assoc($images); //Randomize order

//Demo echoing
foreach ($answers as $id => $answer) {
    echo $answer . ' - ' . $images[$id] . '<br />';
}

//Function to shuffle preserving indexes
function shuffle_assoc($list) { 
    $keys = array_keys($list); 
    shuffle($keys); 
    $random = array(); 
    foreach ($keys as $key) { 
        $random[$key] = $list[$key]; 
    }

    return $random;
} 
José Carlos PHP
  • 1,417
  • 1
  • 13
  • 20
  • tnx, but not work – pooya Nov 10 '21 at 15:00
  • I'm sorry, I forgot that shuffle() works fine only with numeric (indexex) arrays, for associative arrays it loses the keys. So I added a function to do the mix (reorder ramdonly) called shuffle_assoc(), as seen in https://stackoverflow.com/questions/4102777/php-random-shuffle-array-maintaining-key-value – José Carlos PHP Nov 10 '21 at 15:09