Jump to content

Recommended Posts

Hi,

 

I have a searchbar which looks for a value in my tables, it works great but i want to know how i can make the results of the search clickable; go to a page relevent to that search, let me explain in more depth.

 

User searches for a postcode, script finds and displays the postcode on the next page, user clicks the result postcode to goto the next page.

 

What im stuck with is how to take that result and automatically display it on the clicked "final" page?

 

Search script:

 

<?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("xxxxx","xxxx","xxxx"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("removalspacelogin") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "(SELECT postcode FROM freelistings
             WHERE postcode like '%$trimmed%')         
		UNION           
	  (SELECT postcode FROM basicpackage              
	  	 WHERE postcode like '%$trimmed%')          
		UNION           
	  (SELECT postcode FROM premiumuser              
	  	 WHERE postcode like '%$trimmed%')         
	  ORDER BY postcode"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("<b>Query:</b> $query<br><b>Error</b>: " . mysql_error());



// 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 for $q";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row['postcode'];

  echo " $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>&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 " <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>";
  
  
?>

<form method="post" action="localarea.php">
<a href="localarea.php"><?php echo """ .$var. ""</p>";?></a>
</form>
[\code]


The form at the bottom im guessing is where i should take the $var from but how exactly? or if not where should it be pulled from to display on the next page? 

I want this "next" page because ideally there would be lots of results in postcode, so the user can choose which one is more relevant to them and go to that specific page with all the companies within that postcode. Hope this helps for a better understanding of what im trying to achieve, its just implementing it in? 

Link to comment
https://forums.phpfreaks.com/topic/249039-taking-results-from-search-and-echo/
Share on other sites

I didn't bother reading your code, it's far too long.

 

You would do this the exact same way you did it here for your search (note, I only read the first 4 lines of your code, one of which is wrong)

 

- Make a PHP page that accepts $_GET['postcode']

- Have that page display the details for the post code

- When you print the search results, make the post code into a link that goes to theNewPage.php?postcode=$thePostCode

 

This is exactly what PHP is designed to do.  What is confusing you about this? 

Its wrong i'm not sure what's wrong on it as it works fine for the first part?

 

okay first of all thank you for getting back to me. Secondly, i'm confused on how to write the code, i know what i want it to do but when researching it's not sinking in?

 

Is it possible using my far too long script to get the results of the postcode using query

$query = "(SELECT postcode FROM freelistings
             WHERE postcode like '%$trimmed%')         
		UNION           
	  (SELECT postcode FROM basicpackage              
	  	 WHERE postcode like '%$trimmed%')          
		UNION           
	  (SELECT postcode FROM premiumuser              
	  	 WHERE postcode like '%$trimmed%')         
	  ORDER BY postcode";

 

Aswell as display the field results of another column next to it - for example: if "postcode" matches show "company_name" next to it

 

Postcode      Company Name

WS12 2SH - Daves Auto's

 

ive found this so far it pull's the postcode fine but getting the company name to show with the postcode is my problem.

 

Can i have multiple queries using SELECT company_name FROM? or should i use JOIN with just one query?

 

Many thanks for your help so far

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.