genzedu777 Posted July 4, 2010 Share Posted July 4, 2010 Hi guys, Will require your help again, I am building a function() on my own, but it is not working. when I searched it in search.html, no results appeared in search.php. Anyone could pin point my mistake? Thanks Wilson <?php function build_query($user_search) { // 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"; } return $search_query; } // 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 = build_query($user_search); $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%">' . substr($row['description'], 0, 100) . '</td>'; echo '<td valign="top" width="10%">' . $row['state'] . '</td>'; echo '<td valign="top" width="20%">' . substr($row['date_posted'], 0, 100) . '</td>'; echo '</tr>'; } echo '</table>'; mysqli_close($dbc); ?> Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/ Share on other sites More sharing options...
wildteen88 Posted July 4, 2010 Share Posted July 4, 2010 You seem to be generating your query from the build_query() function. Make sure your query is being formatted correctly by echoing out the $query variable.. Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1080987 Share on other sites More sharing options...
genzedu777 Posted July 5, 2010 Author Share Posted July 5, 2010 Hi wildteen88, Could you elaborate further in your explanation? I have created my own function, which is build_query(), this build_query() function will work on $user_search variable. Is it not appropriate to call the function in this manner? $query = build_query($user_search); What actually went wrong, did I use the build_query() function incorrectly? Please advice. Thanks Wilson Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081341 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 After this line <?php $query = build_query($user_search); ?> echo the query to see if it is being generated correctly <?php echo $query . "<br>\n"; ?> If it's not being generated correctly, you will have to debug your function. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081345 Share on other sites More sharing options...
genzedu777 Posted July 5, 2010 Author Share Posted July 5, 2010 Is this the right way to place it? <?php function build_query($user_search) { // 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"; } return $search_query; } // 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 = build_query($user_search); <?php echo $query . "<br>\n"; ?> $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%">' . substr($row['description'], 0, 100) . '</td>'; echo '<td valign="top" width="10%">' . $row['state'] . '</td>'; echo '<td valign="top" width="20%">' . substr($row['date_posted'], 0, 100) . '</td>'; echo '</tr>'; } echo '</table>'; mysqli_close($dbc); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081358 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 Think about it. You're already in PHP, so you don't need the "<?php" before the line or the "?>" after the line. You really should learn how to debug your own scripts -- it's much faster than asking a question in a forum and then waiting for an answer. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081360 Share on other sites More sharing options...
genzedu777 Posted July 5, 2010 Author Share Posted July 5, 2010 Thanks, I really appreciate your help, but it's not very kind of you to say that. As I'm really new in php, and I do not have the skill set to debug my own script unless I pick it up along the way through everyone's advice. Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081367 Share on other sites More sharing options...
genzedu777 Posted July 5, 2010 Author Share Posted July 5, 2010 Anyway, Thanks for everyone's help. I have managed to resolve the error. Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081371 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 No, it's a very kind thing to say. Whenever you learn a new programming language, the first thing you should learn is how to debug the code because you will need that skill all the time. Your code will have errors that need to be fixed, nobody writes perfect code the first time (well, maybe if the code isn't very complicated, it can be done). Ken Quote Link to comment https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081373 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.