Jump to content

Limiting results from database to checkboxes selected


trassalg

Recommended Posts

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?

 

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=\"$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>

Crikey!

 

can't be arsed to read all that code but Iwill give you something that will help A LOT

 

define each check box like so inside you loop that displays them

 

<input type=checkbox name="category[]" value="<?php echo $row['cattitle']; ?>" />

 

obviously change the bits that need changing to suit your needs...

 

OK so you want to select all teh stuff with the search term that exist in the selected catagories...

<?php
$qry = "SELECT `categories` FROM `articleTable` WHERE `articleIDNumber` = " . $articleIDNumber . " AND `category` IN (" . implode(',',$_POST['category']) . ")";
?>

I'm confused.  I've posted this same question on other message boards and keep getting ambiguous answers... telling me I should add "such and such" a thing but nobody seems to tell me in WHICH file or WHERE, which is the biggest problem I'm having.  If anyone can help walk me through this I'd be extremely appreciative.

in your form make sure all teh check boxes have name="category[]" - so that would mean adjusting which ever script generates the html form...

 

in your processing script use the query

 

<?php
$qry = "SELECT `categories` FROM `articleTable` WHERE `articleIDNumber` = " . $articleIDNumber . " AND `category` IN (" . implode(',',$_POST['category']) . ")";
?>

 

so that would be the script that uses the info from the form to search the database - replace the current query with the one posted above.

That's not working.  Here's the page using your suggestion.  Any ideas?

 

<html>
<head>
<?php
$todays_date = date("Y-m-d");
?>
<title>Noticias: <?php echo "$todays_date"; ?></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=="") 
&& ($search2=="") 
&& ($search3=="") 
&& ($search4=="") 
&& ($search5=="") 
&& ($search6=="") 
&& ($search7=="") 
&& ($search8=="") 
&& ($search9=="") 
&& ($search10=="") ) {
	$search1=" ";
}
else {
if ($search1 == "") {
$search1 = "BLANK_FIELD";
}
if ($search2 == "") {
$search2 = "BLANK_FIELD";
}
if ($search3 == "") {
$search3 = "BLANK_FIELD";
}
if ($search4 == "") {
$search4 = "BLANK_FIELD";
}
if ($search5 == "") {
$search5 = "BLANK_FIELD";
}
if ($search6 == "") {
$search6 = "BLANK_FIELD";
}
if ($search7 == "") {
$search7 = "BLANK_FIELD";
}
if ($search8 == "") {
$search8 = "BLANK_FIELD";
}
if ($search9 == "") {
$search9 = "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 \"%$search2%\" 
OR articleText LIKE \"%$search2%\" 
OR articleTitle LIKE \"%$search3%\" 
OR articleText LIKE \"%$search3%\"
OR articleTitle LIKE \"%$search4%\" 
OR articleText LIKE \"%$search4%\" 
OR articleTitle LIKE \"%$search5%\" 
OR articleText LIKE \"%$search5%\"
OR articleTitle LIKE \"%$search6%\" 
OR articleText LIKE \"%$search6%\"
OR articleTitle LIKE \"%$search7%\" 
OR articleText LIKE \"%$search7%\"
OR articleTitle LIKE \"%$search8%\" 
OR articleText LIKE \"%$search8%\"
OR articleTitle LIKE \"%$search9%\" 
OR articleText LIKE \"%$search9%\"
OR articleTitle LIKE \"%$search10%\" 
OR articleText LIKE \"%$search10%\") 
AND ((author LIKE \"$author\"
AND source LIKE \"$source\") 



AND articleIDNumber=(" . $articleIDNumber . ")
AND category IN (". implode(',',$_POST['category']) . ")")



AND (dateOfArticle 
BETWEEN '$fromDate' AND '$untilDate')) ORDER BY \"dateOfArticle\" DESC";

$result = mysql_query($query);
if (!ereg("Resource id #3", $result)) {
$table_content = "";
$table_content .= "<table border='0' width='800'>";
while ($row = mysql_fetch_array($result)) {
	$table_content .= "<tr>
	<td><font color='red'>#: ".$row['articleIDNumber']."</font></td></tr>
	<td><strong>Autor:</strong> ".$row['author']."
		         
		<strong>Fuente:</strong> ".$row['source']."</td></tr>
	<tr><td height='15'></td></tr>
	<tr><td><h3>".$row['articleTitle']."</h3></td>
	<td><strong>Fecha:</strong> ".$row['dateOfArticle']."</td></tr>
	<tr><td width='500' height='10'><hr></td></tr>
	<tr><td colspan='2'>".$row['articleText']."</td></tr>
	<tr><td><br><br><strong>Categorías:</strong> ".$row['categories']."</td></tr>
	<tr><td colspan='2'><br><br><br><br><hr><hr><br><br><br><br></td>
	</tr>";
	}
$table_content .= "</table>";
echo $table_content;
	}
else {
echo "<div align='center'><br><br><br><br><br><br><br><br><br><strong><font size='+3'>No tenes resultados</font></strong></div><br>";
}

mysql_close();

?>
</body>
</html>

None, but that's the idea.  When I didn't have that conditional, the script didn't run and therefore someone on another board suggested using it as a way of having a if/or that would always go to the second option (as the first is logically impossible).

 

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.