Paul_Withers Posted September 28, 2014 Share Posted September 28, 2014 Hi, I have a script which searches the MySQL database. It outputs the result which is a business name, but I need the business name to be in a link so the user can click on it and go to another page to find out more information. However the business name is not included in the link, but the $value is. Here is my script <?php include 'init.php'; // normal search $result_tb = ""; if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) { $e = $_POST['search_value']; $query = 'SELECT aquaticCenterName FROM reviews WHERE ' . "aquaticCenterName LIKE '%$e%' OR " . "town LIKE '%$e%' OR " . "county LIKE '%$e%' OR " . "country LIKE '%$e%' OR " . "rating LIKE '%$e%' "; $query_result = $con->query($query); $result_tb = '<div>'; while ($rows = $query_result->fetch_assoc()) { foreach ($rows as $k => $v) { $result_tb .= '<a href="reviewResults.php?aquaticCenterName= . $v . ">' . $v . '</a><br>'; } } $result_tb .='</div><hr>'; $con->close(); } ?> <?php include 'includes/overall/header.php'; include 'includes/logo.php'; ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td> <input type="text" name="search_value" size="30" maxlength="30"/> </td> <td> <input type="submit" name="SEARCH" value="Search"/> </td> </tr> </table> </form> <?php echo $result_tb; ?> <?php include 'includes/overall/footer.php'; But when I click on the link, I get this in the address bar reviewResults.php?aquaticCenterName=%20.%20$v%20. How do I get this to read eg, reviewResults.php?aquaticCenterName=PaulsAquaticCenter Many Thanks aquaman Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 29, 2014 Author Share Posted September 29, 2014 I have tried changing it to $result_tb .= '<a href="reviewResults.php?aquaticCenterName='$v'">' . $v . '</a><br>'; but I get this error Parse error: syntax error, unexpected '$v' (T_VARIABLE) in /Applications/XAMPP/xamppfiles/htdocs/reviews.php on line 23 How do I get the value of $v into the link? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 29, 2014 Share Posted September 29, 2014 (edited) while ($rows = $query_result->fetch_assoc()) { $v = $rows['aquaticCenterName']; $result_tb .= "<a href='reviewResults.php?aquaticCenterName=$v>$v</a><br>"; } Edited September 29, 2014 by ginerjm Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 29, 2014 Author Share Posted September 29, 2014 Hi and thanks for your reply. I made the change as suggested, but now when I search, I do not get any results. Here is the code now <?php include 'init.php'; // normal search $result_tb = ""; if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) { $e = $_POST['search_value']; $query = 'SELECT aquaticCenterName FROM reviews WHERE ' . "aquaticCenterName LIKE '%$e%' OR " . "town LIKE '%$e%' OR " . "county LIKE '%$e%' OR " . "country LIKE '%$e%' OR " . "rating LIKE '%$e%' "; $query_result = $con->query($query); $result_tb = '<div>'; while ($rows = $query_result->fetch_assoc()) { $v = $rows['aquaticCenterName']; $result_tb .= "<a href='reviewResults.php?aquaticCenterName=$v>$v</a><br>"; } } $result_tb .='</div><hr>'; $con->close(); ?> <?php include 'includes/overall/header.php'; include 'includes/logo.php'; ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td> <input type="text" name="search_value" size="30" maxlength="30"/> </td> <td> <input type="submit" name="SEARCH" value="Search"/> </td> </tr> </table> </form> <?php echo $result_tb; ?> <?php include 'includes/overall/footer.php'; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 29, 2014 Share Posted September 29, 2014 1 - make sure you have php error checking on - see my signature 2 - echo out the complete query statement so you/we can see the actual query you have built. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 29, 2014 Share Posted September 29, 2014 $query = "SELECT aquaticCenterName FROM reviews WHERE aquaticCenterName LIKE '%".$e."%' OR town LIKE '%".$e."%' OR county LIKE '%".$e."%' OR country LIKE '%".$e."%' OR rating LIKE '%".$e."%'"; Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 29, 2014 Share Posted September 29, 2014 the href='...' attribute in the link is missing the closing ', which is probably breaking the rest of the html on the page. use the following inside the loop - $u = $rows['aquaticCenterName']; $v = urlencode($u); // since this is a text name, make it url encoded $result_tb .= "<a href='reviewResults.php?aquaticCenterName=$v'>$u</a><br>"; i would use the id, not the name, as the value being put into the link - reviewResults.php?id=123and you can simplify the multiple col LIKE '%$e%' OR ... term in the query to be - $query = "SELECT aquaticCenterName FROM reviews WHERE CONCAT_WS(' ', aquaticCenterName, town, county, country, rating) LIKE '%$e%'"; Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 29, 2014 Author Share Posted September 29, 2014 Hi, thanks for your reply. Error checking is on and no errors are reported. Here is the echoed query SELECT aquaticCenterName FROM reviews WHERE aquaticCenterName LIKE '%Chelmsford%' OR town LIKE '%Chelmsford%' OR county LIKE '%Chelmsford%' OR country LIKE '%Chelmsford%' OR rating LIKE '%Chelmsford%' And here is the echoed $v Lissies Aquatics This is the result of the query. I just cant get the value of $v to work in a link Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 29, 2014 Author Share Posted September 29, 2014 Hi mac_gyver, thanks for your reply. That all works great, apart from when the resulting aquatic center contains a + in between the words eg. http://localhost/reviewResults.php?aquaticCenterName=Lissies+Aquatics But I wont be able to retrieve information from the database with the + being there. Is it possible to remove this? Many Thanks aquaman Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 29, 2014 Share Posted September 29, 2014 did you actually try? the + in the link is the encoded space (because space characters are not valid in links). the + will be automatically converted by php (it does a urldecode on $_GET data) to a space and using the value in a database query will work. you could always do what i suggested, which will eliminate the problem of trying to pass a space in the link - i would use the id, not the name, as the value being put into the link - reviewResults.php?id=123 Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 30, 2014 Author Share Posted September 30, 2014 Hi mac_gyver, yes that did work, thank you Now I have another search to search for items for sale. I have this working, with the $user_id being the result. But I don't want this to be displayed in the results, I would like the $aquaticCenterName to be displayed instead. ($user_id is going to be changed to id as it has nothing to do with the user). Here is what I got so far <?php include 'init.php'; // normal search $result_tb = ""; if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) { $e = $_POST['search_value']; $query = "SELECT user_id FROM live_sales WHERE CONCAT_WS(' ', user_id, listing_title, fishtype, speciesCommon, speciesScientific, town, county) LIKE '%$e%'"; $query_result = $con->query($query); $result_tb = '<div><h2>Search Results:</h2>'; while ($rows = $query_result->fetch_assoc()) { foreach ($rows as $k => $v) { $u = $rows['user_id']; $v = urlencode($u); // since this is a text name, make it url encoded $result_tb .= "<a href='searchResultslive.php?user_id=$v'>$u</a><br>"; } } $result_tb .='</div>'; $con->close(); } ?> <?php include 'includes/overall/header.php'; include 'includes/logo.php'; ?> <h1>Search Species for Sale</h1> <p>Search for the aquatic species of your choice.</p> <p> You can search for your item via species type, name, scientific name, town or county.</p> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td> <input type="text" name="search_value" size="40" maxlength="40" value="Search for species"/> </td> <td> <input type="submit" name="SEARCH" value="Search"/> </td> </tr> </table> </form> <?php echo $result_tb; ?> <?php include 'includes/overall/footer.php'; So ideally the results would show the Aquatic Center Name, but the link would be results.php?user_id=2 ? Many Thanks aquaman Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2014 Share Posted September 30, 2014 Then you will have to select the name as well as the id in your query Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 30, 2014 Author Share Posted September 30, 2014 Hi Barand, Have tried this $query = "SELECT user_id, listing_title FROM live_sales WHERE CONCAT_WS(' ', user_id, listing_title, fishtype, speciesCommon, speciesScientific, town, county) LIKE '%$e%'"; But this results in the same result being repeated twice Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2014 Share Posted September 30, 2014 Why do you loop through each field in each returned row? Lose the foreach() while ($rows = $query_result->fetch_assoc()) { foreach ($rows as $k => $v) { $v = $rows['user_id']; $u = $rows['listing_title']; $v = urlencode($v); // since this is a text name, make it url encoded $result_tb .= "<a href='searchResultslive.php?user_id=$v'>$u</a><br>"; } } Quote Link to comment Share on other sites More sharing options...
Paul_Withers Posted September 30, 2014 Author Share Posted September 30, 2014 Hi Barand, thanks for your answer, it works great now. Quote Link to comment Share on other sites More sharing options...
pspmarketingindia Posted April 18, 2015 Share Posted April 18, 2015 hi friends, is it possible to make a simple search engine in php by using our own database? example:if user types "doctors in austrailia" then it need to show the result of doctor name, other details in austrailia. if any code is available then share with us. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 18, 2015 Share Posted April 18, 2015 Don't hijack other peoples posts. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.