sorting array inside loop possible?

Discussion in 'PHP' started by bugcoder, Sep 18, 2008.

  1. #1
    it seems illogical but i want to ask is there the way to make a numeric array inside the loop and echo it in the sorted order within the same loop?
     
    bugcoder, Sep 18, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    To assign a new array in php you do this
    
    $array[] = "Some value";
    
    PHP:
    If $array[0] or 1 or so on exist, the new assigned array index is the next integer.
     
    Kaizoku, Sep 18, 2008 IP
  3. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for reply dear.. i think i should explain it with code what i want to do

    here is the sample code...its just displayed that what i want to achive dont debug its syntex.

    
    $ColorSql = select * from tblcolors where fabricid=48;
    
    $ColorResult=mysql_query($ColorSql) or die (mysql_error());
    $ColorNor=mysql_num_rows($ColorResult);
    
             if ($ColorNor!=0) {
                         for ($C=0;$C<$ColorNor;$C++) {
    
    			 $colorid=mysql_result($ColorResult,$C,'cid');
    
       		         $priceSql = select price from tblprices where cid=$colorid;
    
    			//now upon execution of above statement in loop here i want the prices in lowest to highest
    
    
    		     }
              }
    
    PHP:
     
    bugcoder, Sep 18, 2008 IP
  4. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Change an sql to
    
     // you need to learn basic SQL ;)
     $priceSql = "select price from tblprices where cid=$colorid order by price";
    
      // if you want to sort array enywhere even in loop use any of PHP sorts
    foreach($otherArray as $g){
      $array  = array(7,9,2,1);
      asort($array);
    
     // though if you try here to sort $otherArray  the results may be undefined
     // but you can always try it ..
    }
     
    
    PHP:
     
    classic, Sep 18, 2008 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    $priceSql = 'select price from tblprices where cid=$colorid ORDER BY PRICE DESC;';

    Should solve your problem... no need for array sorting.

    Anyway allow me one other suggestion:
    Avoid using mysql_num_rows($ColorResult) to make a loop on its result. Since you are not transactionning your code, the value of the query result might change within the loop due to the action of concurrent users. If your result set gets lower than it was when the statement was executed, then it will generate an error, because you will try to browse an element that is not existing anymore.

    Try as follow:
    
    $query_string="select * from tblcolors where fabricid=48;";
    $response=mysql_query($query_string, $db);
    $result = mysql_fetch_assoc ($response);
    while ($result)
    {
        $priceSql = "select price from tblprices where cid=".$result['cid']." order by price desc";
        //deal with the fetching on $priceSql here
    
        $result = mysql_fetch_assoc ($response);
    }
    
    PHP:
     
    webrickco, Sep 19, 2008 IP