Jump to content

Sorting an array


BelowZero

Recommended Posts

The following code works, but I need to take it a step further and I'm not sure how. What I'd like to do is sort the output so the highest point totals get printed first (along with the associated information). Can anyone help?

 

Here's the site:

http://www.beat-the-spread.net/seasontotals.php

 

Here's the code:

$i=1;
$w=1;
$result = mysql_query( "
SELECT team_name, owner
FROM teams" );
while ($row = mysql_fetch_array($result))
{
echo "<tr>\n",
"</td><td>",
$row['owner'],
"</td><td>",
$row['team_name'];

$result1 = mysql_query( "
SELECT bye, owner_pts
FROM schedule
WHERE team = $i");
$num_rows = mysql_num_rows($result1);

while ($row1 = mysql_fetch_array($result1))
{

	$totalpts = $totalpts + $row1['owner_pts'];

	if (empty($row1['bye']))
	{
		echo "</td><td>", $row1['owner_pts'], "</tr>\n";
	}
	else
	{
		echo "</td><td>","0","</tr>\n";
	}

}

while ($num_rows<=16)
{
	echo "</td><td>","","</tr>";
	$num_rows++;
}

echo "</td><td>","$totalpts","</tr>";
$totalpts=0;
$w++;
$i++;	
}

echo "</table>\n";

mysql_close($con);

 

Thanks!

Link to comment
Share on other sites

I would assume that in your mysql query you use the sort function with a where clause. then it would save it to the array in the order that it is sorted in.

 

What I would do is first echo out the sql results to see if it is in the order you want and then after that pass it into an array.

Link to comment
Share on other sites

I'm not storing a total in the database, only the points per week. So, I have to figure the totals on the fly. It's while I'm doing the totaling that I want to sort the output. Might be easier to create another field and store the totals in the database.

Link to comment
Share on other sites

rough and not tested, there is a better way of sorting a multiD array, but im too tired lol

also i tidied up your html :)

<?
$i=1;
$w=1;
$data = array();
$result = mysql_query("SELECT team_name, owner FROM teams ");
while ($row = mysql_fetch_array($result))
{
  $result1 = mysql_query("SELECT SUM(`owner_pts`) as total FROM `schedule` WHERE `team` = '$i' ORDER BY total ASC");
  while ($row1 = mysql_fetch_array($result1))
  {
    $data[][$row1['total']]['teamname']=$row['owner'];
  }
}
foreach($data as $k=>$v)
{
  foreach($v as $k2=>$v2)
  {
$rowd[$k]['owner'];
$rowd[$k]['team_name'];
  }
}
?>
<table>
<?
  foreach($rowd as $k=>$row)
  {
?>
    <tr>
      <td>
        <?= $row['owner']; ?>
      </td>
      <td>
        <?= $row['team_name']; ?>
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <table>
          <tr>
	    <?
            $result1 = mysql_query("SELECT bye, owner_pts FROM schedule WHERE team = $i");
            $num_rows = mysql_num_rows($result1);
            while ($row1 = mysql_fetch_array($result1))
            {
              ?>
                <td>
                  <?
			    $totalpts = $totalpts + $row1['owner_pts'];
			    if (empty($row1['bye']))
			    {
				  echo $row1['owner_pts'];
			    }
			    else
			    {
				  echo "0";
			    }
			  ?>
                </td>
              <?
            }
		while ($num_rows<=16)
		{
			echo "<td> </td>";
			$num_rows++;
		}
            ?>
              <td><?= $totalpts; ?></td>
            <?
            $totalpts=0;
            $w++;
            $i++;
	  ?>
        </table>
      </td>
    </tr>
    <?	
  }
  ?>
</table>
<?
mysql_close($con);
?>

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.