Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search query - Limit per page problem. 2

Status
Not open for further replies.
Joined
Jun 9, 2004
Messages
188
Location
US
Hello I am executing the following query via a post.
Code:
$query = "SELECT * FROM news WHERE ".$_POST["searchtype"] ." LIKE '%".$_POST["user_input"]."%' ORDER BY id DESC;";

I then do some calculations so that each page only show 5 results per page.

I created links such as 5|10|15| like this:
Code:
 <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> |

The problem I think I have is that when I click on results 10 php loses $_POST["user_input"] $_POST["searchtype"] from the previous page. At the bottom of the web browser status bar it shows the following when I hover the link 10.
Code:
[URL unfurl="true"]http://www.foo.org/results.php?query=SELECT[/URL] * from news WHERE story LIKE 'ub%' ORDER BY id DESC;&page0&limit=10

And when I click on it I get the following error...
"No results found matching your query - SELECT * from news WHERE LIKE '%%' ORDER BY id DESC;"

My question is what is 'ub%' and is my search string '%".$_POST["user_input"]."% being lost in limbo?

thanks for the advice or idea in advance. Thanks.
 
Make it easier on yourself...try something like what i use

Code:
$offset = 0;
$max_results = 5;

if (isset($_GET['page'])){$offset = $_GET['page'];}
if (isset($_POST['searchtype'])){$searchtype = $_POST['searchtype'];}
if (isset($_POST['ui'])){$ui= $_POST['ui'];}

$query = "select * from news where $searchtype LIKE '%$input' ORDER BY id DESC limit $offset, $max_results;";

//run query (omitted for berevity)

//show results (omitted for berevity)
echo "<input type=hidden name=searchtype value='$searchtype'>\n<input type=hidden name=ui value='$ui'>\n";

//show list of links
$query = "select count(*) as counter from news where $searchtype LIKE '%$input' ";
//run count query 
$result = mysql_query($query);
if ($result){
   $ttl_records = mysql_fetch_row($result);
}

$gross_results = ($ttl_records / $max_records); 
if (($ttl_records % $max_records)!=0){
  $gross_results++; //add on more page
}

for ($x = 1; $x < $gross_records; $x++){
  echo <a href=\"".$_SERVER['PHP_SELF']."?page=".($x * $max_results).">".($x * $max_results)."</a> | ";
}//end if


Bastien

Cat, the other other white meat
 
Values in $_POST and $_GET only exist in those scripts to which data has been submitted.

I your particular case, you're going to another script, and no POST-method data of any kind is being submitted. $_POST will correctly be blank.

If you need to carry that information around invisibly to the user, I recommend the use of session variables.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I'm not really sure on how to use session variables. Thanks for the advice I will read up on it.
 
Some general advice on session variables in PHP:

[ul][li]Issue session_start() in every script that will use session variables.[/li][li]Manipulate the elements of the array $_SESSION.[/li][ul][li]If you wish to instantiate a new session variable, simply set an element of $_SESSION:
$_SESSION[['foo'] = 3;[/li][li]To erase a session variable, use unset():
unset($_SESSION['foo']);[/li][/ul][li]Avoid the use of session_register()[/li][/ul]



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top