Jump to content

Recommended Posts

Hi, I want to create a search which uses the 'title' of my 'product' table and matches the terms the user has submitted.

At the moment, my products are displaying one by one.

 

For example,

I have an apple and apricot in my table.

The user searches 'a' and the search results show 'apple' but not apricot.

 

Here's my code:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";
$result = mysqli_query($dbc, $query);

// Fetch the results.
$row = mysqli_fetch_array($result);

// Print the results:


$output[] = '<ul>';
$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';

$output[] = '</ul>';
echo join('',$output);
	}

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}
?>

 

A friend told me I need to use a for-each loop but I can't figure it out.

 

Any help is appreciated,

thanks for reading.

Link to comment
https://forums.phpfreaks.com/topic/230224-searching/
Share on other sites

change this

// Fetch the results.
$row = mysqli_fetch_array($result);

// Print the results:


$output[] = '<ul>';
$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';

$output[] = '</ul>';
echo join('',$output);

to this

$result=mysqli_query($dbc,$query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
        $output[] = '<ul>';
$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
        $output[] = '</ul>';
}
echo join('',$output);

Link to comment
https://forums.phpfreaks.com/topic/230224-searching/#findComment-1185629
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.