tags:

views:

416

answers:

4

Hello,

I'm trying to use urlencode to convert the string: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>">

Actually, i want to implement a search engine.

|-www
 |- index.php
 |- search directory
  |- search.php
  |- header.html
  |- search_form.html
  |- search_result.html
  |- footer.html

search.php includes header.html,search_form.html,search_result.html etc.

I access search.php using: localhost/index.php/?page=search

search_form.html include button to search. And it call search.php using: <form action="index.php/?page=search" method="get">. i'm not sure if it's right.

After submitting the search request, search.php calls search_result.html to show result. The code in search_result.html: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>"><?php print $search_results['did_you_mean_b']; ?>

It seems should work, but after i click the search button, the result url is index.php/?query=&search=1. And i think it should be index.php/?page=search/?query=&search=1.

So, i try to use urlencode to solve it. And i don't know if the idea is right.

Thanks a lot for your help.

+6  A: 
$url = 'search.php?' . http_build_query(array(
    'query'  => $search_results['did_you_mean'],
    'search' => 1
));

That's the most simple way to go - please see http_build_query().

I don't know what your functions quote_replace() and addmarks() do but when you run urlencode("search.php?query=") this will also encode the ? and the = and will result in search.php%3Fquery%3D (same for urlencode("&search=1") which encodes the & and the = and will result in %26search%3D1) which in total will make the URL unusable.

Stefan Gehrig
+1 I had forgotten about this function, it is incredibly useful!
Ben James
@Stefan, you figured how to use http_build_query(). Thanks
garcon1986
@Stefan, I just want to treat the `?`. But i'm totally lost in doing that. Can you help me?
garcon1986
This function would have came in handy years ago when I still did this sort of thing :\
Mark
@Mark, do you have any idea about that?
garcon1986
+1  A: 

urlencode is used like this:

$url = 'http://example.com/page?foo='.urlencode($foo).'&amp;bar='.urlencode($bar);
Bart van Heukelom
@Bart van Heukelom, Thanks, your example is good.
garcon1986
@Bart van Heukelom, but how to handle the question mark?
garcon1986
I don't understand what you mean. Handle is a very generic word. The question mark that seperates the resource name from the query string needs no special treatment.
Bart van Heukelom
A: 

I still have the problem. Could anyone help me? Thanks so much.

garcon1986
This should be edited into your original question, or at least posted as a comment, not an answer :)
Ben James
@Ben, I'm sorry.
garcon1986
I just want to get all of your attention
garcon1986
+1  A: 

Firstly, you should probably not call that via GET. Secondly, a typical way to provide a search engine is something like this;

search.php               - provides the search button, input fields, etc.
search_result.php        - performes some magic to get results depending on your search

So, in search.php you make your search form, the button, and whatever else, pointing to search_result.php. The values of input are submitted automatically if you define the form as GET.

IMO you should overthink your file-structure. It doesn't make much sense to have an own folder for the search if you implement the files in it in your root file.

Try something more like this:

/
 index.php
 search.php
 search_result.php

Furthermore, (and generally for PHP/HTML), it's easier to understand and better writing style to write HTML in PHP, not PHP in HTML. Code like <htmltag attr="<?php echo $value; ?>" /> is very hard to read, maintain and causes the interpreter to consume much more time in escaping things than it would if you write echo '<html attr="'.$value.'" />';.

I hope you get the point. Your approach is okay, but far from effective and your problem is not so much caused by the code you use but by the way you wrote your whole code and especially the file-structure.

Coming back to my example, the (in my world^^) "closest to best" way for your simple searching is, as I said, two *.php-files in the same directory.

Your form points to action="search_result.php". In there, you make the search query and save some things to display to the user. The values of a form are automatically written to your .php-file depending on the name of your input field.

Say, you have an input type="text" name="search_query" and you fill it in with hello worldand press search - you will be redirected to search_result.php?search_query=hello%20world.

In search_result.php you can then access the query of your search via $_GET['search_query'] and do something with it.

For results, lets say that you didn't find anything or have suggestions to the user how to make his query better, you can write him a message and point him back to search.php to fill in his query again.

Wall of text, I know, but if you understand what I wrote and try do adapt it, you will have advanced in terms of PHP-scripting from 15/100 to 50/100 ;-)

ApoY2k
Apo Y2k, Thanks a lot for your answer. It's very good and genuine. I understand what you say. In fact, i think my code is far from perfect, but i'm trying to do it best, although i'm not an expert. You idea is to do the project "simply, efficiently and user-friendly". I'm trying and firstly i implement it, and then optimize it. Thanks for your time and good advice. I appreciate it.
garcon1986