/    Sign up×
Community /Pin to ProfileBookmark

Searching and displaying results in my database

I have posted about this a couple times before. I was receiving some help but the user helping me has not come back in almost a month.

I am trying to set up a search on my website that looks through my database and displays the results (very simplified search engine). I have a table with about 500 entries.

[QUOTE]

<?
include “DBConnect.inc.php”;

//connect to server and database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

//specify how many results to display per page
$limit = 10;

// Get the search variable from URL
$var = @$_GET[’10’] ;
//trim whitespace from the stored variable
$trimmed = trim($var);
//separate key-phrases into keywords
$trimmed_array = explode(” “,$trimmed);

// check for an empty string and display a message.
if ($trimmed == “”) {
$resultmsg = “<p>Search Error</p><p>Please enter a search…</p>” ;
}
[B]
[COLOR=”DarkRed”]// check for a search parameter
if (!isset($var)){
$resultmsg = “<p>Search Error</p><p>We don’t seem to have a search parameter! </p>” ;[/COLOR]
[/B]

}

[/QUOTE]

This is only part of the code but this is where it seems the function stops working as I am getting the error “We dont seem to have a search parameter.

I am fairly new to this and I would really appreciate anyones help.

Thanks!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleDec 03.2007 — Are you passing http://mydomain.com/page.php?10=whatever in the query string?

For example, var would have a value of "foo" in the URL below.

http://mydomain.com/page.php?10=foo

Since var is equal to $_GET['10']. I am not sure how well this works with numbers in your query string. Have you tried it with anything else?
Copy linkTweet thisAlerts:
@sac8513authorDec 03.2007 — No. I just checked and it does not look like it is getting passed through. How would i accomplish that?

as far the Since var is equal to $_GET['10'] part..the help i have been receiving from a past user led me to this.

If there is a better way I am totally open to that...
Copy linkTweet thisAlerts:
@jasonahouleDec 03.2007 — It depends on how you are passing it to the next page. It could be a link, it could be a forwarded URL...you are the only one that can tell that.
Copy linkTweet thisAlerts:
@sac8513authorDec 03.2007 — This is the entire code I am working with. Maybe this can help to determine...

?

include "DBConnect.inc.php";

//connect to server and database

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

//specify how many results to display per page

$limit = 10;

// Get the search variable from URL

$var = @$_GET['10'] ;

//trim whitespace from the stored variable

$trimmed = trim($var);

//separate key-phrases into keywords

$trimmed_array = explode(" ",$trimmed);

// check for an empty string and display a message.

if ($trimmed == "") {

$resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ;

}

// check for a search parameter

if (!isset($var)){

$resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;

}

// Build SQL Query for each keyword entered

foreach ($trimmed_array as $trimm){

// EDIT HERE and specify your table and field names for the SQL query

$query = "SELECT * FROM store WHERE store_name LIKE "%$trimm%" ORDER BY store_name DESC" ;

// Execute the query to get number of rows that contain search kewords

$numresults=mysql_query ($query);

$row_num_links_main =mysql_num_rows ($numresults);

// next determine if 's' has been passed to script, if not use 0.
// 's' is a variable that gets set as we navigate the search result pages.
if (empty($s)) {
$s=0;
}

// now let's get results.
$query .= " LIMIT $s,$limit" ;
$numresults = mysql_query ($query) or die ( "Couldn't execute query" );
$row= mysql_fetch_array ($numresults);

//store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
do{

//EDIT HERE and specify your field name that is primary key

$adid_array[] = $row[ 'store_name' ];

}while( $row= mysql_fetch_array($numresults));

} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){

$resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ;

}

//delete duplicate record id's from the array. To do this we will use array_unique function

$tmparr = array_unique($adid_array);

$i=0;

foreach ($tmparr as $v) {

$newarr[$i] = $v;

$i++;

}

// now you can display the results returned. But first we will display the search form on the top of the page

?>

<form action="test.php" method="get" name="search">

<div align="center">

<input name="q" type="text" value=" <?php echo $q; ?> " size="15">

<input name="search" type="submit" value="Search">

</div>

</form>

<?php

// display what the person searched for.

if( isset ($resultmsg)){

echo $resultmsg;

exit();

}else{

echo "Search results for: " . $var;

}

foreach($newarr as $value){

// EDIT HERE and specify your table and field names for the SQL query

$query_value = "SELECT * FROM store WHERE store_name = '$value'";

$num_value=mysql_query ($query_value);

$row_linkcat= mysql_fetch_array ($num_value);

$row_num_links= mysql_num_rows ($num_value);

//now let's make the keywods bold. To do that we will use preg_replace function.

//EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]

//This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line.

$titlehigh = preg_replace ( "'($var)'si" , "<b>1</b>" , $row_linkcat[ 'store_name' ] );

foreach($trimmed_array as $trimm){

if($trimm != 'b' ){

//IF you added more fields to search make sure to add them below as well.

$titlehigh = preg_replace( "'($trimm)'si" , "<b>1</b>" , $titlehigh);

}

//end highlight

?>

<p>

<?php echo $titlehigh; ?><br>

</p>

<?php

} //end foreach $trimmed_array

if($row_num_links_main > $limit){

// next we need to do the links to other search result pages

if ($s>=1) { // do not display previous link if 's' is '0'

$prevs=($s-$limit);

echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";

}

// check to see if last page

$slimit =$s+$limit;

if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {

// not last page so display next link

$n=$s+$limit;

echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";

}

}

} //end foreach $newarr

?>[/QUOTE]
Copy linkTweet thisAlerts:
@sac8513authorDec 09.2007 — can anyone help?
Copy linkTweet thisAlerts:
@holidayDec 10.2007 — [code=html]<input name="q" type="text" value=" <?php echo $q; ?> " size="15">[/code] text field is named "q", but your using [code=php]$var = @$_GET['10'];[/code] shouldn't it be [code=php]$var = @$_GET['q'];[/code] or am I not understanding something?
Copy linkTweet thisAlerts:
@sac8513authorDec 10.2007 — ok so i had a very interesting result using your suggestion...

when i first changed the code and ran the search on my search page

(this is the code for the search page)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Untitled Document</title>

</head>

<body>


<form action="test.php" method="POST">

<input type="text" name="name"> <input type="submit" name="ok" value="submit"> <form>

</body>

</html>
[/QUOTE]


I got the same error

BUT as part of the code, despite whether a result or an error is found it displays what happens on a subsequent page that also has a form with the search function (similar to the original)

and when i searched using that box..IT DISPLAYED THE RESULT!!!!

so i think we are very close...just not sure why it did not work on the original form search page.

Any ideas??

Thank you so much for that..it does seem to make more sense the way you suggested
Copy linkTweet thisAlerts:
@holidayDec 10.2007 — I'm lost, do you still need help? If you still need help, it would make my life easier if you posted all the code. And stop quoting your code. Use the PHP, HTML or CODE tags instead. ?
Copy linkTweet thisAlerts:
@sac8513authorDec 11.2007 — OK I have been working this code and have finally gotten it to work. Thank you very much. This issue was with name consistency that was being passed through each form.

There is one thing that doesn't work properly though. If there is an entry into my database that is more than one word and i try to search it..the amount of results that show will be equal to the amount of words for that entry.

Basically the loop function does not stop running if there is more than one word in an entry.

Sorry if that was redundant.

Any ideas how to fix?

The entire code is up above as we have been using and the part that i think needs to be altered starts is here:

//store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.

do{

//EDIT HERE and specify your field name that is primary key

$adid_array[] = $row[ 'store_name' ];

}while( $row= mysql_fetch_array($numresults));

} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){

$resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ;

}

//delete duplicate record id's from the array. To do this we will use array_unique function

$tmparr = array_unique($adid_array);

$i=0;

foreach ($tmparr as $v) {

$newarr[$i] = $v;

$i++;

}[/QUOTE]



I know you said to not quote my own code but the whole code is quite large to keep reposting so im trying to make it easy for you to show you the portion that needs help and refer to the giant portion of code in post 5
×

Success!

Help @sac8513 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...