Jump to content

next($array)


mstern527

Recommended Posts

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

Link to comment
Share on other sites

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  ;D cool dude great job maybe you can help me also if i have some trouble with my coding relax

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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>";
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.