PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old July 10th, 2008, 09:10 AM
drewj2k drewj2k is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 162 drewj2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 14 m 42 sec
Reputation Power: 3
Multiple checkbox mysql query

I am not sure the best way to do this:

I have a form with about 50 tick box's of different issues which will link to a field in the database.

What's the best way to make this form search the database for these issues, some being ticked and some not.

here is an example of the form:

PHP Code:
<input name="criteria[Animal Testing Medical Discovery & Development]" type="checkbox" id="Animal Testing Medical Discovery & Development" value="1">
<
input name="criteria[Animal Testing Medical Ingredients]" type="checkbox" id="Animal Testing Medical Ingredients" value="1">
<
input name="criteria[Animal Testing Non-Medical Discovery & Development]" type="checkbox" id="Animal Testing Non-Medical Discovery & Development" value="1">
<
input name="criteria[Animal Testing Non-Medical Ingredients]" type="checkbox" id="Animal Testing Non-Medical Ingredients" value="1"


Thanks!!

Reply With Quote
  #2  
Old July 10th, 2008, 09:55 AM
Osiris Osiris is offline
Contributing User
Codewalkers Novice (500 - 999 posts)
 
Join Date: May 2008
Location: Sussex
Posts: 566 Osiris User rank is Private First Class (20 - 50 Reputation Level)Osiris User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 19 h 54 m 33 sec
Reputation Power: 2
what exactly do u want the search results to display? and what are in the fields to which the checkboxes correspond? Sorry, just trying to understand what you have/want
__________________
~~==~~ Whoever said nothing is impossible never tried pushing a revolving door ~~==~~

Reply With Quote
  #3  
Old July 10th, 2008, 01:18 PM
drewj2k drewj2k is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 162 drewj2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 14 m 42 sec
Reputation Power: 3
Quote:
Originally Posted by Osiris
what exactly do u want the search results to display? and what are in the fields to which the checkboxes correspond? Sorry, just trying to understand what you have/want


The data that will display is anything which corresponds to the checkbox for example if these check box's are ticked:

PHP Code:
<input name="criteria[Animal Testing Medical Discovery & Development]" type="checkbox" id="Animal Testing Medical Discovery & Development" value="1">
<
input name="criteria[Animal Testing Medical Ingredients]" type="checkbox" id="Animal Testing Medical Ingredients" value="1"


when searching the query would return anything in the database which where the criteria field = Animal Testing Medical Ingredients OR Animal Testing Medical Discovery & Development.

Hope this helps thanks!

Reply With Quote
  #4  
Old July 10th, 2008, 04:23 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
Looks like you already have it setup so each checkbox is part of an array. checkboxes only pass the value and name if it is checked so something like this might work:

PHP Code:
if(isset($_POST['criteria']) && !empty($_POST['criteria'])){
    foreach(
$_POST['criteria'] as $key=>$value){
        if(
$value==1$criteria[] = "`columnName`='".mysql_escape_string($key)."'";
    }
    
$criteria implode(' OR '$criteria);
}
$query "SELECT * FROM `tableName` WHERE $criteria";
echo 
$query

Reply With Quote
  #5  
Old July 10th, 2008, 04:43 PM
drewj2k drewj2k is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 162 drewj2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 14 m 42 sec
Reputation Power: 3
Thumbs up

Quote:
Originally Posted by IAmALlama
Looks like you already have it setup so each checkbox is part of an array. checkboxes only pass the value and name if it is checked so something like this might work:

PHP Code:
if(isset($_POST['criteria']) && !empty($_POST['criteria'])){
    foreach(
$_POST['criteria'] as $key=>$value){
        if(
$value==1$criteria[] = "`columnName`='".mysql_escape_string($key)."'";
    }
    
$criteria implode(' OR '$criteria);
}
$query "SELECT * FROM `tableName` WHERE $criteria";
echo 
$query


That worked perfect thanks !!

Reply With Quote
  #6  
Old October 19th, 2009, 06:08 AM
smartans smartans is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 3 smartans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 7 sec
Reputation Power: 0
Assistance

Hi. Can i have the full code? I need that script too but cant seem to get them work. I need to display the output within the same page as the checkbox search filter.
Thanks in advance!

Reply With Quote
  #7  
Old October 19th, 2009, 03:11 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
the code I gave should be all that is needed to get it working. what exactly are you having trouble with? the form posting to the code I provided should look something similar to:
Code:
<form method='post' action='somePage.php'>
<input type='checkbox' name='criteria[1]' value=1>
<input type='checkbox' name='criteria[2]' value=1>
<input type='checkbox' name='criteria[3]' value=1>
<input type='checkbox' name='criteria[4]' value=1>
<input type='checkbox' name='criteria[5]' value=1>
<input type='submit' value='Go'>
</form>

The value on each one should be 1 to basically say "i'm checked" and the name should be criteria[someNumber] where "someNumber" is the id value for the specific row from the database.

Reply With Quote
  #8  
Old October 20th, 2009, 12:50 AM
smartans smartans is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 3 smartans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 7 sec
Reputation Power: 0
RE:Assistance

I am a newbie in php and mysql. I want the search filter to filter out according to the column name. My column name in mysql is 'category'. What i need to do is to change 'criteria' to 'category'

My Search Form:
Code:
<table width="280" border="1">
                    <tr>
                      <td valign="top" height="36"><p>&nbsp;</p></td>
                    </tr>
                    <tr>
                      <td align="left"><font face="Lucida Calligraphy" color="#8d0104">Search Filter:</font></td>
                    </tr>
                    <tr>
                      <td align="left"><font face="Lucida Calligraphy" color="#310100">&nbsp;&nbsp;&nbsp;&nbsp;Product Type</font></td>
                    </tr>
                    <tr>
                      <td><table width="243" height="132" align="right">
                        <tr>
                          <td width="311"><label>
                            <input type="checkbox" name="Product_Type" value="Single Wine" id="Product_Type_0" />
                            Single Wine</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Product_Type" value="Wine Bottle Opener" id="Product_Type_1" />
                            Wine Bottle Opener</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Product_Type" value="Wine Clipper" id="Product_Type_2" />
                            Wine Clipper</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Product_Type" value="Wine Glass" id="Product_Type_3" />
                            Wine Glass</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Product_Type" value="Wine Stopper" id="Product_Type_4" />
                            Wine Stopper</label></td>
                        </tr>
                      </table></td>
                    </tr>
                    <tr>
                      <td><img src="Horizontal_Divider.png" width="280" height="8" /></td>
                    </tr>
                    <tr>
                      <td><font face="Lucida Calligraphy" color="#310100">&nbsp;&nbsp;&nbsp;&nbsp;Price Range</font></td>
                    </tr>
                    <tr>
                      <td><table align="right" width="243">
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value=">50" id="Price_Range_0" />
                            €0 - €49</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value="50-99" id="Price_Range_1" />
                            €50 - €99</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value="100-149" id="Price_Range_2" />
                            €100 - €149</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value="150-199" id="Price_Range_3" />
                            €150 - €199</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value="200-249" id="Price_Range_4" />
                            €200 - €249</label></td>
                        </tr>
                        <tr>
                          <td><label>
                            <input type="checkbox" name="Price_Range" value="<250" id="Price_Range_5" />
                            €250 and Above</label></td>
                        </tr>
                      </table></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td align="center">
                      <script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $('#submit').hover(
            function(){ // Change the input image's source when we "roll on"
                $(this).attr({ src : 'Search_FilterBtnMO.png'});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : 'Search_FilterBtn.png'});             }
        );
    });
</script>
                      <input name="submit" type="image" id="submit" src="Search_FilterBtn.png" width="95" height="34" ></td>
                    </tr>
                  </table>


I also need my search filter to be in a range for the prices. My field name for prices is 'price'.

My DB connection is only:
Code:
mysql_connect('localhost','username','userpass');
mysql_select_db('dbname');
$query = "SELECT * FROM tablename";


I would like to thank you for ur assistance!

Reply With Quote
  #9  
Old October 21st, 2009, 12:38 PM
smartans smartans is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 3 smartans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 7 sec
Reputation Power: 0
Code:
<body>
<form method="post" action="" >
<input name="categories[BottleOpener]" type="checkbox" id="BottleOpener" value="1">Bottle Opener<br /> 

<input name="categories[WineClipper]" type="checkbox" id="WineClipper" value="1">Wine Clipper<br />

<input name="categories[WineGlass]" type="checkbox" id="WineGlass" value="1">Wine Glass<br /> 

<input name="categories[WineBottle]" type="checkbox" id="WineBottle" value="1">Wine Bottle<br />

<input type='submit' value='Search'>

</form>

<?php
mysql_connect('localhost','winecom1_bizbyte','jeri  jeri');
mysql_select_db('winecom1_wine7000');



if(isset($_POST['categories']) && !empty($_POST['categories'])){ 
    foreach($_POST['categories'] as $key=>$value){ 
        if($value==1) $criteria[] = "category='".mysql_escape_string($key)."'"; 
    } 
    $criteria = implode(' OR ', $criteria); 
} 
$query = "SELECT * FROM wineaccessories WHERE $criteria"; 
echo $criteria;  
?>
</body>


Im able to get my code running, but however, it is not from my database. I need the output to be from my database where the category="the category in my database".

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Multiple checkbox mysql query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek