mstern527 Posted June 19, 2007 Share Posted June 19, 2007 I am pretty new to PHP so don't bash my code too much :-). I am trying to build a Golf Leaderboard for a website. I am basically trying to take... Standing | Player | Score 1 Tom 50 2 Ben 51 3 Jon 51 4 Don 52 ...and make it look like this. (notice the "T" for tied) Standing | Player | Score 1 Tom 50 T2 Ben 51 T2 Jon 51 4 Don 52 Here is what the code looks like for the first column in my table where the Standing is printed. // this gets all of the teams that have scores posted for the week played. $count= 0; // this will increment once for every player row. $standing= 1; // this is set to equal $count everytime a score is not tied while($allRows = mysql_fetch_assoc($result)) { $count++; // Print out the scores for each player ?> <tr> <td> <?php if ($allRows['score'] == next($allRows['score'])) { echo "T" .$standing; } else { $standing = $count; echo $count; } ?> </td> I should also add that the error message I am getting looks like this... "Warning: next() [function.next]: Passed variable is not an array or object in..." Thanks in advance for anyones help. Mike Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/ Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 Show me your arrays in php Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277367 Share on other sites More sharing options...
teng84 Posted June 19, 2007 Share Posted June 19, 2007 i think the error is this if ($allRows['score'] == next($allRows['score'])) it say it should be an array so if ($allRows['score'] == next($allRows)) hope it works Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277376 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 i thought that too, however we don't know the structure of $allRows it could be multi-D and scores could be a subset array to the array $allRows Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277380 Share on other sites More sharing options...
teng84 Posted June 19, 2007 Share Posted June 19, 2007 if this is correct $allRows['score'] then its not multi d like what the other guy is saying any way sorry i just want to comment i just cant find ways to waste my time cool dude great job maybe you can help me also if i have some trouble with my coding relax Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277382 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 i agree was playing a bit of online poker and lost so now i'm doing this till I fall asleep. Its so much easier answering forumn questions than working on my work that needs to be done Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277385 Share on other sites More sharing options...
teng84 Posted June 19, 2007 Share Posted June 19, 2007 ASTIG !!!!! Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277389 Share on other sites More sharing options...
mstern527 Posted June 19, 2007 Author Share Posted June 19, 2007 OK... here is where my newbieness is going to come into play. I tried passing just the array like you said... ex: If ($allRows['score'] == next($allRows)) But that didn't work either. I don't know what you mean by show you my array. $result is the result of a query with many columns from my db table. score was one of the columns from the table. example: $result = mysql_query ("SELECT score, x, y, z FROM scores WHERE tournament_id=1"); Then to create the array I did: while($allRows = mysql_fetch_assoc($result)) {... Is there a function I can run that will print the entire array $allRows ? Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277393 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 ahh now that we know its a mysql query give me a minute and i can do it Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277395 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 try this: <?php $result = mysql_query ("SELECT score, x, y, z FROM scores WHERE tournament_id=1" ORDER BY scores ASC); $i = -1; while($rows = mysql_fetch_assoc($result)) { $i++; $data[$i]['score'] = $rows['scores']; $data[$i]['player'] = $rows['player']; //Don't know the mysql field for this } echo "<table><tr><td>Standing</td><td>Player</td><td>Score</td></tr>"; $numplayers = count($data); $i = 0; $j = 1; $standing = 1; while ($i <= $numplayers) { if($data[$i]['score'] == $data[$j]['score']) { echo "<tr><td>T".$standing."</td><td>".$data[$i]['player']."</td><td>".$data[$i]['score']."</td></tr>"; echo "<tr><td>T".$standing."</td><td>".$data[$j]['player']."</td><td>".$data[$j]['score']."</td></tr>"; $i++; $j++; } else { echo "<tr><td>".$standing."</td><td>".$data[$i]['player']."</td><td>".$data[$i]['score']."</td></tr>"; } $i++; $j++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277399 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 I tried it with dummy data and it works fine: <?php /* $result = mysql_query ("SELECT score, x, y, z FROM scores WHERE tournament_id=1" ORDER BY scores ASC); $i = -1; while($rows = mysql_fetch_assoc($result)) { $i++; $data[$i]['score'] = $rows['scores']; $data[$i]['player'] = $rows['player']; //Don't know the mysql field for this } */ $data[0]['score'] = "50"; $data[0]['player'] = "Tom"; $data[1]['score'] = "50"; $data[1]['player'] = "Jon"; $data[2]['score'] = "51"; $data[2]['player'] = "Mike"; $data[3]['score'] = "52"; $data[3]['player'] = "Jerry"; $data[4]['score'] = "52"; $data[4]['player'] = "Gary"; $data[5]['score'] = "56"; $data[5]['player'] = "Bob"; sort($data); $numplayers = count($data); $i = 0; $j = 1; $standing = 1; echo "<table><tr><td>Standing</td><td>Player</td><td>Score</td></tr>"; while ($i < $numplayers) { if($data[$i]['score'] == $data[$j]['score']) { echo "<tr><td>T".$standing."</td><td>".$data[$i]['player']."</td><td>".$data[$i]['score']."</td></tr>"; echo "<tr><td>T".$standing."</td><td>".$data[$j]['player']."</td><td>".$data[$j]['score']."</td></tr>"; $i++; $j++; } else { echo "<tr><td>".$standing."</td><td>".$data[$i]['player']."</td><td>".$data[$i]['score']."</td></tr>"; } $i++; $j++; $standing++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277402 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 only issue i see is that it doesn't handle greater than 2 way ties let me try something Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277404 Share on other sites More sharing options...
mstern527 Posted June 19, 2007 Author Share Posted June 19, 2007 Thanks for all your help cooldude832. I really appreciate it. I think I understand what you did. Am I correct in saying you are basically mimicking the next() functionality manually with $i and $j in a new multi-d array? Why didn't the next() work in my case? In the documentation I read regarding that function it seemed like it was going to work great. It is going to take some massaging to take your code and make it work with my page. In the examples I gave above I simplified things a lot. I am going to try to make it work tomorrow morning. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277417 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 i emailed you a good version Quote Link to comment https://forums.phpfreaks.com/topic/56147-nextarray/#findComment-277422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.