0

I wrote a PHP code to get top list player from a WordPress quiz plugin. My problem is that I want to get an array that contains all the users and echo it. I don't know how to do it all the method I do gives me an error.

Here my latest code these one produces me only one results

<?php
global  $wpdb;    

$sql = "SELECT * FROM wp_wp_pro_quiz_toplist order by points DESC";
$result = $wpdb->get_results($sql) or die(mysql_error());

foreach( $result as $results ) {

        $name = array();
        $points = array();

    $name = $results->name;
    $points= $results->points;

}
?>

Here is an example of the echo data:

<div class='col-md-2 col-md-offset-5 topplayers  text-center item '>
    <ul list-unstyled list-inline>
        <li>
           <div class='players'>
        <img src='<?php  echo esc_url(get_template_directory_uri()); ?>/img/logo2.png' alt='cgce-revision'>
           </div>
           <p class='played'>
               player : <?php echo $name; ?>
           </p>

           <p>
               scored : <?php echo $points; ?>
           </p>
    </li>

</div>
Kirk Beard
  • 8,124
  • 12
  • 39
  • 43
Stanly Medjo
  • 71
  • 3
  • 11
  • `$name =` is reassigning `$name` to be equal to the value of `$results->name`. Use `$name[] =` to add a new index to the array with the value of `$results->name`. You then have to deal with the fact that you're recreating the array every interation of the loop. This is pretty basic though, if you didn't know how to add this, you need to spend more time learning PHP before dealing with production web sites. – Devon Sep 20 '17 at 15:01
  • thanks for the fast reply i admit it was stupid of mind putting that new in the for but as i said i was tried cause i already tried what u said but i got – Stanly Medjo Sep 20 '17 at 15:30
  • Array to string conversion error that's why i just did that above sorry – Stanly Medjo Sep 20 '17 at 15:30
  • Yeah, I don't think you understand enough about what you are doing. You can't echo an array which is why you got that error. It seems pretty pointless to assign name and points to new arrays when you could just loop through $result and echo their properties in the view. – Devon Sep 20 '17 at 15:35
  • i tried that also but it but since i am working with boostrap carousel i had a problem cause the first child must have the class active so it could not work i had no choice to do the above – Stanly Medjo Sep 20 '17 at 15:41
  • That has nothing to do with this. You could easily implement a counter inside a loop or implement that in a plentiful of other ways. Like I said before, there's a lot of things that you seem to need more experience and training with so I recommend you learn more about PHP before working with a web site in it. – Devon Sep 20 '17 at 15:46
  • ok thanks , but still have not solve my problem pls – Stanly Medjo Sep 20 '17 at 15:52
  • @Devon Thanks after taking a close look to what you said i took sometime to think and i figured it out . thanks very much – Stanly Medjo Sep 20 '17 at 19:50

0 Answers0