tc1967uk Posted November 26, 2008 Share Posted November 26, 2008 Hi I'm a total newb at php and mysql but using Dreamweaver I've managed to create a php webpage containing a series of checkboxes (I think of them as tags) and a text field (for URLs) that allows me to enter data into a database. I've done a test post, and it works. The problem is, I don't know how to create a search function that will allow me to tick the same boxes and do a search that will bring up the URLs in the text field. Can someone suggest how to edit my page to add a search option that brings up the results a user defines, with options to choose between showing the result that meet ALL defined criteria, or just SOME criteria? The results should open in a new page. How the search should work - E.g. if we have links on the database that have the following tags: AB A AC B C BC and a user ticks the boxes marked A & C, and also the button marked 'Show results that meet ANY criteria'. He would then see the following results: AB A C BC However, if he had clicked 'Show results that meet ALL criteria' he would have gotten this instead: AC At this moment, I THINK I only need simple script that I could build on, so if someone can suggest a basic script to my homepage, I would be grateful. I'll include my homepage script and also my database script, in case that helps. Thanks for reading this. ====================================================== <?php require_once('Connections/mine_timbo.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO timbo (apple, strawberry, liverpool, manchesterunited, chelsea, arsenal, everton, circle, square, triangle, hexagon, red, orange, yellow, green, algeria, brazil, chile, denmark, egypt, finland, url) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString(isset($_POST['apple']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['strawberry']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['liverpool']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['manchesterunited']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['chelsea']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['arsenal']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['everton']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['circle']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['square']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['triangle']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['hexagon']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['red']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['orange']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['yellow']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['green']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['algeria']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['brazil']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['chile']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['denmark']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['egypt']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString(isset($_POST['finland']) ? "true" : "", "defined","'Y'","'N'"), GetSQLValueString($_POST['url'], "text")); mysql_select_db($database_mine_timbo, $mine_timbo); $Result1 = mysql_query($insertSQL, $mine_timbo) or die(mysql_error()); $insertGoTo = "thanks.html"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } ?><!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Index.me !</title> <style type="text/css"> <!-- .style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; } .style4 {font-size: x-small} .style6 {color: #FF0000} .style8 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; font-weight: bold; } --> </style> <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script> </head> <body> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><img src="logo2.png" alt="logo" width="798" height="87" /></td> </tr> <tr> <td><form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1"> <table border="0" align="left" bgcolor="#FFFFFF"> <tr valign="baseline"> <td width="57" align="left" nowrap="nowrap" bgcolor="#FFFFFF"><span class="style3">Fruit</span></td> <td colspan="2" bgcolor="#FFFFFF"><span class="style3">Apple: <input name="apple" type="checkbox" id="apple" value="" /> Strawberry: <input name="strawberry" type="checkbox" id="strawberry" value="" /> </span></td> </tr> <tr valign="baseline"> <td align="left" nowrap="nowrap" bgcolor="#DDE3F4"><span class="style3">Team:</span></td> <td colspan="2" bgcolor="#EBEFF9"><span class="style3">Liverpool: <input name="liverpool" type="checkbox" id="liverpool" value="" /> Manchester United: <input name="manchesterunited" type="checkbox" id="manchesterunited" value="" /> Chelsea: <input name="chelsea" type="checkbox" id="chelsea" value="" /> Arsenal: <input name="arsenal" type="checkbox" id="arsenal" value="" /> Everton: <input name="everton" type="checkbox" id="everton" value="" /> </span></td> </tr> <tr valign="baseline"> <td align="left" nowrap="nowrap" bgcolor="#FFFFFF"><span class="style3">Shapes:</span></td> <td colspan="2" bgcolor="#FFFFFF"><span class="style3">Square: <input name="square" type="checkbox" id="square" value="" /> Circle: <input name="circle" type="checkbox" id="circle" value="" /> Triangle: <input name="triangle" type="checkbox" id="triangle" value="" /> Hexagon: <input name="hexagon" type="checkbox" id="hexagon" value="" /> </span></td> </tr> <tr valign="baseline"> <td align="left" nowrap="nowrap" bgcolor="#DDE3F4"><span class="style3">Colours:</span></td> <td colspan="2" bgcolor="#EBEFF9"><span class="style3">Red: <input name="red" type="checkbox" id="red" value="" /> Orange: <input name="orange" type="checkbox" id="orange" value="" /> Yellow: <input name="yellow" type="checkbox" id="yellow" value="" /> Green: <input name="green" type="checkbox" id="green" value="" /> </span></td> </tr> <tr valign="baseline"> <td align="left" nowrap="nowrap" bgcolor="#FFFFFF"><span class="style3">Countries:</span></td> <td colspan="2" bgcolor="#FFFFFF"><span class="style3">Algeria: <input name="algeria" type="checkbox" id="algeria" value="" /> Brazil: <input name="brazil" type="checkbox" id="brazil" value="" /> Chile: <input name="chile" type="checkbox" id="chile" value="" /> Denmark: <input name="denmark" type="checkbox" id="denmark" value="" /> Egypt: <input name="egypt" type="checkbox" id="egypt" value="" /> Finland: <input type="checkbox" name="finland" value="" /> </span></td> </tr> <tr valign="baseline"> <td align="left" valign="bottom" nowrap="nowrap" bgcolor="#FFFFFF"> </td> <td width="621" valign="bottom" bgcolor="#FFFFFF"><span class="style8">Url:</span> <input name="url" type="text" value="" size="32" /> <input name="submit" type="submit" value="Insert record" /> <input name="Reset" type="reset" id="button" value="Reset" /></td> <td width="108" align="right" valign="bottom" bgcolor="#FFFFFF"> </td> </tr> </table> <input type="hidden" name="MM_insert" value="form1" /> </form> <p> </p></td> </tr> </table> </body> </html> ============================ -- phpMyAdmin SQL Dump -- version 2.11.6 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Nov 24, 2008 at 10:37 AM -- Server version: 4.1.22 -- PHP Version: 5.2.6 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `blah_blah` -- -- -------------------------------------------------------- -- -- Table structure for table `timbo` -- CREATE TABLE IF NOT EXISTS `timbo` ( `timbo_id` int(11) NOT NULL auto_increment, `apple` varchar(10) NOT NULL default '', `strawberry` varchar(10) NOT NULL default '', `liverpool` varchar(10) NOT NULL default '', `manchesterunited` varchar(10) NOT NULL default '', `chelsea` varchar(10) NOT NULL default '', `arsenal` varchar(10) NOT NULL default '', `everton` varchar(10) NOT NULL default '', `square` varchar(10) NOT NULL default '', `circle` varchar(10) NOT NULL default '', `triangle` varchar(10) NOT NULL default '', `hexagon` varchar(10) NOT NULL default '', `red` varchar(10) NOT NULL default '', `orange` varchar(10) NOT NULL default '', `yellow` varchar(10) NOT NULL default '', `green` varchar(10) NOT NULL default '', `algeria` varchar(10) NOT NULL default '', `brazil` varchar(10) NOT NULL default '', `chile` varchar(10) NOT NULL default '', `denmark` varchar(10) NOT NULL default '', `egypt` varchar(10) NOT NULL default '', `finland` varchar(10) NOT NULL default '', `url` text NOT NULL, PRIMARY KEY (`timbo_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; -- -- Dumping data for table `timbo` -- INSERT INTO `timbo` (`timbo_id`, `apple`, `strawberry`, `liverpool`, `manchesterunited`, `chelsea`, `arsenal`, `everton`, `square`, `circle`, `triangle`, `hexagon`, `red`, `orange`, `yellow`, `green`, `algeria`, `brazil`, `chile`, `denmark`, `egypt`, `finland`, `url`) VALUES (34, 'Y', 'N', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'www.test01.com'); Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/ Share on other sites More sharing options...
haku Posted November 26, 2008 Share Posted November 26, 2008 It depends on how your database is set up. Are each of the checkboxes a separate column in the database, or do they all refer to potential data in one column? Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-699456 Share on other sites More sharing options...
.josh Posted November 26, 2008 Share Posted November 26, 2008 LIKE Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-699580 Share on other sites More sharing options...
tc1967uk Posted November 27, 2008 Author Share Posted November 27, 2008 I think they're all seperate, Haku. Crayon violet, I don't undertand the code in your example. Is it not possible to tell from the info I've listed above? Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-700162 Share on other sites More sharing options...
.josh Posted November 27, 2008 Share Posted November 27, 2008 I think they're all seperate, Haku. How do you suppose to get something done or even ask for help, when you aren't even sure what you're working with? Crayon viole[n]t, I don't undertand the code in your example. Is it not possible to tell from the info I've listed above? Unless I misunderstood, you asked how to return results containing submitted data. names John Mary Greg Rita Return all names with for example 'a' in them, right? Use the sql LIKE comparison. Instructions on how to use it are in the link. Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-700330 Share on other sites More sharing options...
sasa Posted November 27, 2008 Share Posted November 27, 2008 try <form method="POST"> <input type="checkbox" value="A" name="le[]" />A<br /> <input type="checkbox" value="B" name="le[]" />B<br /> <input type="checkbox" value="C" name="le[]" />C<br /> <input type="submit" name="submit" value="any" /> <input type="submit" name="submit" value="all" /> </form> <?php if (isset($_POST['submit'])){ mysql_connect('localhost', 'root',''); mysql_select_db('test') OR die(mysql_error()); if ($_POST['submit'] == 'all'){ if (isset($_POST['le'])){ $sql = 'SELECT * FROM `link`'; foreach ($_POST['le'] as $i => $le){ $sql ='SELECT * FROM ('. $sql. ') as xx'.$i.' WHERE `link` LIKE "%'. $le. '%"'; } }else echo 'error,'; } if ($_POST['submit'] == 'any'){ if (isset($_POST['le'])){ $sql = 'SELECT * FROM `link` WHERE link LIKE "%'. implode('%" OR link LIKE "%', $_POST['le']). '%"'; }else echo 'error,'; } if ($sql){ $res = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($res)) echo "$row[id] -> $row[link]<br />\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-700463 Share on other sites More sharing options...
tc1967uk Posted November 29, 2008 Author Share Posted November 29, 2008 Thanks Sasa. I've tried to get your code working, and have inserted username , password and tablename succesfully, but nothing happens when I click 'all' or 'any'. I think this may be due to the fact that when I set it up using dreamweaver, I didn't set up columns, which Haku mentioned before. I see that I gave the wrong information due to lack of understanding (sorry Haku). They're grouped in my example (i.e. some are fruit, some are football teams, some are places etc) but in my database it's just one long list of of items turned on or off by checkboxes. At this moment I don't see a need to group my list and create columns (though to be honest, I wouldn't have a clue how to create and group them if there were a need). Will the code you gave me not work unless I do so? Quote Link to comment https://forums.phpfreaks.com/topic/134351-how-to-set-up-a-search-that-uses-checkboxes/#findComment-701838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.