Jump to content

php/sql search help


danx30

Recommended Posts

My search is working just perfect, except since there are well over 1500 entries they are all listed on the same page, how would I change this code so that it only displays 25 or 50 results on each page?  I know there are topics on this, and I have read them but with no luck.

 

<table border="0" width="100%">
<tr>
<td width="6%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Rank</font></small></td>
<td width="7%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Name</font></small></td>
<td width="11%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Amount Owed</font></small></td>
<td width="76%" bgcolor="#00FFFF"><p align="left"><small><font face="Verdana">IP Number</font></small>
</td>

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }


if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

mysql_connect("localhost","username","password"); //(host, username, password)
mysql_select_db("db") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * FROM users WHERE name LIKE \"%$trimmed%\"  
  order by name ASC"; // EDIT HERE and specify your table and field names for the SQL query

$result = mysql_query($query);
if(mysql_num_rows($result)) {
  $rank = 1;
  while($row = mysql_fetch_row($result))
  {
    print("</tr><tr>");
    if($color == "#D8DBFE") {
     $color = "#A6ACFD";
    } else {
      $color = "#D8DBFE";
    }
   print("<td width=\"6%\" bgcolor=\"$color\"><center><small>");
   print("<font face=\"Verdana\">$rank</font></small></center></td>");
   print("<td width=\"7%\" bgcolor=\"$color\"><center><small>");
   print("<font face=\"Verdana\"><strong>$row[0]</strong></font></small></center></td>");
   print("<td width=\"11%\" bgcolor=\"$color\">$row[2]<center><small>");
   print("<td width=\"76%\" bgcolor=\"$color\"><left><small>");
   print("<font face=\"Verdana\">$row[15]</font></small></left></td>");
  $rank++;
  }
}
?>

Link to comment
Share on other sites

I just did something like that here is what I used, it may or may not work but it is worth a shot:

<?php
$page = $_GET["page"];
             $row_max = 20; //or how many results you want per page

if(!isset($page)) {
 $page = 1;
}

 if($page == 1) {	 	
 	$pagein = 0;
 	$pageout = $row_max;
 }	

if($page > 1) { 
	$pagein = $page * $row_max - $row_max;
	$pageout = $pagein * $row_max;
}

$query = "SELECT * FROM tablename WHERE thing = '$thing'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$query2 = "SELECT * FROM tablename WHERE thing = '$thing' LIMIT $pagein, $pageout";
$result2 = mysql_query($query2);
while ($row = mysql_fetch_array($result2)) {

//show table results here

}
			$pagecount = ceil($num / $row_max);
			echo "Page ";
$x = 1;
$y = $pagecount;
for( $i = $x; $i <= $y; $i++ ) {
print "<a href='index.php?page=$i'><b>[$i]</b></a> ";
}
?>

 

I hope you get the idea

Link to comment
Share on other sites

I just did something like that here is what I used, it may or may not work but it is worth a shot:

<?php
$page = $_GET["page"];
             $row_max = 20; //or how many results you want per page

if(!isset($page)) {
 $page = 1;
}

 if($page == 1) {	 	
 	$pagein = 0;
 	$pageout = $row_max;
 }	

if($page > 1) { 
	$pagein = $page * $row_max - $row_max;
	$pageout = $row_max;// changed the limit out to $row_max sorry for the mistake
}

$query = "SELECT * FROM tablename WHERE thing = '$thing'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$query2 = "SELECT * FROM tablename WHERE thing = '$thing' LIMIT $pagein, $pageout";
$result2 = mysql_query($query2);
while ($row = mysql_fetch_array($result2)) {

//show table results here

}
			$pagecount = ceil($num / $row_max);
			echo "Page ";
$x = 1;
$y = $pagecount;
for( $i = $x; $i <= $y; $i++ ) {
print "<a href='index.php?page=$i'><b>[$i]</b></a> ";
}
?>

 

I hope you get the idea

Link to comment
Share on other sites

I tried to combine what you posted with my script and I get nothing but a blank page.  :(  I'm sure it's something simple I'm not seeing.

 

<table border="0" width="100%">
<tr>
<td width="6%" bgcolor="#00FFFF"><p align="center"><small><font 

face="Verdana">Rank</font></small></td>
<td width="7%" bgcolor="#00FFFF"><p align="center"><small><font 

face="Verdana">Name</font></small></td>
<td width="11%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Amount 

Owed</font></small></td>
<td width="76%" bgcolor="#00FFFF"><p align="left"><small><font face="Verdana">IP 

Number</font></small>
</td>

<?php
$page = $_GET["page"];

        $var = @$_GET['q'] ;
        $trimmed = trim($var);
                $row_max = 20; //or how many results you want per page

if(!isset($page)) {
 $page = 1;
}

 if($page == 1) {	 	
 	$pagein = 0;
 	$pageout = $row_max;
 }	

if($page > 1) { 
	$pagein = $page * $row_max - $row_max;
	$pageout = $pagein * $row_max;
}

mysql_connect("localhost","username","password");
mysql_select_db("db") or die("Unable to select database");
$query = "select * FROM users WHERE name LIKE \"%$trimmed%\"  
  order by name ASC";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$query2 = "SELECT * FROM users WHERE name LIKE \"%trimmed%\" LIMIT $pagein, $pageout";
$result2 = mysql_query($query2);
$rank = 1;
while ($row = mysql_fetch_array($result2)) {

    print("</tr><tr>");
    if($color == "#D8DBFE") {
     $color = "#A6ACFD";
    } else {
      $color = "#D8DBFE";
    }
   print("<td width=\"6%\" bgcolor=\"$color\"><center><small>");
   print("<font face=\"Verdana\">$rank</font></small></center></td>");
   print("<td width=\"7%\" bgcolor=\"$color\"><center><small>");
   print("<font face=\"Verdana\"><strong>$row[0]</strong></font></small></center></td>");
   print("<td width=\"11%\" bgcolor=\"$color\">$row[2]<center><small>");
   print("<td width=\"76%\" bgcolor=\"$color\"><left><small>");
   print("<font face=\"Verdana\">$row[15]</font></small></left></td>");
  $rank++;
  }



}
			$pagecount = ceil($num / $row_max);
			echo "Page ";
$x = 1;
$y = $pagecount;
for( $i = $x; $i <= $y; $i++ ) {
print "<a href='newsearch.php?page=$i'><b>[$i]</b></a> ";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.