Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multi page php script

    Please help
    This script retrieves data from my database. It displayes results on multiple pages. It works great if I assign a static value to $var like “saw”.
    Otherwise when I click next page $var value is empty. I think I know why but cannot fix it. I’ve tried sessions but the session value is lost also.
    I am new to php so be gentle.

    <?php

    $var = @$_GET['q'] ;


    $sk = trim($var);

    // check for a search parameter
    if (!isset($var))
    {
    echo "<p>We dont seem to have a search parameter!</p>";
    exit;
    }


    // Connect to database.
    // select table
    mysql_select_db($dbname, $con);

    // Build SQL Query
    $query = "select * from CDS where top like \"%$sk%\"
    order by top"; // EDIT HERE and specify your table and field names for the SQL query
    $numresults=mysql_query($query);
    $numrows=mysql_num_rows($numresults);
    // get results
    if ($numrows == 0)
    {
    echo "<h4>Results</h4>";
    echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

    }

    //how many rows to show per page
    $rowsPerPage = 5;

    // by default we show first page
    $pageNum = 1;

    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    $pageNum = $_GET['page'];
    }

    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    $count=1;

    // get results
    $query .= " limit $offset, $rowsPerPage";

    $result = mysql_query($query) or die("Couldn't execute query");

    while ($row= mysql_fetch_array($result)) {

    $topic = $row["top"];
    $description= $row["descr"];
    $cd_num = $row["cdnum"];
    $track = $row["track"];
    // result table
    echo "<td>$count </td>";
    echo "<td>$topic</td>";
    echo "<td> $description </td>";
    echo "<td> $cd_num </td>";
    echo " <td> $track </td>";
    $count++;
    }
    $maxPage = ceil($numrows/$rowsPerPage);
    //break before paging
    echo "<br />";
    $self = $_SERVER['PHP_SELF'];
    $nav = '';
    for($page = 1; $page <= $maxPage; $page++)
    if ($page == $pageNum)
    {
    $nav .= " $page "; // no need to create a link to current page
    }
    else
    {
    $nav .= " <a href=\"$self?page=$page\">$page</a> ";
    }

    // creating previous and next link
    // plus the link to go straight to
    // the first and last page

    if ($pageNum > 1)
    {
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    $prev = '&nbsp;'; // we're on page one, don't print previous link
    $first = '&nbsp;'; // nor the first page link
    }

    if ($pageNum < $maxPage)
    {
    $page = $pageNum + 1;
    $next = " <a href=\"$self?page=$page\">[Next]</a> ";
    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    $next = '&nbsp;'; // we're on the last page, don't print next link
    $last = '&nbsp;'; // nor the last page link
    }
    // print the navigation link
    echo $first . $prev . $nav . $next . $last;
    ?>

  • #2
    Regular Coder
    Join Date
    Feb 2010
    Posts
    208
    Thanks
    15
    Thanked 2 Times in 2 Posts
    when you are creating next link, you must send all the info to your same page where it will be read again.
    like:

    PHP Code:
    $next " <a href=\"$self?page=$page\">[Next]</a> "
    must be like:

    PHP Code:
    $next " <a href=\"$self?page=$page?q=$q\">[Next]</a> "
    I don't know why you use "q" but you must send the value again...

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you

    Perfect! I new I was close but you saved me. Q is my input variable from the search form.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    $next " <a href=\"$self?page=$page?q=$q\">[Next]</a> "
    should be
    PHP Code:
    $next " <a href=\"$self?page=$page&q=$q\">[Next]</a> "


  •  

    Posting Permissions

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