Skip to main content

Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #24164

    Hi all,
    Its been a while since I have had to mess with PHP but I can’t get pagination to work on a page of searched results. Can anybody help?

    Here is my code, I have tried different things but it don’t seem to work, so here is working code of the page showing all results on a page.

    Thanks Damian

    <?
    mysql_connect("host", "user", "password");
    mysql_select_db("database");

    $search=$_POST["search"];

    $result = mysql_query("SELECT * FROM Raids WHERE Date LIKE ‘%$search%’ OR Target LIKE ‘%$search%’ OR Plane LIKE ‘%$search%’ OR Callsign LIKE ‘%$search%’ OR Pilotsurname LIKE ‘%$search%”");

    while($r=mysql_fetch_array($result))
    {
    $Date=$r["Date"];
    $Plane=$r["Plane"];
    $Callsign=$r["Callsign"];
    $Target=$r["Target"];
    $Pilotsurname=$r["Pilotsurname"];
    $Pilotrank=$r["Pilotrank"];
    $Pilotinitials=$r["Pilotinitials"];
    $Comments=$r["Comments"];

    echo "<table cellpadding=0 border=1>";
    echo "<tr>";
    echo "<th>";
    echo "Date";
    echo "</th>";
    echo "<th>";
    echo "Aircraft";
    echo "</th>";
    echo "<th>";
    echo "Call Sign";
    echo "</th>";
    echo "<th>";
    echo "Target";
    echo "</th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>$Date</td>";
    echo "<td>$Plane</td>";
    echo "<td>$Callsign</td>";
    echo "<td>$Target</td>";
    echo "<tr>";
    echo "<th>";
    echo "Pilot Surname";
    echo "</th>";
    echo "<th>";
    echo "Pilot Rank";
    echo "</th>";
    echo "<th>";
    echo "Pilot Initials";
    echo "</th>";
    echo "</tr>";
    echo "<tr>";
    Print "<td>$Pilotsurname</td>";
    Print "<td>$Pilotrank</td>";
    Print "<td>$Pilotinitials</td>";
    echo "<tr>";
    echo "<th>";
    echo "<tr>";
    echo "<th>";
    echo "Notes";
    echo "</th>";
    echo "<th>";
    echo "</tr>";
    echo "<tr>";
    Print "<td>$Comments</td>";
    echo "<br>";
    echo "</tr>";
    echo "</table>";
    }
    ?>

    #55228
    rcmatt4321
    Member

    Ah, pagination, Personally I think its more fun to figure this out for yourself, so if you want to read the first part, and attempt it, go ahead, but, if you want to use the source, feel free to. This kinda came to me in the shower one day while I was thinking abut my state tech fair project. It was a blog that needed paginated posts…

    You’re going to want to decide how many results per page you want to display. For now, I’d go with 5.

    The theory behind pagination is that if you are on page one, you want to have the first result displayed the page number times the number of results per page minus the results per page. For example.

    If you are on page one, and you are displaying 10 results per page your first result would be the 1st in the database.

    1 (page number) * 10 (posts per page) – 10 (posts per page) = 1 (first result on page)
    2 (page number) * 10 (posts per page) – 10 = 10 (first result on page)

    Thats the theory behind it (If my theory is stupid, someone please correct me :P ).

    Some basic data you’re going to need for this to work. How many results were returned, how many results per page you would like, and what page you are on.

    To find out how many results were returned in your array you should use the PHP mysql_num_rows() function.
    To find out what page you are on, you need to use the GET array, $_GET;
    To know how many results per page, simply set a variable, I recommend a config file with database and others settings inside of it.

    Ok on with your script, remember, you can stop using PHP at any time, and write HTML. Its really really unnecessary (and slow) to use like 30 lines of echos.

    config.php

    Code:

    index.php

    Code:

    Search

    Countries by continent

    ” method=”get” >

    Countries that speak



    &page=“>Previous Page




    &page=“>Next Page

    The code looks really really really ugly so, you can download/view it here http://www.mattegan.net/code.txt

    I hope that helps :D Btw, my PHP may not be the best, as in, it works, but I’m not sure if this is how most people would go about it. I’ve only been programming PHP for about a year, and there are plenty of people way better than me.

    #55210

    You should always escape user inputted strings that are being used in database queries. If you don’t follow this advice malicious users could get unrestricted access to your data.

    Code:
    //getting our search query string
    $search = mysql_real_escape_string($_GET[‘search’]);
    #55233
    rcmatt4321
    Member

    Woops, forgot to mention that. Thanks Dave :D

    Also, the database I was using was the world.sql file you can get off of the mySQL site.

    #55305
    rcmatt4321
    Member

    Please tell me I didn’t go through the trouble of writing and commenting through all that code for nothing…. :?

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.