Jump to content

Recommended Posts

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);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/206680-help-in-function/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081341
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081358
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081360
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/206680-help-in-function/#findComment-1081373
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.