1

I'm attempting to shuffle the users that I have in my database and then output the first and last name of those results. I have PHP error coding in this file and it is not throwing any errors. Just nothing is outputting not even..

if ($shuffle_firstname == true) { echo $shuffle_firstname . $shuffle_lastname; } else { echo "No users have been registered yet."; }

Does anyone see what I'm doing wrong?

$con = mysqli_connect("localhost", "root", "", "db");
$shuffle_run = mysqli_query($con,"SELECT * FROM users WHERE `group`= 3");
$shuffle_numrows = mysqli_num_rows($shuffle_run);
if( $shuffle_numrows > 0) {
    while($shuffle_row = mysqli_fetch_assoc($shuffle_run)){
        $shuffle_id = $shuffle_row['id'];
        $shuffle_firstname = $suffle_row['firstname'];
        $shuffle_lastname = $shuffle_row['lastname'];
        $shuffle_username = $shuffle_row['username'];
        $shuffle_email = $shuffle_row['email'];

        if ($shuffle_firstname == true) {
            echo $shuffle_firstname . $shuffle_lastname;
        } else {
            echo "No users have been registered yet.";
        }
    }
}

if(isset($_POST['shuffle']))  {
    $shuffle_row = array();
    shuffle($shuffle_row);
    foreach ($shuffle_row as $shuffle) {
        echo $shuffle_firstname . " " . $shuffle_lastname;
    }
}

?>
<input type="submit" value="Shuffle" name="shuffle">
Paul
  • 3,348
  • 5
  • 32
  • 76
  • why not use "ORDER BY RAND()" in your mysql statement? – Grumpy Jul 16 '15 at 13:42
  • 2
    `$shuffle_firstname = $suffle_row['firstname'];` you misspelled the second `shuffle` word – tektiv Jul 16 '15 at 13:42
  • Because I will be using the results from this and making a submit button to insert the results into another db table. – Paul Jul 16 '15 at 13:42
  • The misspelled word still didn't cause any results to output. Thanks for that catch though. – Paul Jul 16 '15 at 13:44
  • If you have got blank page this is probably because error reporting is off. What is the output when you enable error reporting? http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – alariva Jul 16 '15 at 13:44
  • I already have error reporting added in and my page is not displaying any errors for this. – Paul Jul 16 '15 at 13:47
  • have you tried using `var_dump()` on several vars to see where could be the problem ? – tektiv Jul 16 '15 at 13:50
  • I changed some values in my db, as for some reason there were not any group#'s as 3. So now the line I mentioned is outputting that data, but still the shuffle isn't working. – Paul Jul 16 '15 at 13:52
  • The problem may be that you are rewriting values of `$shuffle_id`,`$shuffle_firstname`,`$shuffle_lastname`,`$shuffle_username`,`$shuffle_email` during each loop. I guess that's why the program knows only one value associated to these vars and can't shuffle with several values.I may be wrong, I think I misunderstood what you're trying to do – tektiv Jul 16 '15 at 14:01
  • I'm wanting to carry forward the name, last name, email, etc so I can insert this into another db table after I shuffle it. – Paul Jul 16 '15 at 14:04
  • @Paul Do you want to shuffle users or informations you have (like an user's firstname with another user's last name) ? – tektiv Jul 16 '15 at 14:07
  • @tektiv I want to shuffle the users. – Paul Jul 16 '15 at 14:08
  • `$shuffle_firstname == true`? If it's a string, then you shouldn't be comparing to true. maybe `$shuffle_firstname != ''`, or `strlen($shuffle_firstname) > 0` – Marc B Jul 16 '15 at 14:38

1 Answers1

1

You are overwriting your results with an empty array, then you use shuffle on an empty array, then you're looping the empty array using foreach. I've cleaned up the code, could you try this?

<?php
$con = mysqli_connect("localhost", "root", "", "db");
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");

echo 'Normal results: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
    $array[] = $row;
    echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
if (isset($_POST['shuffle'])) {
    shuffle($array);
    echo 'Shuffled results: <br>';
    foreach ($array as $result) {
        echo $result['firstname'] . ' ' . $result['lastname'] . '<br>';
    }
    // echo $results[0]['firstname'] . ' ' . $results[0]['lastname']; // To display one random result
}
?>
<form method="post">
    <input type="submit" value="Shuffle" name="shuffle">
</form>
vonUbisch
  • 1,384
  • 17
  • 32
  • I removed that line and it didn't help. – Paul Jul 16 '15 at 13:47
  • @Paul I've updated my answer, please take a look. Haven't tested this, but this should work. – vonUbisch Jul 16 '15 at 14:00
  • I get a bunch of errors like this... Warning: Illegal string offset 'firstname' in /home4/db/public_html/example.com/userCreator.php on line 193 – Paul Jul 16 '15 at 14:05
  • Are you sure the table `users` has a column `firstname`? – vonUbisch Jul 16 '15 at 14:07
  • Yes, it is working in my orginial code here above the shuffle code,,,`if ($shuffle_firstname == true) { echo $shuffle_firstname . $shuffle_lastname; } else { echo "No users have been registered yet."; }` – Paul Jul 16 '15 at 14:09
  • There are too many flaws in your original code. I think you are not suppose to output `"No users have been registered yet."` in your `while` statement. That could output several times `"No users have been registered yet."`. Hence the reformatting. Try to understand the code, not copy paste blindly and think it will work. – vonUbisch Jul 16 '15 at 14:11
  • I understand the code for the most part, I just don't get why nothing is displaying. – Paul Jul 16 '15 at 14:19
  • I've updated my answer again with a simplified version, and tested the code, this works on my side. – vonUbisch Jul 16 '15 at 14:25
  • Works great! Is there a way to make the results show up one at a time? – Paul Jul 16 '15 at 14:40
  • I do not understand what you mean. – vonUbisch Jul 16 '15 at 14:50
  • Say I had 10 results. Rather than all 10 shuffle results displaying at one, for them to appear one at a time. – Paul Jul 16 '15 at 14:53
  • Instead of the `foreach` statement, just use `echo $results[0]['firstname'] . ' ' . $results[0]['lastname'];`. This will return the first row of the shuffled array. But there are many ways to accomplish this. – vonUbisch Jul 16 '15 at 14:57