array can not sort!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sanbad
Forum Newbie
Posts: 4
Joined: Tue Jun 07, 2005 2:17 am
Location: Iran

array can not sort!

Post 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]
User avatar
artexercise
Forum Commoner
Posts: 33
Joined: Thu Nov 20, 2003 9:38 am
Location: Raleigh, NC

Post 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.
sanbad
Forum Newbie
Posts: 4
Joined: Tue Jun 07, 2005 2:17 am
Location: Iran

Post 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
:?: :?: :?: :?: :?: :?: :?:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

show us your db structure of products and categories and products
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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'];
}
Post Reply