hey there, what I want to do is transform search results into hyperlinks.

This part isn't really the hard part I don't think. But what I would like it have a generic details.php page which would bring up everything associated with the url you clicked.

For example, my database is a book catalogue. So you search for Harry Potter and it brings up every Harry Potter book in the database, now instead of statically creating a separate page for each book I want to have a generic php file so that when you click on Harry Potter and the Goblet of Fire the generic php script would show everything related to that title. The trouble I'm facing is how to go about setting the variable in the show details file.

I don't have any code yet because I've been trying to work the logistics of it on paper first.

I'm thinking something like

for(number of search results)

print http://localhost/details.php/q=$title

I'm just not sure how to go about setting the title so that it gets passed over to the details script when you click the link. I might have to just dive in and see what happens.

Recommended Answers

All 11 Replies

Hi,

You can do your search something like this.

when user search for 'Harry Potter' in your generic php file you can fire a query with LIKE keyword on title filed or if you want exact result then you can use equalto (=) in your query. so your url will look like this when you send request to your generic php file

http://localhost/details.php?title=$title

by using GET method you can get the search keyword and then on the same file or on another file you can show the all of the records related to your search keyword.

Hope I explain you which you want. :)

HI, sorry it took so long to get back to this, but I've finally come back to this section of my code. While I understand your answer in theory - as its pretty much what I was thinking as well - I'm finding implementation to be harder than I expected. Here's my search.php as I have it so far

<?php
	include 'C:\wamp\www\Catalogue\php\topbar.php';
	@mysql_connect($_SESSION['hostname'],$_SESSION['username'],$_SESSION['password']) or die(@mysql_error);
	@mysql_select_db($_SESSION['db']);
	$keyword = $_POST['searchbar'];
	$query = "SELECT * FROM books WHERE title LIKE '%$keyword%'";	
	$result=@mysql_query($query) or die(@mysql_error);
	$number=mysql_numrows($result);
	if($number==0)
		print("<center>No match</center>");
	else if($number > 0){
		print("<center>Results<br/><br/>");
		print("<table border = \"2\"");
	print("<tr>
			<td>title</td>
		</tr>");
		$i=0;
	while($i<$number){
			$title = mysql_result($result, $i, "title");
			print("<tr>
					<td>$title</a></td>
				</tr>");
			$i++;
			}
	print("</table></center>");
	}
	mysql_close();

?>

What I need is the $title in the printed table to be a URL but I'm sure how best to create the URL. I would then need $title past to the php script of the created URL. The thing that's bugging me the most is I think we did sometime similar in class and I can't find the example. It's like knowing the answer but not quite being able to get at it!

your line 21 could be something like <td><a href='$title'>$title</a></td> assuming there is a field somewhere in the result array that includes a url if the title is not the url <td><a href='$result['url']'>$title</a></td>

you question have two part one is search the term and 2nd add search term in url.

am i right

two parts yes, part one being turning the search results into a url to a details.php script and then having that script get the $title value depending on which link is clicked.

For example I search Harry Potter and I get three results:

Harry Potter and the Philosopher's Stone
Harry Potter and the Goblet of Fire
Harry Potter and the Deathly Hallows

and I want to turn each of those to link to details.php and have the details returned based on the $title clicked.

As much I got your problem I think you want something like this.

in your result title you want to put a link which gose to details.php. is it something like that ?

if yes then you can do your code like below.

<td><a href='details.php?id=<?php echo $id; ?>'><?php echo $title; ?></a></td>

your link will go to details.php and by passing the id you can get the unique book details.

I hope this will help to overcome your problem. :)

yeah something like details.php?q=$title where when you click it becomes detail.php?q=Harry Potter and the Goblet of Fire

and then details.php gets q from the URL

ok. I got you.

in the link you just write down id as i given above and for your requirement of book name in url you have to use .htaccess for that which fetch the book name related to book id.

and you are done. :)

pb's answer above, as the OP code print("<tr><td><a href='details.php?id=urlencode($id)'>$title</a></td></tr>"); because human readable text contains characters that mess up GET

Thanks guys, I believe I have it. I have yet to deal with the special characters that show up in book title like ' and so forth. That'll be a tweaking step for later I think

I think dyingatmidnight have the id as a number (unique number of the table) so we dont need urlencode() for the id field.

and for the url contains characters we can use .htaccess from which we can sperate the characters by - (dash) or _ (underscore).

this is only possible in the condition if id is having integer value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.