www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to order an array or array output

  1. #1
    Join Date
    Apr 2010
    Posts
    227

    How to order an array or array output

    I have a simple function that takes the content of an array of products and if they have amounts in them output the contents into a csv file.
    (user then clicks a link to open the csv file.)

    The array was in turn created from a Mysql DB.

    PHP Code:
    function set_create_csv(){

         if (!
    $this->handle fopen($this->filepath'w'))
                {
                 echo 
    "Cannot open file (".$this->filepath.")";
                }

        
    //file output header
        
    if (fwrite($this->handle"Product Code,Product Description,Unit Price,Qty,\n") === FALSE) {
               echo 
    "Cannot write to file (".$this->filepath.")";
               }

            foreach(
    $this->all_items as $key => $value){
            if(
    $this->all_items[$key]['amount']){
                
    $description $this->all_items[$key]['description'];
                
    $buyprice $this->all_items[$key]['buyprice'];
                
    $qty $this->all_items[$key]['amount'];
                
    fwrite($this->handle"$key,$description,$buyprice,$qty\n");
            }
        }

    Simple and it works.

    The problem is that the output is in order of the array keys (which are actually part numbers) and not in a logical grouping that users can easily scan.

    My question is how do i order the output so that i can group parts together logically?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    My first recommendation would be to change the database query with an "order by" clause that sorts the results as desired.

    If that is impractical for some reason, then you can look at uasort() and its related functions to sort the array for you.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2010
    Posts
    227
    *($&^#(*^#( #$()^#$

    First thing I'd tried was ordering by my mysql query.

    Didnt have ANY effect. So i convinced myself that that wasnt how the foreach statement was pulling the data from the array.

    Well, of course it was how it does using the internal pointers.

    So after your post i went back and sure enough my mysql order by statement was up the duff. Corrected and works like a treatment.

    So how do i order it?

    I created a table colum called 'order' and gave each part a number that is in order of importance and grouped similar parts together.

    PHP Code:
        $query "SELECT * FROM `items` ORDER BY `items`.`Order` ASC"
    What a n00b!

    So thankyou for the tip nogdog and for not slapping me with a wet fish like a really had earnt.

    Solved

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles