Page 1 of 1

2D Array

Posted: Tue Mar 16, 2010 10:28 am
by carleihar
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I absolutely hate arrays. I try to avoid them in all code I write (which I know is awful), so imagine my dismay when I realized I needed a 2D array!

What I need to do is calculate the horses total score (with info coming from a table) then store that total with its corresponding horseID in an array so that I can sort them (to place them--like a show) and then store their place in a different table. I don't even know where to start!

This is what I have so far...

Code: Select all

 
    $q="SELECT COUNT(*), horseID FROM enteredHorses WHERE showID='$showid'";
    $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        
        $numHorses = $row['COUNT(*)'];
        $horseID = $row['horseID'];
        
        if ($showType == 'DRES' || 'HUNT') {
        
            $total = dresHunt($horseID);
            
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.';
                    include ('includes/footer01.html'); 
                    exit();
                }   
        
        }
 
        if ($showType == 'SJ' || 'CC') {
        
            $total = SjCC($horseID);
                
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.';
                    include ('includes/footer01.html'); 
                    exit();
                }           
        
        }
        
        if ($showType == 'EQ') {
        
            $total = detEq($horseID);
            
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.';
                    include ('includes/footer01.html'); 
                    exit();
                }   
            
        
        }
        
        
        
       

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: 2D Array

Posted: Tue Mar 16, 2010 1:39 pm
by tr0gd0rr
You could turn the 2D array into a hash in this case. Then sort by value.

Code: Select all

// hash of horseID => score
$hash = array();
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
  $hash[$row['horseID']] = $row['COUNT(*)'];
}
asort($hash);
Then `$hash` should be in the order of lowest scores first. If you want highest first, use `array_reverse($hash, true)`

Re: 2D Array

Posted: Tue Mar 16, 2010 3:05 pm
by John Cartwright
Just so you know,

Code: Select all

if ($showType == 'DRES' || 'HUNT') {
evaluates to

Code: Select all

if ($showType == 'DRES' || true) {
//or basically...
if (true) {
What you want is

Code: Select all

if ($showType == 'DRES' || $showType == 'HUNT') {

Re: 2D Array

Posted: Wed Mar 24, 2010 12:27 pm
by carleihar
Thanks both of you for your answers, but I'm still having problems.

Code: Select all

if (($completed == 0) && ($endDate < $today)) {
 
    $hash = array();
    
    
    $q="SELECT COUNT(*), horseID FROM enteredHorses WHERE showID='$showid'";
    $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        
        $numHorses = $row['COUNT(*)'];
        $horseID = $row['horseID'];
        
    
        if ($showType == 'DRES' || $showType == 'HUNT') {
        
            $total = dresHunt($horseID);
            
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.1';
                    include ('includes/footer01.inc.php'); 
                    exit();
                }   
        
        }
 
        if ($showType == 'SJ' || $showType =='CC') {
        
            $total = SjCC($horseID);
                
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.2';
                    include ('includes/footer01.inc.php'); 
                    exit();
                }           
        
        }
        
        if ($showType == 'EQ') {
        
            $total = detEq($horseID);
            
                if ($total == FALSE){
                    echo 'Something went wrong. Please contact an administrator.3';
                    include ('includes/footer01.inc.php'); 
                    exit();
                }   
            
        
        }
        
    
    // hash of horseID => score
    $hash[$row['horseID']] = $total;
 
 
 
 
    } //end  while
 
    $pool = $numHorses * $entryFee;
    
    
    asort($hash);
    print_r($hash);
 
 
tr0gd0rr told me to do this:

Code: Select all

$hash[$row['horseID']] = $row['COUNT(*)'];
Which I changed to equal $total, because I want each horseID to be with it's consecutive total.

Anyways, when I run the script it just shows the first horse in the database, even though $numHorses = 4. Any help?

Also, when I do get it working, how would I store the place in the array (like, first horse (best total), fourth, ext.) into a database?

Thanks for all your help!

Re: 2D Array

Posted: Thu Mar 25, 2010 5:11 pm
by tr0gd0rr
You need to add a GROUP BY clause to the SQL. The field to group by is the horse id.