trassalg Posted August 5, 2007 Share Posted August 5, 2007 I've got the following two pages: search.php and categories_search.php. In a page that uses search.php as a form handler, I have categories_search.php included. What I can't figure out is how to get the selected checkboxes from the page buildArticleList.php (the page that uses search.php to handle the form data) to select the individual values selected in the checkboxes and limit the returned results to articles that have these values selected. Make sense? From reading around forums and tutorial sites, I think I need to use something like the following, but have no idea what data to fill it with or where it goes: foreach($_POST['checkbox'] as $id => $val) search.php <html> <head> <title>Baso de datos de Rodolfo</title> <link href="styles/cal.css" rel="stylesheet" type="text/css"> <? include("includes/misc.inc"); include("includes/connection.inc"); include("includes/menuBar.php"); ?> </head> <body> <?php if ( ($search1=="") ... && ($search10=="") ) { $search1=" "; } else { if ($search1 == "") { $search1 = "BLANK_FIELD"; } ... if ($search10 == "") { $search10 = "BLANK_FIELD"; } } $fromDate = $_POST['fromDate']; $untilDate = $_POST['untilDate']; $orderby = $_POST['orderby']; $categories = $_POST['labels']; $query = "SELECT * FROM articleTable WHERE 1=0 OR (articleTitle LIKE \"%$search1%\" OR articleText LIKE \"%$search1%\" ... OR articleTitle LIKE \"%$search10%\" OR articleText LIKE \"%$search10%\") AND ((author LIKE \"$author\" AND source LIKE \"$source\") AND (categories LIKE \"%$categories%\") AND (dateOfArticle BETWEEN '$fromDate' AND '$untilDate')) ORDER BY 'dateOfArticle'"; $result = mysql_query($query); if (!ereg("Resource id #3", $result)) { echo "<br><br><br><div align=center>Your Results:</div>"; $table_content = ""; $table_content .= "<table border='1'>"; while ($row = mysql_fetch_array($result)) { $table_content .= "<tr> <td>".$row['articleIDNumber']."</td> <td>".$row['dateOfArticle']."</td> <td>".$row['categories']."</td> <td>".$row['author']."</td> <td>".$row['articleTitle']."</td> <td>".$row['articleText']."</td> <td>".$row['source']."</td> </td> </tr>"; } $table_content .= "</table>"; echo $table_content; } else { echo "<div align='center'><br><br><br><br><br><br><br><br><br><strong>No tenes resultados</strong></div><br>"; } mysql_close(); ?> </body> </html> categories_search.php <?php //###### function def ######## function checkbox($name,$label,$value_yn,$input='',$category_array='') { $chk = ''; $attr = $category_array; if(is_array($category_array)) { $attr = ''; foreach($category_array as $key=>$aValue) { $attr .= " $key=\"$aValue\""; } } if($value_yn == trim($input)) { $chk = ' checked="checked"'; } $tag = "<label><input type=\"checkbox\" name=\"categories[$i]\" value=\"$value_yn\"".$attr.$chk." />$label</label>\n"; echo $tag; } //###### end function def ######## ?> <?php include("includes/misc.inc"); include("includes/connection.inc"); $result = mysql_query("SELECT categories FROM articleTable WHERE articleIDNumber=$articleIDNumber"); while ($row = mysql_fetch_array($result)) { $category_data = explode(",",$row['categories']); } echo "<table>\n <tr>\n <td>\n"; // Below are the categories you want them to choose from // Lets use them as values too $labels = array( '1', '11', '12', ... '1110', '1111', '1112' ); $names = array( '<strong>1. ALIMENTOS</strong>', '1.1 - AGRO', '1.2 - INDUSTRIA<br>', '<strong>2. AMBIENTE</strong>', ... '11.10 - Tema del...', '11.11 - Tendencias', '11.12 - Teorías<br>' ); for ($i = 0; $i < count($labels); $i++){ $in = ''; if(in_array($labels[$i],$category_data)) { $in = $labels[$i]; } if(in_array($names[$i],$category_data)) { $in = $names[$i]; } checkbox('categories[]',$names[$i],$labels[$i],$in); echo "<br>\n"; } ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/63414-limiting-results-to-checkbox-values-from-database/ Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 Make the checkbox value = the cat id $tag = "<label><input type=\"checkbox\" name=\"categories[$i]\" value=\"$id\"".$attr.$chk." />$label</label>\n"; to process <?php $cats = join ("','", $_POST['categories']) and in the query, use ... WHERE category IN ('$cats') Quote Link to comment https://forums.phpfreaks.com/topic/63414-limiting-results-to-checkbox-values-from-database/#findComment-316033 Share on other sites More sharing options...
trassalg Posted August 18, 2007 Author Share Posted August 18, 2007 I tried your suggestion, but it doesn't seem to be retrieving results correctly (it's not using the category selections as a filtering method). What am I doing wrong? <?php //###### function def ######## function checkbox($name,$label,$value_yn,$input='',$category_array='') { $chk = ''; $attr = $category_array; if(is_array($category_array)) { $attr = ''; foreach($category_array as $key=>$aValue) { $attr .= " $key=\"$aValue\""; } } if($value_yn == trim($input)) { $chk = ' checked="checked"'; } $tag = "<label><input type=\"checkbox\" name=\"categories[$i]\" value=\"$id\"".$attr.$chk." />$label</label>\n"; echo $tag; } //###### end function def ######## ?> <?php include("includes/misc.inc"); include("includes/connection.inc"); $cats = join ("','", $_POST['categories']); $result = mysql_query("SELECT categories FROM articleTable WHERE articleIDNumber=$articleIDNumber AND category IN ('$cats')"); while ($row = mysql_fetch_array($result)) { $category_data = explode(",",$row['categories']); } echo "<table>\n <tr>\n <td>\n"; // Below are the categories you want them to choose from // Lets use them as values too $labels = array( '1', '11', '12', '2', '21', '22', '23', '3', '31', '32', '33', '34', '35', '4', '41', '42', '5', '51', '52', '53', '6', '61', '611', '612', '613', '614', '615', '616', '617', '618', '62', '621', '622', '623', '624', '625', '7', '71', '72', '73', '74', '75', '8', '81', '82', '83', '84', '85', '86', '87', '9', '91', '92', '93', '94', '95', '10', '101', '102', '103', '104', '105', '106', '107', '108', '109', '1010', '1011', '1012', '1013', '1014', '1015', '1016', '1017', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '1110', '1111', '1112' ); $names = array( '<strong>1. ALIMENTOS</strong>', '1.1 - AGRO', '1.2 - INDUSTRIA<br>', '<strong>2. AMBIENTE</strong>', '2.1 - Ecología', '2.2 - Energía', '2.3 - Urbanismo<br>', '<strong>3. CIENCIA: SU DESARROLLO</strong>', '3.1 - Biológicas', '3.2 - Ciencias Todas', '3.3 - De la Salud', '3.4 - Duras', '3.5 - Sociales<br>', '<strong>4. COMUNICACIÓN: SU DESARROLLO</strong>', '4.1 - Medios', '4.2 - Transporte<br>', '<strong>5. CULTURA: SU DESARROLLO</strong>', '5.1 - Antropología Cultural', '5.2 - Arte y Espectáculo', '5.3 - Educación<br>', '<strong>6. ECONOMÍA: SU DESARROLLO</strong>', '6.1 - ma MACRO', '6.1.1 - ma Crédito y Finanzas', '6.1.2 - ma Expo Impo', '6.1.3 - ma Impuestos', '6.1.4 - ma Inversiones', '6.1.5 - ma Marketing', '6.1.6 - ma Panorama', '6.1.7 - ma Tendencias', '6.1.8 - ma Teorías', '6.2 - ma x Zona', '6.2.1 - Empresas', '6.2.2 - Capacitación', '6.2.3 - Marketing', '6.2.4 - Panorama', '6.2.5 - Tendencias<br>', '<strong>7. JURÍDICA: SU DESARROLLO</strong>', '7.1 - Comercial', '7.2 - Constitucional y Varios', '7.3 - Familia', '7.4 - Laboral', '7.5 - Penal<br>', '<strong>8. POLÍTICA: SU DESARROLLO</strong>', '8.1 - Elecciones 2007', '8.2 - Estado y Planificación', '8.3 - Gobierno Nacional', '8.4 - Internacionales', '8.5 - Noticias', '8.6 - Partidos', '8.7 - Politica Regional<br>', '<strong>9. SALUD: SU DESARROLLO</strong>', '9.1 - Calidad de Vida SA', '9.2 - Conceptual', '9.3 - Institucional', '9.4 - Medicina', '9.5 - Panorama<br>', '<strong>10. SOCIOLOGÍA: SU DESARROLLO</strong>', '10.1 - Calidad de Vida SO', '10.2 - Clases', '10.3 - Control Social', '10.4 - Demografía', '10.5 - Economía Social', '10.6 - Futuro CF', '10.7 - Género', '10.8 - Globalización', '10.9 - Historia Toda', '10.10 - Joventud', '10.11 - Modas', '10.12 - Modernidad PosModernidad', '10.13 - ONG', '10.14 - Religión', '10.15 - Seguridad', '10.16 - Sociología', '10.17 - Trabajo<br>', '<strong>11. HERRAMIENTAS: SU DESARROLLO</strong>', '11.1 - ABC', '11.2 - Anita', '11.3 - Bibliografía', '11.4 - Calendario 2007', '11.5 - Clientes', '11.6 - Gentes', '11.7 - Medios', '11.8 - Personal', '11.9 - Tecnologís', '11.10 - Tema del...', '11.11 - Tendencias', '11.12 - Teorías<br>' ); for ($i = 0; $i < count($labels); $i++){ $in = ''; if(in_array($labels[$i],$category_data)) { $in = $labels[$i]; } if(in_array($names[$i],$category_data)) { $in = $names[$i]; } checkbox('categories[]',$names[$i],$labels[$i],$in); echo "<br>\n"; } ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/63414-limiting-results-to-checkbox-values-from-database/#findComment-327304 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.