Losing Query In Search

They have: 5 posts

Joined: May 2006

Hi ya all, I have created a page with a drop down menu that has categories from my database. Once I select and submit a category it opens another file called search that displays all the results from the database. On the search page I'm using page numbering so it will only show 5 results per page, thats cool and works fine but when I click on page two or three it seems to lose the query and I end up with a blank page. Is there something that I can add into the search page to keep the query going.Confused

Thank you

He has: 1,380 posts

Joined: Feb 2002

Can you give us some code?

They have: 5 posts

Joined: May 2006

Yeah sure,here is the search file that seems to lose the query as soon as I goto the second page and so on.

<?
$category = $_POST['category'];
include 'db6.php';
$rowsPerPage = 5;
$pageNum = 1;

if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

$offset = ($pageNum - 1) * $rowsPerPage;

$query = "SELECT * FROM hw73ksu349x WHERE category='$category' ORDER BY pr DESC LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
$e=0;
while($row = mysql_fetch_array($result))
{
if ($e == "0"){ $color="#C1F2FF"; $e=1; }else{
$color="#93EAFF"; $e=0; }
echo "

".$row['title']."

".$row['description']."

";
}

$query = "SELECT COUNT(title) AS numrows FROM hw73ksu349x WHERE category='$category'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page ";
}
else
{
$nav .= " $page ";
}
}

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " [Prev] ";

$first = " [First Page] ";
}
else
{
$prev = ' ';
$first = ' ';
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " [Next] ";

$last = " [Last Page] ";
}
else
{
$next = ' ';
$last = ' ';
}

echo $first . $prev . $nav . $next . $last;

include 'db6.php';
?>

One thing I forgot in my first post is that I have only just started to learn php/mysql, so I would be very greatful if you can help me.
Cheers.

Busy's picture

He has: 6,151 posts

Joined: May 2001

You'll have to pass the values over to the other pages either in the url (page.php?a=this&b=that ...) or by hidden form values () or by session or cookie - pick your poison Wink

He has: 1,380 posts

Joined: Feb 2002

My thoughts exactly. I would recommend for a relatively small amount of info, putting it in page.php?a=heave&b=hell

Just my preference

They have: 5 posts

Joined: May 2006

Erm, I seem to be lost. Everything I keep trying dont seem to work. Confused

He has: 1,380 posts

Joined: Feb 2002

Can you give us some code for your attempts?

They have: 5 posts

Joined: May 2006

Well no not really, every time I have tried it I just get errors. I dont really understand what you mean by: page.php?a=heave&b=hell

He has: 1,380 posts

Joined: Feb 2002

Oh. Well, if you have a page, we'll call it "index.php", and want to pass information to it, you can have the user go to "index.php?variable=value".

An example:

<a href="/index.php?page=4">News</a>
'

Now, to access that information, you need to include a parser on the same page (because you're redirecting from index.php to index.php, but changing the variable's value). Something like

<?php
$page_id
= strip_tags($_GET['page']);
?>

With that information, you can then build a website all on one page, or multiple options per page:

<?php
if ($page == 1) {
  
// Main Page
} elseif ($page == 2) {
  
// Music Page
}
?>
and so on.

Does this make a little more sense now? If you try and implement this, give us the code you use and the errors it returns.

They have: 5 posts

Joined: May 2006

I think I need to start learning php/mysql again...lol. Then again it might be easy to pay someone to fix this script for me I guess.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.