array sorting
Moderator: General Moderators
array sorting
how do i sort an array by the key?
re
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
well not that simple, i have a resultset from a sql query with a few rows, i want to put the array through a iterator and output the rows to html. i want to sort the rows by a specific column -- a key in the array (since the columns use different names i have to use the numbered keys)
how do i go about telling it which key to sort by?
here is my code: (you might have seen this before)Code: Select all
$sql1="SELECT 'administration', ad_id, ad_date, ad_title, ad_short_desc
FROM cgi_admin
UNION SELECT 'activities', act_id, act_date, act_title, short_desc
FROM cgi_activities";
$q=mysql_query($sql1) ;
$res=mysql_fetch_array($q);print_r($res);
while($res=mysql_fetch_array($q))
{
echo '<table width="545" border="1" cellspacing="2" cellpadding="0" height="113">
<tr>
<td colspan="6" width="535"><strong>'.$res[3].'</strong></td>
</tr>
<tr>
<td width="42"><strong>Date:</strong></td>
<td width="90">'.date("j-m-y" ,$res[2]).'</td>
<td width="53"><strong>Subcat:</strong></td>
<td width="120">'.$res[0].'</td>
<td width="70"><strong>assoc files</strong></td>
<td width="140">$res[assoc_files]</td>
</tr>
<tr height="57">
<td colspan="6" width="535" height="57">'.$res[4].
'<br><A Href="/det_dl.php?subcat=activities&id='.$res[1].'">Click here for more.</a></td>
</tr>
</table>';
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
then it'd be usort() or array_multisort()
try doing this..i used this to sort the records by lastname..
below is a table that have a LastName link and when you click it..records will be sort according to LastName:
here is the sort code:
hope it helps..

below is a table that have a LastName link and when you click it..records will be sort according to LastName:
Code: Select all
<td width="300"><div align="center"><font face="Arial, Helvetica, sans-serif" class="style1"><strong><a href="forview.php?sort=lastname">Last_Name</a></strong></font></div></td>here is the sort code:
Code: Select all
<?php
if (isset($_GET['sort']) && $_GET['sort'] == 'lastname') {
$select = "Select * from [i]table_name[/i] order by [i]column_name[/i]";
$result = mysql_query($select) or die(mysql_error());
$num = mysql_num_rows($result);
if ($num > 0) {
$i=1;
while($row = mysql_fetch_array($result)) {
echo '
<tr>
<td><div class="style1">' . $i . '</div></td>
<td><div class="style1">' . $row['LastName'] . '</div></td>
<td><div class="style1">' . $row['FirstName'] . '</div></td>
<td><div class="style1">' . $row['bldgName'] . '</div></td>
<td><div class="style1">' . $row['UnitOwnerName'] . '</div></td>
<td><div class="style1"><b>Date: </b>' .$row['DateCreated'] .'<br><b>Time: </b>' .$row['ClockCreated'] .'</div></td>
<td><div class="style1"><b>Date: </b>' .$row['LastEdited'] . '<br><b>Time: </b>' .$row['ClockEdited'] . '</div></td>
<td bgcolor="#ECE9D8"><div class="style1" align="center">' . $row['Active'] . '</div></td>
<td><div class="style1" align="center"><a href="viewall.php?id='.$row['id'].'">View/Edit</a></div></td>
<td><div class="style1" align="center"><a href="deleteCustomer.php?id='.$row['id'].'">Delete</a></div></td>
<td><div class="style1" align="center"><a href="printbldg.php?id='.$row['id'].'">Print</a></div></td>
</tr>
';
$i++;
}
}
}
?>re
this is how i ended up doing it - unexpected and simple
theoretically it should sort the first select by the primary column
and the second select by the column i specified but it actually sorts the entire resultset.
thank you all for your help.
Code: Select all
"SELECT id, date, title, short_desc
FROM cgi_admin
UNION SELECT id, date, title, short_desc
FROM cgi_activities
ORDER BY `title` ASC ";and the second select by the column i specified but it actually sorts the entire resultset.
thank you all for your help.