Page 1 of 1

Limiting results to checkbox values from database

Posted: Sun Aug 05, 2007 8:41 am
by trassalg
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

Code: Select all

<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

Code: Select all

<?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>

Posted: Sun Aug 05, 2007 7:55 pm
by califdon
It might be as simple as inserting a space between $attr and $chk in your concatenation of $tag. What does $tag echo? Does it look correct?