genzedu777 Posted June 25, 2010 Share Posted June 25, 2010 Hi guys, I have tried using the implode() and explode() function which are highlighted in red, to search for any relevant words which the user have typed, For example, a user typed in Fighter, The code should be able to pull out 'Bull Fighter Matador', 'Fire Fighter' etc. However the coding doesn't work, when I typed in Fighter in www.dress-a-holic.com/search.html, nothing comes out in www.dress-a-holic.com/search.php Appreciate if anyone could assist me. Thank you Wilson <?php //Insert header $page_title = 'Search'; $page_header = 'Search Results'; require_once ('header.php'); // Grab the sort setting and search keywords from the URL using GET $sort = $_GET['sort']; $user_search = $_GET['usersearch']; // Start generating the table of results echo '<table border="0" cellpadding="2">'; // Generate the search result headings echo '<tr class="heading">'; echo '<td>Job Title</td><td>Description</td><td>State</td><td>Date Posted</td>'; echo '</tr>'; // Connect to the database require_once('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Query to get the results $search_query = "SELECT * FROM riskyjobs"; $where_list = array(); $user_search = $_GET['usersearch']; $search_words = explode (' ', $user_search); foreach ($search_words as $word) { $where_list[] = "description LIKE '%word%'"; } $where_clause = implode (' OR ', $where_list); if(!empty($where_clause)) { $search_query = "WHERE $where_clause"; } $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)) { echo '<tr class="results">'; echo '<td valign="top" width="20%">' . $row['title'] . '</td>'; echo '<td valign="top" width="50%">' . $row['description'] . '</td>'; echo '<td valign="top" width="10%">' . $row['state'] . '</td>'; echo '<td valign="top" width="20%">' . $row['date_posted'] . '</td>'; echo '</tr>'; } echo '</table>'; mysqli_close($dbc); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/ Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 $where_list[] = "description LIKE '%$word%'"; Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1077127 Share on other sites More sharing options...
genzedu777 Posted June 27, 2010 Author Share Posted June 27, 2010 Hi Nightslyr, Thanks for the advice. I have added the '$' sign, however it still doesn't work, when I try searching a general word, like fighter. When I conduct a search for 'bull', I believe my coding should pull out search result like 'Electric Bull Repairer', which can be found in my database. Do you have any possible suggestions of why my coding is not working? Thanks Wilson <?php //Insert header $page_title = 'Search'; $page_header = 'Search Results'; require_once ('header.php'); // Grab the sort setting and search keywords from the URL using GET $sort = $_GET['sort']; $user_search = $_GET['usersearch']; // Start generating the table of results echo '<table border="0" cellpadding="2">'; // Generate the search result headings echo '<tr class="heading">'; echo '<td>Job Title</td><td>Description</td><td>State</td><td>Date Posted</td>'; echo '</tr>'; // Connect to the database require_once('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Query to get the results $search_query = "SELECT * FROM riskyjobs"; $where_list = array(); $user_search = $_GET['usersearch']; $search_words = explode (' ', $user_search); foreach ($search_words as $word) { $where_list[] = "title LIKE '%$word%'"; } $where_clause = implode (' OR ', $where_list); if(!empty($where_clause)) { $search_query = "WHERE $where_clause"; } $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)) { echo '<tr class="results">'; echo '<td valign="top" width="20%">' . $row['title'] . '</td>'; echo '<td valign="top" width="50%">' . $row['description'] . '</td>'; echo '<td valign="top" width="10%">' . $row['state'] . '</td>'; echo '<td valign="top" width="20%">' . $row['date_posted'] . '</td>'; echo '</tr>'; } echo '</table>'; mysqli_close($dbc); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1077855 Share on other sites More sharing options...
KevinM1 Posted June 27, 2010 Share Posted June 27, 2010 Have you tested to see if $search_words is being set correctly? That you're successfully retrieving the $_GET data? Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1077925 Share on other sites More sharing options...
genzedu777 Posted June 30, 2010 Author Share Posted June 30, 2010 Hi Nightslyr, Yes I did. This is my search.HTML code before executing search.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Risky Jobs - Search</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <img src="riskyjobs_title.gif" alt="Risky Jobs" /> <img src="riskyjobs_fireman.jpg" alt="Risky Jobs" style="float:right" /> <h3>Risky Jobs - Search</h3> <form method="get" action="search.php"> <label for="usersearch">Find your risky job:</label><br /> <input type="text" id="usersearch" name="usersearch" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Are there any other possibilities? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1079122 Share on other sites More sharing options...
genzedu777 Posted June 30, 2010 Author Share Posted June 30, 2010 Hi Nightslyr, Thank you so much for your help, I have found my error. I have all along been using $result = mysqli_query($dbc, $query); where my '$query' should be written as $search_query. Appreciate your time in looking through my code. Thank you $search_query = "SELECT * FROM riskyjobs"; // Extract the search keywords into an array $clean_search = str_replace(',', ' ', $user_search); $search_words = explode(' ', $clean_search); $final_search_words = array(); if (count($search_words) > 0) { foreach ($search_words as $word) { if (!empty($word)) { $final_search_words[] = $word; } } } // Generate a WHERE clause using all of the search keywords $where_list = array(); if (count($final_search_words) > 0) { foreach($final_search_words as $word) { $where_list[] = "title LIKE '%$word%'"; } } $where_clause = implode(' OR ', $where_list); // Add the keyword WHERE clause to the search query if (!empty($where_clause)) { $search_query .= " WHERE $where_clause"; } $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)) { echo '<tr class="results">'; echo '<td valign="top" width="20%">' . $row['title'] . '</td>'; echo '<td valign="top" width="50%">' . $row['description'] . '</td>'; echo '<td valign="top" width="10%">' . $row['state'] . '</td>'; echo '<td valign="top" width="20%">' . $row['date_posted'] . '</td>'; echo '</tr>'; } echo '</table>'; mysqli_close($dbc); ?> Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1079127 Share on other sites More sharing options...
magnetica Posted June 30, 2010 Share Posted June 30, 2010 Hi Try: //there was no space here $search_words = explode (' ', $user_search); //concat error $where_list[] = "description LIKE '%" . $word . "%'"; Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1079128 Share on other sites More sharing options...
Adam Posted June 30, 2010 Share Posted June 30, 2010 He's already fixed it? //there was no space here $search_words = explode (' ', $user_search); There was, now there's 2. //concat error $where_list[] = "description LIKE '%" . $word . "%'"; There was nothing wrong with the concatenation. Quote Link to comment https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/#findComment-1079129 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.