Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    nca
    nca is offline
    New Coder
    Join Date
    Dec 2003
    Posts
    39
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Storing MYSQL Results into an array

    Storing MYSQL Results into an array

    Here is the code I have:
    PHP Code:
    echo "<table border=0 width=200 bgcolor=\"#FFFFFF\" cellpadding=5 cellspacing=0><tr><td colspan=2 align=center bgcolor=\"#FFFF00\"><b><font face=verdana size=3>Today's Rankings</font></b></td></tr>";            
            
    // get all stylists that have worked today
    $qgasthwt mysql_query("SELECT STYLIST FROM TIMECLOCK WHERE DATEOFPUNCH='$yra' GROUP BY STYLIST");
    $qgasthwt2 mysql_num_rows($qgasthwt);
    $GAI 0;
    while (
    $GAI $qgasthwt2) {
    if (
    $d == "") {$bgc "#FFFFCC";$d=1;} else {$bgc "#FFFFFF";$d=0;}
    // Get stylist name we are working with this time around
    $GSNAME mysql_fetch_array($qgasthwt);
    // Add up their points
    $autp mysql_query("SELECT SUM(STYLISTPOINTS) FROM CHECKOUT WHERE STYLIST='$GSNAME[STYLIST]' AND DATE='$yra'");
    $autp2 mysql_fetch_array($autp);
    // Get their name
    $gtn3 mysql_query("SELECT NAME FROM STYLIST WHERE ID='$GSNAME[STYLIST]'");
    $gtn9 mysql_fetch_array($gtn3);

    echo 
    "<tr bgcolor=$bgc><td>$gtn9[NAME]</td><td>$autp2[0]</td></tr>";
            
    $GAI++;
    }
            
    echo 
    "</table>"
    It currently outputs this:

    Today's Rankings
    Albert 4
    Orner 0
    Satterfield 1
    Knair 0
    Morgan 3


    I need it to have them show in order now by their number after the name.
    So in other words display as:

    Today's Rankings
    Albert 4
    Morgan 3
    Satterfield 1
    Orner 0
    Knair 0


    Is this possible by somehow entering the results into an array then organizing the array output?

    Any help is greatly appreciated! Thank you!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    You are already using arrays - $GSNAME, $autp2, and $gtn9 are arrays so you can use PHP array methods on them to organise them.

    A better solution though would be to use an ORDER BY clause in the SQL to organise the results before you retrieve it into the array.

    Note that the mysql_ calls are deprecated and will no longer exist in the next version of PHP. You need to replace them with PDO or mysqli_ calls before then.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    nca
    nca is offline
    New Coder
    Join Date
    Dec 2003
    Posts
    39
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Can I use ORDER BY when it needs to run "SELECT SUM(STYLISTPOINTS) FROM CHECKOUT WHERE STYLIST='$GSNAME[STYLIST]'" first and then I'm saying how would I then use those results to have them in order from highest to lowest?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    No you can't use an aggregate in the order by.

    So you'd either have to create a temporary table with the output of the select and then use a second select on the temporary table with the order by or more simply just use PHP to sort the array that you are retrieving the entries into.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    nca
    nca is offline
    New Coder
    Join Date
    Dec 2003
    Posts
    39
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Would you be able to help in how I can have PHP create and then sort an array?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    You mean something like:

    Code:
    $autp = mysql_query("SELECT SUM(STYLISTPOINTS) FROM CHECKOUT WHERE STYLIST='$GSNAME[STYLIST]' AND DATE='$yra'"); 
    $autp2 = mysql_fetch_array($autp);
    sort($autp2);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •