Jump to content

If > 1 then print 1 variable.


golfer

Recommended Posts

I currently have a search page on my site that prints the products but it prints the products more than once if its in more than one category I have tried getting distinct item in my SQL. But this doesnt work so im trying an if statement that if there is more than one specific result then to just print this once. I was wondering if anyone had any ideas of how to do this using an if statement I just dont know how to go about just printing the result just once if its greater than 1. The code is below to make it clearer.

 

$searchterm = $_POST['searchterm'];

trim ($searchterm);

 

/*check if search term was entered*/

if (!$searchterm){

        echo 'Please enter a search term.';

echo $searchterm;

}

/*add slashes to search term*/

if (!get_magic_quotes_gpc())

{

$searchterm = addslashes($searchterm);

}

 

 

/*query the database*/

$query = "SELECT * from

(products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id)

LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id WHERE prod_title LIKE '%" . $searchterm .  "%' ORDER BY cat_title, prod_title";

$result = mysql_query($query);

/*number of rows found*/

$num_results = mysql_num_rows($result);

 

echo '<p><h1>Search Results: '.$num_results.'</h1></p><br />';

/*loops through results*/

for ($i=0; $i <$num_results; $i++)

{

$num_found = $i + 1;   

$row = mysql_fetch_assoc($result);

echo "$num_found. "?><a href="store-<?php echo $row['cat_id'];?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><strong><?php echo $row['prod_title']; ?></strong></a> <br />

Link to comment
https://forums.phpfreaks.com/topic/221222-if-1-then-print-1-variable/
Share on other sites

Use LIMIT in your select query.

 

0 is starting from where, 10 is the max limit from the starting point

 

displays the first 10 results from the database

SELECT * FROM `your_table` LIMIT 0, 10

 

displays records 6, 7, 8, 9,,10

SELECT * FROM `your_table` LIMIT 5, 5

 

$searchterm = $_POST['searchterm'];
trim ($searchterm);

/*check if search term was entered*/
if (!$searchterm){
        echo 'Please enter a search term.';
      echo $searchterm;
}
/*add slashes to search term*/
if (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}


/*query the database*/
$query = "SELECT * from
(products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id)
LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id WHERE prod_title LIKE '%" . $searchterm .  "%' ORDER BY cat_title, prod_title LIMIT 0,1";
$result = mysql_query($query);
/*number of rows found*/
$num_results = mysql_num_rows($result);

echo '<p><h1>Search Results: '.$num_results.'</h1></p><br />';
/*loops through results*/
for ($i=0; $i <$num_results; $i++)
{
$num_found = $i + 1;   
$row = mysql_fetch_assoc($result);
echo "$num_found. "?><a href="store-<?php echo $row['cat_id'];?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><strong><?php echo $row['prod_title']; ?></strong></a> <br />

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.