Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Multiple entries for search
Old 05-22-2009, 08:32 AM Multiple entries for search
Skilled Talker

Posts: 60
Trades: 0
How do I do multiple searches in the same link?
At the moment I'm using:
PHP Code:
<li><a href="shop.php?category_id=5">Computers</a></li
But I would like to search more than one category, so as well as searching computers I'd also search printers (categories 5 and 8). To get the results I'm using
PHP Code:
$category_id=$_GET['category_id'];
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' and category_id='$category_id' and picture1!='' and (expire_date > '$datecheck') order by expire_date"
I tried to change the url so that it said "shop.php?category_id=5+8" but that didn't work. I also tried to use explode, but it didn't work. I used:
PHP Code:
<li><a href="shop.php?category_id=5+8">Computers</a></li
and then for the shop.php page I put:
PHP Code:
$category_id=$_GET['category_id'];
$category_id explode("+"$category_id);
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' and category_id='$category_id' and picture1!='' and (expire_date > '$datecheck') order by expire_date"
at the top but it resulted in a blank page with no results - but I know both categories should have lots of results

Also how do I get it to show all the results regardless of the category id? Again I tried using "*" but that didn't work
Thanks
thehappyappy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-22-2009, 10:51 AM Re: Multiple entries for search
Novice Talker

Posts: 9
Trades: 0
The problem is that after you call explode on $category_id, $category_id becomes an array.

So your sql statement is invalid at category_id='$category_id'

You will need to make a for loop to append the string "and category_id = '$category_id" based on the size of the category_id array
Quote:

for($a=0;$a<count($category_id);$a++)
$category_sql .= "and category_id = $category_id[$a]";


$feature_sql="select * from shop where status='sale' ".$category_sql." and picture1!='' and (expire_date > '$datecheck') order by expire_date";
Something like the above. I haven't tested or anything.

To get results you will need to do a mysql_query on your $feature_sql
and print_r to display what is returned.

I hope this helps.
__________________

Please login or register to view this content. Registration is FREE

Free Pay Per Click for Webmasters

Last edited by sc_king; 05-22-2009 at 10:57 AM..
sc_king is offline
Reply With Quote
View Public Profile
 
Old 05-22-2009, 11:04 AM Re: Multiple entries for search
Skilled Talker

Posts: 60
Trades: 0
Okay I think I must have misunderstood what you said. I used this:
PHP Code:
$category_id=$_GET['category_id']; 
$category_id explode("+"$category_id); 
for(
$a=0;$a<count($category_id);$a++)
$category_sql .= "and category_id = $category_id[$a]";
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' and category_id='$category_id' and picture1!='' and (expire_date > '$datecheck') order by expire_date limit 0, 10";
    
$feature_res=mysql_query($feature_sql);
    
$tot_count=mysql_num_rows($feature_res);
    
$count=0;
    if(
$tot_count 0)
    { 
but the page was then blank

Last edited by thehappyappy; 05-22-2009 at 11:07 AM..
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 05-22-2009, 12:16 PM Re: Multiple entries for search
Novice Talker

Posts: 9
Trades: 0
add a space in front for $category_sql and make sure u insert it into you $feature_sql

PHP Code:
for($a=0;$a<count($category_id);$a++)
$category_sql .= " and category_id = $category_id[$a]";
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' ".$category_sql." and picture1!='' and (expire_date > '$datecheck') order by expire_date limit 0, 10";
 
$feature_res=mysql_query($feature_sql);
echo 
$feature_sql // may remove once sql statement is correct
echo mysql_num_rows($feature_res); // may remove once you're getting the results 
__________________

Please login or register to view this content. Registration is FREE

Free Pay Per Click for Webmasters
sc_king is offline
Reply With Quote
View Public Profile
 
Old 05-22-2009, 12:22 PM Re: Multiple entries for search
Novice Talker

Posts: 9
Trades: 0
Ahh crap, the sql statement won't work because you want each category. You will need an OR instead of AND. I'll get back to u.
__________________

Please login or register to view this content. Registration is FREE

Free Pay Per Click for Webmasters
sc_king is offline
Reply With Quote
View Public Profile
 
Old 05-22-2009, 12:28 PM Re: Multiple entries for search
Skilled Talker

Posts: 60
Trades: 0
thank you
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 05-22-2009, 12:33 PM Re: Multiple entries for search
Novice Talker

Posts: 9
Trades: 0
Try This,

PHP Code:
for($a=0;$a<count($category_id);$a++)
{
 
$category_sql .= "category_id = $category_id[$a]";
 if(
$a!=(count($category_id)-1))
  
$category_sql .= " or ";
 
}
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' and  (".$category_sql.") and picture1!='' and (expire_date > '$datecheck') order by expire_date limit 0, 10";
$feature_res=mysql_query($feature_sql);
echo 
$feature_sql// may remove once sql statement is correct
echo mysql_num_rows($feature_res); // may remove once you're getting the results 

I recommend you shorten your sql statement until you find the results you're looking for then add more to it. It will help u debug your issues.

You may take out the count in the for loop later on and place it in a variable also.
__________________

Please login or register to view this content. Registration is FREE

Free Pay Per Click for Webmasters

Last edited by sc_king; 05-22-2009 at 12:42 PM..
sc_king is offline
Reply With Quote
View Public Profile
 
Old 05-27-2009, 05:30 AM Re: Multiple entries for search
Skilled Talker

Posts: 60
Trades: 0
Sorry to be a pain, but that is returning no results at all, yet I checked that both of the category_id's had entires. I think that I'm doing something wrong somewhere, but am not sure what.
Quote:
Originally Posted by sc_king View Post
Try This,

PHP Code:
for($a=0;$a<count($category_id);$a++)
{
 
$category_sql .= "category_id = $category_id[$a]";
 if(
$a!=(count($category_id)-1))
  
$category_sql .= " or ";
 
}
$datecheck=date("Y-m-d H:i:s");
$feature_sql="select * from shop where status='sale' and  (".$category_sql.") and picture1!='' and (expire_date > '$datecheck') order by expire_date limit 0, 10";
$feature_res=mysql_query($feature_sql);
echo 
$feature_sql// may remove once sql statement is correct
echo mysql_num_rows($feature_res); // may remove once you're getting the results 

I recommend you shorten your sql statement until you find the results you're looking for then add more to it. It will help u debug your issues.

You may take out the count in the for loop later on and place it in a variable also.
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 05-28-2009, 09:00 PM Re: Multiple entries for search
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
To include where sc_king left off...

PHP Code:
// If $category_id is an array, we first better clean it:
$category_id array_map('intval'$category_id);
$feature_res mysql_query("
  SELECT *
  FROM shop
  WHERE status = 'sale'
    AND picture != ''
    AND expire_date > '" 
date('Y-m-d H:i:s') . "'
    AND category_id IN(" 
implode(', '$category_id) . ")
  ORDER BY expire_date
  LIMIT 0, 10
"
);
echo 
mysql_num_rows($feature_res); 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-29-2009, 05:07 AM Re: Multiple entries for search
Skilled Talker

Posts: 60
Trades: 0
thanks, I'll give that a go
thehappyappy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multiple entries for search
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32264 seconds with 12 queries