DSLR Photography Forum

Full Version: Help with some PHP code...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
...if you don't mind. I'm starting to dislike all those php-freak forums out there because there's alot of attitude. Since I know there's a few php coders 'round here - and everyone here is friendly, I'll try here first.

I need some help with multi-dimensional arrays... I've managed to bush-whack my way through creating one which works beautifully but I still can't quite wrap my head around how they work... The code currently dumps that array into a MySQL db, sorts it, and then spits it out - clearing the table at the end. Nothing is actually stored in the db, it's just a fancy calculator. Anyway, the number of queries is HUGE so I'd like to find a way to do it with arrays... Here's the function, annotated for your viweing pleasure...

Code:
function results($divs, $pnames, $partynum, $seatnum)      //$divs is a multi-array, $pnames is an associative array, $partynum & $seatnum are simple vars
{
    for($pcount=0; $pcount<$partynum; $pcount++){
        for($scount=0; $scount<$seatnum; $scount++){
            $query=mysql_query("INSERT INTO pr (div, party, partyid) VALUES ('{$divs[$pcount][$scount]}', '{$pnames[$pcount]}', '$pcount')");  
// this dumps the $divs array into the db along with identifying variables - keeping these together while sorting is the hard part
        }
    }
    $query="select * FROM pr ORDER BY div DESC LIMIT $seatnum";  //this sorts the  list and spits out the top X numbers
    if ($r = mysql_query($query)) {
        $counter=1;
        while ($row = mysql_fetch_array ($r)) {   //basically how do I creat this array with using MySQL...
            //print $row['div']."&nbsp;".$row['party']."<br>\n";    //USE THIS TO DISPLAY ORDERED LIST FOR VERIFICATION
            if ($counter==1){
                $winner=$row['partyid'];
            }
            $partyid=$row['partyid'];  //these two lines count the number of times each identifier appears witha div number
            $results[$partyid]++;
            $counter++;
        }
        $results['divlimit']=$results[$winner];
        
    }else{
        mysql_error();
    }        
return ($results);
}

If you's like to see what this code does, look here.

Thanks in advance...Smile
I agree that it's using a DB unnecessarily. Essentially a db table is basically a 2D array, and the good thing with php is that you can have pointers to arrays within arrays (arrays of arrays).

So it looks like the db is being constructed using:
VALUES ('{$divs[$pcount][$scount]}', '{$pnames[$pcount]}', '$pcount')

I guess create a 2D array with those values as items... easier said than done though! Smile


Not sure what the query itself is trying to do though...

I did a fair bit of mysql in the past but haven't done so in a while so I'll probably have to go back to php.net and browse through the function lists again (or trawl google for examples). By the way, quick tip - just go to php.net/<function> to quickly jump to reference for that function.
The conceptual problem I have is that the new array would contain two more arrays ($divs & $pnames) which makes it quite complex...
Yeah, it would be messy indeed...

Alternative is to look simplifying your data structure, or accessing / manipulating those arrays in such a way that you can avoid the complexity.

It's a bit hard looking at code without context - maybe take a different approach - can you describe the inputs (and structure) and what output you're expecting from the function?
Funny how once you try to explain things to someone else, they become simpler?? Anyway, I think I've got the array structure down (much simpler than I thought) but I am getting an error I've never seen... Code first: (note that $div is now $quot because apparently I don't know the diff between a dividend and a quotient!!)
Code:
57function results($quots, $pnames, $partynum, $seatnum)
58{      
59    $counter=0;
60    $db=array();
61    for($pcount=0; $pcount<$partynum; $pcount++){
62        for($scount=0; $scount<$seatnum; $scount++){
63            $db[$counter]=$quots[$pcount][$scount];
64            $db[$counter]['party']=$pnames[$pcount];
65            $db[$counter]['partyid']=$pcount;
66            $counter++;
67        }
68    }
69     rsort($db);
70     for($scount=0; $scount<$seatnum; $scount++) {
71         $partyid=$db[$seatnum][partyid];
72         $results[$partyid]++;
73     }
74     //$winner=$db[0][partyid];
75     //$results['quotlimit']=$results[$winner];
76    $results['quotlimit']=$seatnum-1;
77     var_dump($db);             
78return ($results);
79}

The error I'm getting is:
Quote:Warning: Cannot use a scalar value as an array in C:\~~~.php on line 64
Warning: Cannot use a scalar value as an array in C:\~~~.php on line 65
Any ideas?
Well, I managed to solve it!!!! Thanks ST for your help - even if it was just so I wasn't talking to myself, exactly. Wink

If you're interested, here's the code:
Code:
function results($quots, $pnames, $partynum, $seatnum)
{      
    $counter=0;
    for($pcount=0; $pcount<$partynum; $pcount++){
        for($scount=0; $scount<$seatnum; $scount++){
            $db[$counter]['quots']=$quots[$pcount][$scount];
            $db[$counter]['party']=$pnames[$pcount];
            $db[$counter]['partyid']=$pcount;
            $counter++ ;
            }
    }
    rsort($db);
    for($scount=0; $scount<$seatnum; $scount++) {
        $partyid=$db[$scount]['partyid'];
        $results[$partyid]++;
    }
    $winner=$db[0]['partyid'];
    $results['quotlimit']=$results[$winner];
return ($results);
}