problems with sorting an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    #1

    problems with sorting an array

    I have a page that's doing a search of a database, create an array and
    displays it. And I have it displaying the array just fine, but when I
    try to sort it, I get the error:

    Warning: sort() expects parameter 1 to be array, null given in
    /home/iestud/public_html/gto/search.php on line 43
    (line 43 is the sort() function).

    Here's the page--go to:

    and just do a search. The word "for" for example is a good one to show
    most of the products input so far. (The site's not live, so I only
    have a few handful of items to test with.)

    Below is the script itself.

    I did a search for topcs on array sorting and that error, and all I've
    found are instances in which sort() is being put in the wrong place.
    And I'm pretty sure I have it in the right place...but I'm not
    positive.
    I'm likely doing something horribly wrong...I just have no idea now.
    =/

    Thanks for any help! Just a clue even. =)
    Liam

    $search_txt = $_POST['txt_search'];
    $search_txt_arr ay = explode(" ", trim($search_tx t));
    $sql_cat = "SELECT * FROM tbl_category ORDER BY ct_name";
    $result_cat = @mysql_query($s ql_cat, $dbh);
    $numrows_cat = mysql_num_rows( $result_cat);
    while ($row_cat = mysql_fetch_arr ay($result_cat) ) {
    unset($pr_disp, $pr_disp2);
    $c = $row_cat[ct_id];
    $c_name = $row_cat[ct_name];
    For($i = 0; $i <= count($search_t xt_array); $i++) {
    unset($pr_disp, $pr_collect);
    $sql_search = "(SELECT pr_id, pr_name, pr_description,
    pr_category, pr_subcategory FROM tbl_product WHERE pr_keywords LIKE
    '%".$search_txt _array[$i]."%' AND pr_category = '".$c."' ORDER BY
    pr_name) ".
    "UNION ".
    "(SELECT pr_id, pr_name, pr_description, pr_category,
    pr_subcategory FROM tbl_product WHERE pr_name LIKE
    '%".$search_txt _array[$i]."%' AND pr_category = '".$c."' ORDER BY
    pr_name) ".
    "UNION ".
    "(SELECT pr_id, pr_name, pr_description, pr_category,
    pr_subcategory FROM tbl_product WHERE pr_description LIKE
    '%".$search_txt _array[$i]."%' AND pr_category = '".$c."' ORDER BY
    pr_name)";
    $result_search = @mysql_query($s ql_search, $dbh);
    while ($row_search = mysql_fetch_arr ay($result_sear ch)) {
    $pr_cat = $row_search[pr_category];
    $pr_subcat = $row_search[pr_subcategory];
    $pr_name = $row_search[pr_name];
    $pr_desc = $row_search[pr_description];
    $pr_collect .= $pr_name."-".$pr_desc. "<br />";
    $pr_disp = array($pr_colle ct); //array("is_dir" => 0, "name" =>
    "b.jpg");
    } // end WHILE
    sort($pr_disp);
    reset($pr_disp) ;
    } // end FOR
    for ($i = 0; $i < count($pr_disp) ; $i++) {
    $pr_disp2 .= "<div class=\"tah11\" >".$pr_disp[$i]."</div>";
    }
    $master_disp .= "<div><a href=\"#\"
    class=\"tah11Bl ueBold\">".$c_n ame."</a></div>".$pr_disp2 ."<p />";
    } // end WHILE
  • Janwillem Borleffs

    #2
    Re: problems with sorting an array

    LRW wrote:[color=blue]
    > I did a search for topcs on array sorting and that error, and all I've
    > found are instances in which sort() is being put in the wrong place.
    > And I'm pretty sure I have it in the right place...but I'm not
    > positive.
    > I'm likely doing something horribly wrong...I just have no idea now.
    > =/[/color]

    Your main problem is that your code has become unreadable, so let's start
    narrowing it down to the parts needed to start debugging:
    ....
    $result_search = @mysql_query($s ql_search, $dbh);
    while ($row_search = mysql_fetch_arr ay($result_sear ch)) {
    ...
    $pr_disp = array($pr_colle ct);
    } // end WHILE
    sort($pr_disp);
    reset($pr_disp) ;
    ....

    See what happens? Only when the query succeeds, the while loop is entered
    and $pr_disp is initialized as an array. Otherwise, $pr_disp is initialized
    with an empty value (with the proper error_reporting level, a notice will be
    displayed).

    You can surpress the error message by prepending an at-sign to the function
    call:

    @sort($pr_disp) ;

    But since you are assigning an array with just one value to $pr_disp,
    sorting it really makes no sense. The following would make sense:
    ....
    $pr_disp = array();
    ....
    while ($row_search = mysql_fetch_arr ay($result_sear ch)) {
    ...
    $pr_disp[] = array($pr_colle ct);
    } // end WHILE
    ....
    sort($pr_disp);
    ....


    JW



    Comment

    Working...