Name: Anonymous 2005-12-13 5:14
Anyone know a good script that would take data from a mysql table, import it into a php array, and then sort it? I found tutorials on how to print mysql data, and understood that easily enough... and then found a tutorial on how to sort an array in php, and that made enough sense, but I can't seem to figure out how to connect the two.
Here's the relevant bits of code:
<?
$username="username";
$password="password";
$database="your_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
$i++;
}
?>
<?php
// demo array to sort
$latest_array = array(
array('news','1234567890','sdf','asdpojq'),
array('news','1234567892','uruy','xbuqiwpo'),
array('comment','1234567893','fghj','mjktyu'),
array('article','1234567891','cvmo','pjklgg'),
array('news','1234567894','qwenb','asbhtyhj'),
);
$sort_field = 3; // enter the number of field to sort
// compare function
function cmpi($a, $b)
{
global $sort_field;
return strcmp($a[$sort_field], $b[$sort_field]);
}
// do the array sorting
usort($latest_array, 'cmpi');
// demo output
echo '<pre>';
print_r($latest_array);
?>
Here's the relevant bits of code:
<?
$username="username";
$password="password";
$database="your_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
$i++;
}
?>
<?php
// demo array to sort
$latest_array = array(
array('news','1234567890','sdf','asdpojq'),
array('news','1234567892','uruy','xbuqiwpo'),
array('comment','1234567893','fghj','mjktyu'),
array('article','1234567891','cvmo','pjklgg'),
array('news','1234567894','qwenb','asbhtyhj'),
);
$sort_field = 3; // enter the number of field to sort
// compare function
function cmpi($a, $b)
{
global $sort_field;
return strcmp($a[$sort_field], $b[$sort_field]);
}
// do the array sorting
usort($latest_array, 'cmpi');
// demo output
echo '<pre>';
print_r($latest_array);
?>