SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, I have a little problem with this long skript. It is working fine, exept one problem. This skript gets some variable from a form for to search in the database. One variable is $cat ( which is for the category of the event). On the form I have a select menü, and for the option "search in all categories" I have the value "0".
    The problem is, that I can always see the first site of the search result, when I click on the next button I get the mistake " no events found ". But I don't have this problem when I keep the part with "$cat" out of the where string, than everything works fine. So something seems to be wrong with this here :

    if ($cat != "0") {
    $where .= " AND events.cat='$cat'";
    }

    Here is the full skript. Would be nice if you have any idea what could be wrong.

    <?php

    // this staff is just for the date
    if (empty($starttag) OR empty($startmonat) OR empty($startjahr))
    $dateselect = False ;
    else
    $dateselect = True;

    $start = mktime(0,0,0,$starttag,$startmonat,$startjahr);
    $veranstart = date("Y-m-d H",$start);

    $ende = mktime(0,0,0,$endetag,$endemonat,$endejahr);
    $veranende = date("Y-m-d H",$ende);

    // now there is the part which builds up the where string,
    //depending on what the user is searching for.

    $where = " where (events.ev_id > 0 ";
    if ($dc_s != "") {
    $where .= " AND dc_s LIKE '%$dc_s%'";
    }
    if ($name != "") {
    $where .= " AND loc.bez='$name'";
    }
    if ($ort != "") {
    $where .= " AND loc.ort LIKE '%$ort%'";
    }
    if ($plz != "") {
    $where .= " AND loc.plz LIKE '$plz%'";
    }
    if ($cat != "0") {
    $where .= " AND events.cat='$cat'"; // Herer is the problem
    }

    if ($dateselect) {
    $where .= " AND date.date>='$veranstart' AND date.date<='$veranende'";
    }
    $where.=")";


    $limit=5;
    dbConnect ('xxx');

    // This is for to get the number of results for the prev/next
    // function
    $strsql = "SELECT * FROM date LEFT JOIN events ON date.ev_id=events.ev_id LEFT JOIN loc ON events.loc_id=loc.loc_id $where";

    $numresults = mysql_query($strsql);
    $numrows = mysql_num_rows($numresults);


    if (empty($offset)) {
    $offset=0;
    }

    if ($numrows=="") {
    echo("<p>No events found</p>");
    exit();
    }

    if (empty($page)) {
    $page = 1;
    }

    $offset = $limit * ($page - 1);

    // Now the real query

    $strsql = "SELECT events.ev_id, events.dc_s, loc.bez, date.date FROM date LEFT JOIN events ON date.ev_id=events.ev_id LEFT JOIN loc ON events.loc_id=loc.loc_id $where LIMIT $offset,$limit ";

    $result = mysql_query($strsql);
    if (!$result) {
    echo("<P>Following error<BR>".
    "Error: " . mysql_error());
    exit();
    }



    table_out ($result);

    // now the prev / next button - staff

    if ($page > 1) { // bypass PREV link if offset is 0
    $regpage = $page - 1;
    print "<a href='$PHP_SELF?page=$regpage'><img src='prev.gif' border='0'>&nbsp;PREV</a> &nbsp; \n";

    }


    $numpages = intval($numrows / $limit);


    if ($numrows % $limit) {

    $numpages++;
    }

    for ($i = 1; $i <= $numpages; $i++) { // loop thru
    if($page != $i) {
    print "<a href='$PHP_SELF?page=$i'>";
    }
    print $i;
    if($page != $i) {
    print "</a>";
    }
    print "&nbsp; \n";
    }

    if ($page < $numpages) {
    // not last page so give NEXT link
    $refpage = $page + 1;
    print "<a href='$PHP_SELF?page=$refpage'><img src='next.gif' border='0'>&nbsp;NEXT</a><p>\n";
    $vobi = true;
    }

    ?>

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haven't looked in detail but here:

    if ($numrows=="") {
    echo("<p>No events found</p>");
    exit();
    }

    Should be

    if ($numrows==0) {
    echo("<p>No events found</p>");
    exit();
    }
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forget to pass all the vars needed in the query along in the link for the navigation, I assume $cat is needed to make your search perform properly so you would need to pass it to each page of the results right?

    Like here:

    PHP Code:
    print "<a href='$PHP_SELF?page=$i'>"
    should be (make sure all neccesarry vars get passed here

    PHP Code:
    print "<a href='$PHP_SELF?page=$i&cat=$cat'>"
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But so I will have to pass 6 variables, isn't it to much? And as I said at the beginning, it worked fine without the part with "$cat" in the where string, so the problem seems to be anywhere in this part.

  5. #5
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's me again, I tried it to pass all the variables with the prev/next link, but it did'nt solve the problem.
    I do not understand, why the problem is just with this one here:

    if ($cat != "0") {
    $where .= " AND events.cat='$cat'";
    }

    and not with all the other if-clauses that put the where string together.

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easiest way to debug this is to print the query instead of running it then look for obvious errors in the query and try running from the command line or phpMyAdmin to see what errors it produces there.


    PHP Code:
    $strsql "SELECT events.ev_id, events.dc_s, loc.bez, date.date FROM date LEFT JOIN events ON date.ev_id=events.ev_id LEFT JOIN loc ON events.loc_id=loc.loc_id $where LIMIT $offset,$limit "

    print 
    $strsql;
    //esult = mysql_query($strsql); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •