SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Elyira, OH
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Ordering an Array

    I have a php array that I need to figure out how to order differently.

    Here is the code that I am using:

    Code:
    $prod_id = array();
    
    $result = mysql_query("SELECT * FROM categories2products WHERE cat_id='$cate_id'");
    	while ($row = mysql_fetch_array ($result))
    		{
    		$prod_id[] = $row['prod_id'];
    		}
    
    foreach($prod_id as $value) {
    
    $result = mysql_query("SELECT * FROM products WHERE product_id='$value' AND display='yes' ORDER BY product_title ASC, product_id ASC");
    while ($row = mysql_fetch_assoc ($result))
    					{
    The second query is how I want to have it ordered, but for some reason it doesn't do anything with that order part. How would I get it to order that way?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You select one product id at the time. Assuming it's a unique key for the product table, there's nothing to order, since you'll get only one record for each query.

    What you need to do is merge the 2 queries into one, using a JOIN:

    PHP Code:
    $query 
      
    "SELECT p.*
       FROM categories2products cp
       INNER JOIN products p
       ON cp.product_id = p.product_id
       AND p.display = 'yes'
       WHERE cat_id='
    $cate_id'
       ORDER BY product_title ASC, product_id ASC"
    ;
    $result mysql_query($query);
    while (
    $row mysql_fetch_assoc ($result))



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •