SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach() does not allow for sorting

    I want to sort a results set, but the foreach() loop does not allow it the way it is presently constructed. Could use some help modifying it so the results set is sorted properly instead of cycling between the query and the display of results.

    The following code comes after a block that finds appropriate records ($pcode) to display.

    Note: To the end of the query, I have added an echo statement to show how the loop works now at the test site:
    http://test.kripalu.org/search.php?s...note+worldwide

    Here is the code:
    PHP Code:
     //pull out applicable records one at a time
    foreach($found_present as $pcode) {
    //dump out codes of purged list
    //echo("<p>P Code: $pcode</p>");
    //get the found presenters one at a time
    $sql = "select 
        presenter.presenterid, 
        presenter.lastname, 
        presenter.firstname, 
        presenter.sanskritname
    from 
        presenter 
    where 
        presenter.presenterid = '$pcode' 
    order by 
        presenter.lastname, presenter.firstname ";
    echo("<p>SQL: $sql</p>");
     
    //retrieves one record and builds a record set 
    $rsSearch = $db->query($sql);
    if (mysql_num_rows($rsSearch)) {
        while ($rowSearch = mysql_fetch_array($rsSearch)) {
    ?>
    <!-- <p> -->
    <?
    //if any results, print links 
    ?>
    <a href="/presenter/<? echo($pcode); ?>/<? echo($_GET["s"]); ?> ">
    <? echo(htmlentities($rowSearch["sanskrit"])); ?> 
    <? echo(htmlentities($rowSearch["firstname"])); ?>
    <? 
    echo(htmlentities($rowSearch["lastname"])); ?></a><br />
    <?
     
        
    }//end while 
    }//end numrows
    }//end foreach
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sort your array before using it in foreach using sort() or any other array sort function of your choice.

  3. #3
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ClickHeRe. Unfortunately, the array $found_present does not contain the same terms as the terms on which I want to sort. It contains the id of the presenter record, so that's all I could sort on using sort() before the foreach loop. What I need to sort on is lastname and firstname, which is acquired in the query within the loop.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  4. #4
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you query for the data first , store the query in an array and then sort the results set after you query?

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use the implode function to create part of your query, enabling you to only perform one query:

    PHP Code:

    $additional_sql 
    implode('\' || presenter.presenterid = \''$found_present);


    $sql "select 
        presenter.presenterid, 
        presenter.lastname, 
        presenter.firstname, 
        presenter.sanskritname 
    from 
        presenter 
    where 
        presenter.presenterid = '
    $additional_sql
    order by 
        presenter.lastname, presenter.firstname " 


  6. #6
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would be my recommendation also

    build an array of all the values to be retreived and sort it, then you can display the result.

    if sort() is not enough, you can use array_multi_sort() (not sure of the exact name) but it enables you to sort in mutliple ways the array

  7. #7
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I'll chew on these ideas.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  8. #8
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did a simple "order by lastname, first " clause in the first foreadh() query that finds the record id. That presented the ids to the display foreach(), so the presenters now are listed in lastname, firstname order.

    Seems now like a no brainer. Thanks for the nudge.
    Paul C.
    ClickBasics
    http://www.clickbasics.com


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
  •