Click to See Complete Forum and Search --> : mysql table search
diamonds
06-14-2003, 04:41 PM
Can anyone lelp me make a (advanced) mysql table search?
I would like users to use a drop-down list to select a column to search, a search field to enter text to search, and, if possable, a "exact" or "containing" drop-down, to select with. I will enter my pass , username, server, and table to search when i get it, if you could make them 4 variables near the top ;)
oh, two more things,
if you can highlight the text thet the user searched for, and (2) supply the entire row to show the other ones.
Khalid Ali
06-14-2003, 08:33 PM
Looking for free consulting ????
Thats not nice..
diamonds
06-15-2003, 09:07 AM
how about it prints the entire table, and removes all rows that don't comply with the search?
Depending on what you are trying to do, you may want to look at this: http://www.mysql.com/doc/en/String_comparison_functions.html
Also, your query might look something like this:
$query = "SELECT * FROM tablename WHERE column_name LIKE '$search%'";
The % means to matche any number of characters. So, if $search equaled 'test', it would also return 'testing', 'Testing' (note case insensativity) or 'tester'. Now, if you want to match 'my test', you would do this:
$query = "SELECT * FROM tablename WHERE column_name LIKE '%$search%'";
diamonds
06-16-2003, 04:49 PM
I get you 100%.
But how can you make it into a complete script?
surely you cannot go
<?php echo "SELECT * FROM tablename WHERE column_name LIKE '$search%'"; ?>
and expect it to work? Right?
Originally posted by diamonds
But how can you make it into a complete script?As Khalid said, this isn't a site to look for free contract programming... If you have questions regarding the script, we'd be happy to help, but if you need an entire script, you may want to contact a programmer (myself included), or look around on the 'net for something that does what you need...
diamonds
06-16-2003, 05:10 PM
like where? Do you know some scripts on the web or others I can contact?
Try searching google... http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=PHP+search+mysql+database The few I looked at are more or less expounded versions of what I posted above.. I only gave the query...
As far as other PHP programmers, I'm the only one I know... ;) lol
diamonds
06-16-2003, 05:36 PM
I got close... but everything was coming up with errors...
Oh well... the computer is as dumb as the programmer!
I will see what I can come up with in the next hour...
anyone have any immediate ideas?
Post the code you are having trouble with and an explination of the problem.
diamonds
06-16-2003, 06:00 PM
This (http://www.designplace.org/scripts.php?page=1&c_id=25) is the code I want the most and what iv'e posted is slightly modified so the username and password is a variable is at the top:
<?php
$username = ""
$password = ""
$host = ""
// 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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect($host,$username,$password);
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from the_table where 1st_field like \"%$trimmed%\"
order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
Now part of the way down is a specify database *EDIT REQUIRED HERE* comment. What do I do here?
oh, I forgot this:
search.html:
<form name="form" action="search.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're usingyou just need to change "database" to be the name of your database...
diamonds
06-16-2003, 06:12 PM
duh...
<?php
$user = ""
$pass = ""
$host = ""
$database = ""
$table = ""
// 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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect($host,$user,$pass); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db($database) or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from the_table where 1st_field like \"%$trimmed%\"
order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
is there anything else thet needs editing? there is one more EDIT HERE comment...
Yep, in the next EDIT HERE area, you need to set the tablename, and the column name. Something like this:
$query = "SELECT * FROM yourtable WHERE yourcolumn LIKE \"%$trimmed%\" ORDER BY yourcolumn";
diamonds
06-16-2003, 06:30 PM
Like this?(I highlighted all new text)
<?php
$user = ""
$pass = ""
$host = ""
$database = ""
$table = ""
$col = trim($_GET['col'] ;); //trim whitespace from the stored
// 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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect($host,$user,$pass); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from $table where $col like \"%$trimmed%\" order by $col";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (! ( $s*$limit >=$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
Well, $col should be the name of a column in your mySQL database... Also, I'm assumbing you have values for these variables in your actual script?
$user = ""
$pass = ""
$host = ""
$database = ""
$table = ""
diamonds
06-16-2003, 06:35 PM
yah, sure
just look at it!
diamonds
06-16-2003, 07:26 PM
When I run the script, it returns "Parse error: parse error, unexpected T_VARIABLE in ******/search.php on line 3"
I stared it out for a reason :)
You forgot semicolons ( ; ):
<?php
$user = "yourusername";
$pass = "yourpassword";
$host = "yourhost";
$database = "yourdatabasename";
$table = "yourtablename";
diamonds
06-17-2003, 01:21 PM
for(i==44000;i<1;i--){echo "duh";}
Let me try it now...
diamonds
06-17-2003, 01:52 PM
works Great... Thanks!!!
It took me a few more errors, but it works now! thanks!
One more thing... how can I make it so it shows other cells in the same row it is in? If i want to create a two-column table, one for the link and one for the keywords, say, how could I do that?
here is the (current) code:
<?php
$user = ""; //your MySql username
$pass = ""; //
$host = "localhost"; //your host
$database = ""; // the database to search
$table = ""; // the table to search
$col = ""; //leave blank if you want your users to specify it.
if($col == ""){
$col = trim($_GET['col']); //trim whitespace from the stored
}else{}
// 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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
mysql_connect($host,$user,$pass);
mysql_select_db($database) or die("Unable to select database"); //select which database we're using
$query = "select * from $table where $col like \"%$trimmed%\" order by $col";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: \"" . $trimmed . "\" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: \"" . $var . "\"</p>";
// begin to show results set
echo "Results:<br>";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[$col];
echo "$count.) $title<br>" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a> ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
I think you are going to want to do something along these lines:
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[$col];
$other = $row["someothercolumn"];
echo "$count.) $title<br>" ;
echo "The other column is: $other <br>";
$count++ ;
}
diamonds
06-17-2003, 02:12 PM
Works perfictly, and no errors, even once :)
thanks so much for your help! I'll see into makining a more complex search myself(yah, right)
diamonds
06-17-2003, 02:32 PM
oh, P.S. heres the code, for anyone who wants it:
<?php
//MySQL options
$user = "username"; //your MySql username
$pass = "password"; //
$host = "localhost"; //your host
//Database options
$database = "database"; // the database to search
$table = "table"; // the table to search
$col = ""; //column to search. leave blank if you want your users to defint it themselves.
$col_two = "";//second column to display, it displays the cell in this colunm on the same row
//highlighting options
?>
<?php
if($col == ""){
$col = trim(@$_GET['col']); //trim whitespace from the stored
}else{}
// 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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
mysql_connect($host,$user,$pass);
mysql_select_db($database) or die("Unable to select database"); //select which database we're using
$query = "select * from $table where $col like \"%$trimmed%\" order by $col";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
echo "<title>search results for \"".$trimmed."\" in ".$col."</title>";
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: \"" . $trimmed . "\" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: \"" . $var . "\"</p>";
// begin to show results set
echo "Results:<br>";
$count = 1 + $s ;
echo "<table>";
echo "<tr><td>no.)</td><td>search</td><td>supporting</td></tr>\n" ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row[$col];
$two = $row[$col_two];
echo "<tr><td>$count.)</td><td> $title </td><td>$two</td></tr>\n" ;
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a> ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
Edit it to your need, everyone!
diamonds
06-17-2003, 02:57 PM
crash!
whenever it shows a "next 10" link, is shows an error about an undefined variable, "PHP_SELF" on line 126.
I think PHP_SELF is the page it is currently on?
What is happining?
Don't uses global variables such as $PHP_SELF, use this instead: $_SERVER["PHP_SELF"];
diamonds
06-17-2003, 06:07 PM
The problem is fixed!
This it the new code:
echo " <a href=\"" .$_SERVER["PHP_SELF"]. "?s=" .$news. "&q=" .$var. "\">Next ".$limit." >></a>";
I also replaced "10" with $limit, so if you have supply be "8" it still dosent say "10".
But there is another problem:
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in \search.php on line 53"
this looks like an MySQL error. what does it mean?
diamonds
06-18-2003, 01:23 PM
Grr... all I needed to do was add some other things to the URL... but still, when I click on the link, no change is made, even if "s = 10" in the URL. What is happining?
diamonds
06-18-2003, 07:05 PM
I did it myself!:D it turned out to be that this code to trun s in the URL to the $s variable was ommited.
Thanks so much, pyro!
Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form" action="search.php" method="get">
<select name="col" size="1">
<option>Search In</option>
<option value="1"<?php if(trim(@$_GET['col'])=="1"){echo " selected";}?>>1</option>
<option value="2"<?php if(trim(@$_GET['col'])=="2"){echo " selected";}?>>2</option>
<option value="3"<?php if(trim(@$_GET['col'])=="3"){echo " selected";}?>>3</option>
<option value="4"<?php if(trim(@$_GET['col'])=="4"){echo " selected";}?>>4</option>
<option value="5"<?php if(trim(@$_GET['col'])=="5"){echo " selected";}?>>5</option>
<option value="6"<?php if(trim(@$_GET['col'])=="6"){echo " selected";}?>>6</option>
<option value="7"<?php if(trim(@$_GET['col'])=="7"){echo " selected";}?>>7</option>
<option value="8"<?php if(trim(@$_GET['col'])=="8"){echo " selected";}?>>8</option>
<option value="9"<?php if(trim(@$_GET['col'])=="9"){echo " selected";}?>>9</option>
</select>
<select name="sort" size="1">
<option>Sort By</option>
<option value="1"<?php if(trim(@$_GET['sort'])=="1"){echo " selected";}?>>1</option>
<option value="2"<?php if(trim(@$_GET['sort'])=="2"){echo " selected";}?>>2</option>
<option value="3"<?php if(trim(@$_GET['sort'])=="3"){echo " selected";}?>>3</option>
<option value="4"<?php if(trim(@$_GET['sort'])=="4"){echo " selected";}?>>4</option>
<option value="5"<?php if(trim(@$_GET['sort'])=="5"){echo " selected";}?>>5</option>
<option value="6"<?php if(trim(@$_GET['sort'])=="6"){echo " selected";}?>>6</option>
<option value="7"<?php if(trim(@$_GET['sort'])=="7"){echo " selected";}?>>7</option>
<option value="8"<?php if(trim(@$_GET['sort'])=="8"){echo " selected";}?>>8</option>
<option value="9"<?php if(trim(@$_GET['sort'])=="9"){echo " selected";}?>>9</option>
</select>
<input name="q" type="text" value="<?php echo trim(@$_GET['q']) ?>" />
<input name="Submit" type="submit" value="Update" /></form><br>
<?php
//MySQL options
$user = ""; //your MySql username
$pass = ""; //
$host = ""; //your host
//Database options
$database = ""; // the database to search
$table = ""; // the table to search
$col = ""; //column to search. leave blank if you want your users to defint it themselves.
$col_two = "name";//second column to display, it displays the cell in this colunm on the same row
$sorted= "";//what to sort by. leave blank for user selection.
$limit=10; // how many at max to display on one page
/*
ALSO!!!
there is one more spot you need to update down further, you need to match it to your MySQL table
*/
?>
<?php
if($col == ""){
$col = trim(@$_GET['col']); //trim whitespace from the stored
}else{}
if($sorted == ""){
$sorted = trim(@$_GET['sort']); //trim whitespace from the stored
}else{}
// Get the search variable from URL
$var = @$_GET['q'];
$trimmed = trim($var); //trim whitespace from the stored variable
$s = trim(@$_GET['s']);
// rows to return
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
mysql_connect($host,$user,$pass);
mysql_select_db($database) or die("Unable to select database"); //select which database we're using
$query = "select * from $table where $col like \"%$trimmed%\" order by $sorted";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
echo "<title>search results for \"".$trimmed."\" in ".$col."</title>";
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: \"" . $trimmed . "\" returned no results</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: \"" . $var . "\"</p>";
// begin to show results set
echo "Results:<br>";
$count = 1 + $s ;
/* This its the header, be sure you update this to match your columns in your MySQL table */
echo "<table border=\"1\">";
echo "<tr>
<td>no.</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>\n" ;
/* Be sure you update this to match your columns in your MySQL table */
while ($row= mysql_fetch_array($result)) {
$col_1 = $row['1'];
$col_2 = $row['2'];
$col_3 = $row['3'];
$col_4 = $row['4'];
$col_5 = $row['5'];
$col_6 = $row['6'];
$col_7 = $row['7'];
$col_8 = $row['8'];
$col_9 = $row['9'];
echo "<tr>";
echo "<td> $count.</td>";
echo "<td> $col_1 </td>";
echo "<td> $col_2 </td>";
echo "<td> $col_3 </td>";
echo "<td> $col_4 </td>";
echo "<td> $col_5 </td>";
echo "<td> $col_6 </td>";
echo "<td> $col_7 </td>";
echo "<td> $col_8 </td>";
echo "<td> $col_9 </td>";
/* End edit */
echo "</tr>\n";
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br>";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"".$_SERVER["PHP_SELF"]."?s=".$prevs. "&q=" .$var. "&col=" .$col."&sort=" .$sorted."\"><< Prev ".$limit."</a> ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (! ((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo "<a href='" .$_SERVER["PHP_SELF"]. "?s=" .$news. "&q=" .$var. "&col=" .$col."&sort=" .$sorted."'>Next ".$limit." >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
</body>
</html>
Glad it's all working for you... :)
avnikl
05-03-2007, 02:30 PM
i got the same code from another website.
And whenever i run it it keeps giving me an error. here is the error:
Parse error: parse error, unexpected T_VARIABLE in /home/content/html/search.php on line 9
code on my line 9 is $limit=10;
I am putting my form as well as the code i have. someone please give me some suggestions
<form action="search.php" method="post" class="form" id="form1">
<input name="q" type="text" class="search" />
<input name="Submit" type="submit" class="button" value="Search" />
and here is the php
<?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;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database
mysql_connect("localhost","user","pass"); //(host, username, password)
//specify database
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from articles where articlename like \"%$trimmed%\"
order by articlename"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"" .$_SERVER["PHP_SELF"]. "?s=" .$news. "&q=" .$var. "\">Next ".$limit." >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
avnikl
05-03-2007, 03:48 PM
actually i made a mistake. I forgot to put a semi colon on the top. sorry :D Problem solved