/    Sign up×
Community /Pin to ProfileBookmark

PHP search page

Hi, I admit I’m a novice at PHP, having a mild form of dyslexia doesn’t help, anyway…

I have borrowed some code for searching my database, which it does the problem is he paging doesn’t work, I have searched thru the code but I cant figure it out Im probably being really dumb. heres the code. Many thanks for any help

[code=html]
<form name=”form” action=”<? $PHP_SELF; ?>” method=”get”>
<input type=”text” name=”q” />
<input type=”submit” name=”Submit” value=”Search” />
</form>

<?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 ** EDIT REQUIRED HERE **
mysql_connect(“localhost”,”root”,”mysql”); //(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 name like “%$trimmed%” OR othername like “%$trimmed%”
order by 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: &quot;” . $trimmed . “&quot; 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: &quot;” . $var . “&quot;</p>”;

// begin to show results set
echo “<p>Results </p>”;
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$name = $row[“name”];
$othername = $row[“detail”];
$id = $row[“id”];

echo $count . $name . ” <a href=inddex.php?id=” .
$id . ” style=text-transform:capitalize; >” .
$name . ” – ” . $othername . “</a> <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 “&nbsp;<a href=”$PHP_SELF?s=$prevs&q=$var”>&lt;&lt;
Prev 10</a>&nbsp&nbsp;”;
}

// 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 “&nbsp;<a href=”$PHP_SELF?s=$news&q=$var”>Next 10 &gt;&gt;</a>”;
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo “<p>Showing results $b to $a of $numrows</p>”;

?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordOct 02.2008 — Hmm try this, and also post the errors that you get

[code=php]
<form name="form" action="<?php echo $PHP_SELF; ?>" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>


<?php

$page = $PHP_SELF;

// 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("localhost","root","mysql"); //(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 name like "&#37;$trimmed%" OR othername like "%$trimmed%"

order by 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: &quot;" . $trimmed . "&quot; 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: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "<p>Results </p>";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$name = $row["name"];
$othername = $row["detail"];
$id = $row["id"];

echo $count . $name . " <a href=index.php?id=" .
$id . " style=text-transform:capitalize; >" .
$name . " - " . $othername . "</a> <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 "&nbsp;<a href="$page?s=$prevs&q=$var">&lt;&lt;
Prev 10</a>&nbsp&nbsp;";
}

// 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 "&nbsp;<a href="$page?s=$news&q=$var">Next 10 &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) {
$a = $numrows ;
}

$b = $s + 1 ;

echo "<p>Showing results $b to $a of $numrows</p>";

?>
[/code]
Copy linkTweet thisAlerts:
@ariellOct 02.2008 — Hi there,

the code itself is not as error-prone as it looks like.

However, you should NOT query the database twice, you should NOT evaluate all those $_GET stuff thru 15 lines of code, or so, and you should get rid of that fatal error (division by zero) limit/s, and so on. I'm in the middle of a thing. If it is still not working next time I drop by, I'll send you the code.

later and best.
Copy linkTweet thisAlerts:
@tefnutauthorOct 03.2008 — Thanks guyz

When, i try Phils code it does the same as before, it doesnt show any error messages, when you click the next button that I could give you, it literally does nothing apart from refresh the page and display: [I]?s=10&q=e[/I] after the url in the address bar

Thanks Im very grateful and know Im a dunce :S
×

Success!

Help @tefnut spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...