0

i have a function which accept an array which i want to breakdown and pass into where condition. My Function

<?php
function somearray($value = array())
{
    $conditions = "";
    foreach($value as $key => $condition)
    {
        $conditions .= "$key=$condition && ";
    }
    $conditions = substr($conditions, 0, -4);
    $qry = mysql_query("SELECT * FROM maincase WHERE $conditions");
    while($arr = mysql_fetch_array($qry))
    {
        echo 'Ticket Number= '.$arr['ticket_number'].'<br />';
    }
}

?>

i want to send values through a form, but only the selected checkbox

<?php 
        if(isset($_GET['filter']))
        {
            $list = array(
                "region" => $_GET['region'],
                "status" => $_GET['status']
            );
            somearray($list);
        }
    ?>

    <form action="" class="form-inline" method="get">
        <label for="">Region</label>
        <input type="checkbox" name="region" <?php if(isset($_GET['region'])) { echo 'checked' ;} ?> value="1">

        <label for="">Status</label>
        <input type="checkbox" name="status" <?php if(isset($_GET['status'])) { echo 'checked' ;} ?> value="3">

        <input type="submit" value="filter" name="filter">
    </form>

this code works. But i have to filter 7 to 8 fields. I want to send the values only if the checkbox is checked.

Sushant Aryal
  • 3,125
  • 1
  • 25
  • 22

2 Answers2

0

Passing a value in your function should be like this:

function somearray($value)
{
    $conditions = "";
    foreach($value as $key => $condition)
    {
        $conditions .= "$key=$condition && ";
    }
    $conditions = substr($conditions, 0, -4);
    $qry = mysql_query("SELECT * FROM maincase WHERE $conditions");
    while($arr = mysql_fetch_array($qry))
    {
        echo 'Ticket Number= '.$arr['ticket_number'].'<br />';
    }
}
?>

In your case the parameters are initialized as default or empty array where as you pass an array with value.

See reference:

Mark
  • 8,046
  • 15
  • 48
  • 78
  • i learned that from Laravel. but thats not the problem. if i check both field i want to send "region" => $_GET['region'], "status" => $_GET['status'] if i check status "status" => $_GET['status'] and so on.. – Sushant Aryal Jul 30 '13 at 09:41
  • Do you have data from your [query string/URL](http://stackoverflow.com/questions/8469767/get-url-query-string)? – Mark Jul 30 '13 at 09:43
0

Unchecked check boxes should not be sent by default.


<form action="?" method="post">
    <input type="checkbox" name="first" />
    <input type="checkbox" name="second" />
    <input type="submit" name="OK" />
</form>
<?php
    var_dump($_POST);

Sample output:

array(2) { 'first' => string(2) "on" 'OK' => string(6) "Submit" }

This code shows, that only seleted checkboxes' data is sent.

Mantas
  • 4,259
  • 2
  • 27
  • 32