Page 1 of 1

array can not sort!

Posted: Tue Jun 07, 2005 2:21 am
by sanbad
Hello
I use php4.3.2
I wrote this script but array can not sort!
Why?! please help me.
*******************************

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<? 

$host = "localhost";
$user = "myname";
$password = "mypass";
$dbname = "mydb";

$tablecat = "products_to_categories";
$tableproducts = "products";


$link = mysql_connect($host,$user,$password);
$query1 = "SELECT * from $tablecat where (categories_id = $cat)";
$result1 = mysql_db_query ($dbname,$query1,$link);

while ($row = mysql_fetch_array($result1)) {
$query2 = "SELECT * from $tableproducts where (products_id = $row[0])";
$result2 = mysql_db_query ($dbname,$query2,$link);
while ($row2 = mysql_fetch_array($result2)) {
$a = $row2[2];
$b = $row2[4];
$b = round($b);
$m = array($a=>$b);
}
arsort($m);
reset($m);
for ($n = 0; $n < count($m); $n++) {
$line = each ($m);
print ("$line[key] ----> $line[value] <br> \n ");
} 
}


?>
</body>
</html>
********************************
result in browser:
DVD-RPMK ----> 42
DVD-MATR ----> 40
DVD-UNSG ----> 30
DVD-UNSG2 ----> 30
DVD-FDBL ----> 24600
DVD-DHWV ----> 32800
DVD-LTWP ----> 29000
DVD-SPEED ----> 32000
DVD-SPEED2 ----> 3500
And when i delete this:
arsort($m);
But result in browser is like above!
Why arsort do not sort my array?

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]

Posted: Tue Jun 07, 2005 6:37 am
by artexercise
The problem lies in the nested while statements.
You are grabbing all the information associated with a single products_id number and processing that and then going back and doing the same thing again, for however many times. Each time it is printing the results before collecting a full array of all values.

Try moving the '}' last curly brace directly above the arsort($m); statement and see what the results are.

Posted: Wed Jun 08, 2005 3:03 am
by sanbad
I test many format for above script. but can not solve problem.
please help me.
if we want capture data from database we must use array and array in above script model can not sort.
please help me how i can sort captured data?
for help me; can you write any script for sort above data? or can you debug my script?
thanks
:?: :?: :?: :?: :?: :?: :?:

Posted: Wed Jun 08, 2005 6:58 am
by John Cartwright
show us your db structure of products and categories and products

Posted: Wed Jun 08, 2005 10:07 am
by timvw
I have good feeling you need a basic programming structures course/manual. This way you will learn how to iterate through a sorted file/resultset with different levels.. It will also allow you to write code that does all this with only 1 query.

Code: Select all

SELECT * 
FROM $tablecat 
INNER JOIN $tableproducts ON (products_id)
WHERE (categories_id = $cat)&quote;;
ORDER BY $tablecat.name, $tableproducts.price

Code: Select all

$current_category = '';

$rs = mysql_query(...);
while ($row = mysql_fetch_assoc($rs))
{
  if ($curent_category != $row['category'])
  {
    // a new category has started...
  }

  // do something with the product row

  $current_category = $row['category'];
}