Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/205842-help-in-implode-and-explode-function/
Share on other sites

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>

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

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

?>

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.

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.