Jump to content


Photo

MySQL Queryies on Multiple pages


  • Please log in to reply
3 replies to this topic

#1 LeeRoy8888

LeeRoy8888
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 04 March 2006 - 11:29 PM

Hi all,

I'm hoping someone can help me..... I'm struggling to write a fairly basic database search engine, I have most of it sorted out but i'm getting a few problems.

I ahev users defining a variable that is passed to another PHP file, the results.php. In this I have the following code.

$company=$_POST['company'];

This seems to work fine. However I have justed added some code to enable the results to be split onto multiple pages.

Sample of the code below....
<?
$db_addr = 'localhost';        // address of MySQL server.
$db_user = 'thewight_admin';        // Username to access server.
$db_pass = 'password';        // Password access server.
$db_name = 'thewight_database';    // Name of database to connect to.
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");

if (!($connect)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}

if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $db_name database.</p>");
exit();
}

if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = mysql_query("SELECT * FROM contacts WHERE company LIKE '%". $query ."%'"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>

<?
//Go back into PHP mode.

// Now we can display results.
$results = mysql_query("SELECT * FROM contacts WHERE company LIKE '%". $query ."%' ORDER BY company ASC LIMIT $page, $limit");
while ($data = mysql_fetch_array($results))
{
?>
<p><?=$data["company"]?>  <?=$data["address1"]?> <?=$data["phone"]?></p>
<?
}
?>
<p align="center">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b>$i</b> \n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");}
?>
</p>


The problem is when I click the next page link, the code rerun's and the "$company=$_POST['company'];" doesn't work as the POST isn't avalaible from the previous phpfile.

Is there a way of making the $company variable remain?

Does this make any sense to anyone.... I'm a very new at PHP a need some help, PLEASE!!

Thanks in Advance.
Lee



#2 Hooker

Hooker
  • Members
  • PipPipPip
  • Advanced Member
  • 193 posts
  • LocationWales, UK

Posted 05 March 2006 - 07:08 AM

i suggest looking at: [a href=\"http://www.phpfreaks.com/tutorials/73/0.php\" target=\"_blank\"]http://www.phpfreaks.com/tutorials/73/0.php[/a]

#3 LeeRoy8888

LeeRoy8888
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 05 March 2006 - 01:37 PM

Hi,

Thanks for the fast reply. I have looked at the tutorial and the code is very similar to mine.

But, after reading the replies to the tutorial people are still having problems when using a variable that has been set by a POST from another file.

They all seem to get the problem when the next page is clicked the variable then isn't set and then all the records are shown.

Does anyone know how I can get around this problem?


Thanks Again in Advance!!

Lee

#4 LeeRoy8888

LeeRoy8888
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 07 March 2006 - 10:14 AM


Any ideas anyone?? Please!!

Thanks Again
Lee






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users