dimanche 30 octobre 2016

How to find what random row from a random table was chosen

I'm trying to create a website where users can vote on different items from a game, like a tierlist. I want the item that a user votes on to be from a random table and a random row in that table.

Right now, I have the following code set up to find the random item:

$ran = mt_rand(1, 10);
switch ($ran) {
    case "1":$sql = "SELECT item, rating FROM ditems ORDER BY RAND() LIMIT 1";  
         $result = $conn->query($sql);  
         while($row = $result->fetch_assoc()) {
            echo $row["gun"];
         };
         break;
    case "2":$sql = "SELECT item, rating FROM citems ORDER BY RAND() LIMIT 1";  
         $result = $conn->query($sql);  
         while($row = $result->fetch_assoc()) {
            echo $row["item"];
         }; 
etc

The problem I run into is that I can't access what the chosen item is outside of the switch{} statement because the $row array is declared locally, but I have to be able to do that so that I can seet up a button to vote on the item.

What am I missing? Or is there a easier way to do this?




Aucun commentaire:

Enregistrer un commentaire