Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2003, 12:23 PM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jams30
Default Pagination problems - please help

I've created a page that calls different functions from an inc file depending on the case in a switch statement. When the page is first run, it displays a search interface on the left and some blurb in the main body of the page. When the form in the interface on the left is submitted, the page is refreshed calling a new function that queries a database and returns the results. This is all fine to this point.

However, when I try to include some script that paginates the results (limiting each page to 10 results), the first 10 results are displayed with links to each next 10 results, but when I click on any of these links, the page is refreshed as though it was loaded for the first time - (no results, just the interface and the blurb as before)

This is the code from the include:

<?php

function mailing_list()
{
?>
      <TR>
       <TD WIDTH=492 BACKGROUND="images/horizontal_dot_18.gif" HEIGHT=20></TD>
      </TR>
      <TR>
       <TD WIDTH=492>Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here..........Mailing list stuff goes here.........</TD>
      </TR>
<?php
}

function s_interface()
{

# FROM AND TO PRICES LISTBOXES
echo "<FORM METHOD=POST ACTION='$PHP_SELF'>";
echo "<TABLE WIDTH='255' BORDER='0' CELLPADDING='0' CELLSPACING='0'>";
echo "<INPUT TYPE='HIDDEN' NAME='action' VALUE='s_results'>";
echo "<TR><TD WIDTH='130' NOWRAP COLSPAN='2'>[u]Basic Property Search</u></TD></TR>";
echo "<TR><TD WIDTH='130' COLSPAN='2'>Price Range:</TD></TR>";
echo "<TR><TD WIDTH='130'>From:</TD>";
echo "<TD WIDTH='125'><SELECT NAME='From_Price'>";
$Bottom=array("85000", "95000", "110000", "120000", "130000", "140000", "150000", "160000", "170000", "180000", "190000", "220000");
echo "<OPTION VALUE=$Bottom[0]>$Bottom[0]</OPTION>";
echo "<OPTION VALUE=$Bottom[1]>$Bottom[1]</OPTION>";
echo "<OPTION VALUE=$Bottom[2]>$Bottom[2]</OPTION>";
echo "<OPTION VALUE=$Bottom[3]>$Bottom[3]</OPTION>";
echo "<OPTION VALUE=$Bottom[4]>$Bottom[4]</OPTION>";
echo "<OPTION VALUE=$Bottom[5]>$Bottom[5]</OPTION>";
echo "<OPTION VALUE=$Bottom[6]>$Bottom[6]</OPTION>";
echo "<OPTION VALUE=$Bottom[7]>$Bottom[7]</OPTION>";
echo "<OPTION VALUE=$Bottom>$Bottom</OPTION>";
echo "<OPTION VALUE=$Bottom[9]>$Bottom[9]</OPTION>";
echo "<OPTION VALUE=$Bottom[10]>$Bottom[10]</OPTION>";
echo "<OPTION VALUE=$Bottom[11]>$Bottom[11]</OPTION>";
echo "</SELECT></TD></TR>";
echo "<TR><TD>To:</TD>";
echo "<TD><SELECT NAME='To_Price'>";
$Top=array("85000", "95000", "110000", "120000", "130000", "140000", "150000", "160000", "170000", "180000", "190000", "220000");
echo "<OPTION VALUE=$Top[0]>$Top[0]</OPTION>";
echo "<OPTION VALUE=$Top[1]>$Top[1]</OPTION>";
echo "<OPTION VALUE=$Top[2]>$Top[2]</OPTION>";
echo "<OPTION VALUE=$Top[3]>$Top[3]</OPTION>";
echo "<OPTION VALUE=$Top[4]>$Top[4]</OPTION>";
echo "<OPTION VALUE=$Top[5]>$Top[5]</OPTION>";
echo "<OPTION VALUE=$Top[6]>$Top[6]</OPTION>";
echo "<OPTION VALUE=$Top[7]>$Top[7]</OPTION>";
echo "<OPTION VALUE=$Top>$Top</OPTION>";
echo "<OPTION VALUE=$Top[9]>$Top[9]</OPTION>";
echo "<OPTION VALUE=$Top[10]>$Top[10]</OPTION>";
echo "<OPTION VALUE=$Top[11]>$Top[11]</OPTION>";
echo "</SELECT></TD></TR>";

# BEDROOMS DROP DOWN LIST BOX

echo "<TR><TD COLSPAN='2'>Bedrooms:</TD></TR>";
echo "<TR><TD WIDTH='130'>At Least:</TD><TD><SELECT NAME='Bedrooms'>";
$Rooms=array("1", "2", "3", "4", "5", "6");
echo "<OPTION VALUE=$Rooms[0]>$Rooms[0]</OPTION>";
echo "<OPTION VALUE=$Rooms[1]>$Rooms[1]</OPTION>";
echo "<OPTION VALUE=$Rooms[2]>$Rooms[2]</OPTION>";
echo "<OPTION VALUE=$Rooms[3]>$Rooms[3]</OPTION>";
echo "<OPTION VALUE=$Rooms[4]>$Rooms[4]</OPTION>";
echo "<OPTION VALUE=$Rooms[5]>$Rooms[5]</OPTION>";
echo "</SELECT></TD></TR>";

# PROPERTY TYPE DROP DOWN LIST

echo "<TR><TD COLSPAN='2'>Property type?</TD></TR>";
echo "<TR><TD WIDTH='130'> </TD><TD><SELECT NAME='Prop_Type'>";
$Type=array("house", "bungalow", "flat", "caravan", "cardboard box", "other");
echo "<OPTION VALUE=$Type[0]>$Type[0]</OPTION>";
echo "<OPTION VALUE=$Type[1]>$Type[1]</OPTION>";
echo "<OPTION VALUE=$Type[2]>$Type[2]</OPTION>";
echo "<OPTION VALUE=$Type[3]>$Type[3]</OPTION>";
echo "<OPTION VALUE=$Type[4]>$Type[4]</OPTION>";
echo "<OPTION VALUE=$Type[5]>$Type[5]</OPTION>";
echo "</SELECT></TD></TR>";

# LOCATION INFORMATION

echo "<TR><TD COLSPAN='2'>Location:</TD></TR>";
echo "<TR><TD WIDTH='130'> </TD><TD><INPUT NAME='Location' TYPE='TEXT' SIZE='10' VALUE='London'></TD></TR>";
echo "<TR><TD WIDTH='130'> </TD><TD><INPUT TYPE=SUBMIT VALUE='Find a property!!'></TD></TR>";
echo "</FORM>";
echo "</TABLE>";
}

function s_blurb()
{
echo "Welcome to the Property Agency website where you can search through a vast database of properties on offer by estate agents throughout the country!";
}

function s_results()
{
$database = "propertyfinder";
$table_name = "properties";

echo "<center><p>You have searched for a " . $_POST['Prop_Type'] . " with " . $_POST['Bedrooms'] . " bedroom(s) priced from $" . $_POST['From_Price'] . " to $". $_POST['To_Price'] . "</p>";

if ($_REQUEST['From_Price'] > $_REQUEST['To_Price']) {
echo("<p><div align='center'><strong>Your Starting Price is higher than your Maximum Price!</strong></div></p>");
mailing_list();
exit();
}
$table_name = "properties";
$connection = sql_connect();
db_connect();

if (empty($page))
{
    $page=1;
}
else {
    $page = $_GET['page'];
}
echo "Page no: $page<BR>";
$limit = 10;
$limit_value = ($page * $limit) - $limit;
echo "Limit Value is: $limit_value<BR>";
$result = mysql_query("SELECT * FROM $table_name WHERE price >= $_POST[From_Price] AND price <= $_POST[To_Price]");
$total_rows = mysql_num_rows($result);
echo "Total number of records found: $total_rows<BR>";

if (FALSE === $result)
{
    echo "There was a problem with the database query.\n";
    echo "MySQL said: " . mysql_error();
    exit();
}
else if (mysql_num_rows($result) == 0)
{
    echo "Sorry, no matching properties found!<P>";
    mailing_list();
    exit();
}

echo "[center]<strong>[u]Your search has returned " . mysql_num_rows($result) . " result(s). </u></strong></center><P>";
$result = mysql_query("SELECT * FROM $table_name WHERE price >= $_POST[From_Price] AND price <= $_POST[To_Price] LIMIT $limit_value, $limit");
while($query_data = mysql_fetch_array($result))
{
extract ($query_data);
$f_price = number_format($price);
?>
<TABLE BORDER="0" WIDTH="492" CELLSPACING="0" CELLPADDING="0">
<TR>
<TD COLSPAN="2"><IMG SRC='images/results_tabs_17.jpg' WIDTH='492' HEIGHT='39' ALT='Search Results for property ref <?php echo "$prop_id" ?>'><BR></TD>
</TR>
<TR>
<TD WIDTH="392">
<TABLE WIDTH="492" BORDER="0" CELLSPACING="5" CELLPADDING="0">
<TR>
<TD WIDTH="391">Property ref <?php echo "$prop_id" ?> is a <?php echo "$prop_type" ?> and is priced £<?php echo "$f_price" ?> and has <?php echo "$bedrooms" ?> bedroom(s)<P></TD>
<TD WIDTH="101"><IMG SRC=<?php echo "$image1_filename" ?> WIDTH='101' HEIGHT='58' ALT='Image for prop ref <?php echo "$prop_id" ?>'></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<?php
}
    if($page != 1)
    {
        $pageprev = $page - 1;
        // Fancy way of subtracting 1 from $page
        echo("<a href='prop_search2.php?page=$pageprev'>prev</a> ");
    }else echo("prev ");

$numofpages = ceil($total_rows / $limit);

for ($i = 1; $i <= $numofpages; $i++)
      {
        if($i == $page)
        {
            echo($i." ");
        }
        else
        {
            echo("<a href=\"$PHP_SELF?page=2\">$i</a> ");
        }
      }

    if($i == $numofpages)
    {
        $pagenext = $page + 1;
        echo("<a href='prop_search2.php?page=$pagenext'>next</a>");
    }
    else echo("next");
echo "<BR>No. of pages required: $numofpages<BR>";

mailing_list();
exit();
}

?>

Does anyone have any ideas on why this is behaving the way it is?

The switch statement in the php page is as follows:

          <?php
             global $page, $i;
          switch ($action)
          {
          case "s_results":
          s_results();
          break;
          default:
          s_blurb();
          break;
          }
          ?>


Thanks in advance


Jamal

 
Old December 17th, 2003, 04:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, your pagination links don't reset $action. Your switch statement, therefore, executes the default case.

You'll need to add "&action=s_results" to all your pagination links to persist it's value (unless you want to store it in a session).


Take care,

Nik
http://www.bigaction.org/
 
Old December 18th, 2003, 08:22 AM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jams30
Default

Quote:
quote:Originally posted by nikolai
 Well, your pagination links don't reset $action. Your switch statement, therefore, executes the default case.

You'll need to add "&action=s_results" to all your pagination links to persist it's value (unless you want to store it in a session).


Take care,

Nik
http://www.bigaction.org/
Hi Nik,

Thanks for the pointer, I tried adding "&action=s_results" as you suggested, but I now get the error as follows:


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\prop_search.inc on line 127

and:

There was a problem with the database query. MySQL said: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND price <=' at line 1

I did try to work out what was going wrong according to these errors but am still unsure, any further help would be appreciated.

Thanks again

Jamal

 
Old December 18th, 2003, 04:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What is your query? Can you echo the value of your query to see where the syntax is getting messed up?


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pagination with checkboxes argan Pro JSP 2 November 28th, 2008 12:48 AM
Pagination problem nanetsu Other Programming Languages 1 June 12th, 2008 06:12 AM
pagination Regornil Pro JSP 2 May 31st, 2005 06:10 AM
Please.....Pagination junemo PHP Databases 2 September 25th, 2004 01:14 PM
pagination junemo Oracle 0 July 1st, 2004 04:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.