For each loop

Hi,

I am trying to output the values of a checkbox that is set from a database like so:


<input type="checkbox" name="printCheckOutstanding" value="<?=$row['ID']?>" />

<?=$row[‘ID’]?> is a unique ID for each record as there are more than one.

       
if(isset($_POST['printOutstanding']) && isset($_POST['printCheckOutstanding'])):

            foreach($_POST['printCheckOutstanding'] AS $printOrderID) :
                print_r($printOrderID);
            endforeach;

endif;

But i get an error saying Warning: Invalid argument supplied for foreach() …

Any ideas what i am doing wrong?

Many thanks,

checkboxes return “on” or do not return anything.

If you have a bunch of similarly named checkboxes, try doing this instead:


<input type="checkbox" name="printCheckOutstanding[<?=$row['ID']?>]" />

Otherwise you can only detect if a box was checked.

Here is some old code from a reply I made some time ago, have a play around with that:


<?php
if( isset($_POST) )
    var_dump( $_POST );

$tag_ids = array_keys($_POST['tag_id']);

$verified = ( array_map('is_numeric', $tag_ids));

if( in_array(false, $verified) ){ 
exit( 'Eek! a bad one' );
}

$sql_in_argument = '(' . implode(',', $tag_ids) . ')';

echo '<hr />' . $sql_in_argument . '<hr />';

// this array is pretending to be your mysql select result
// ie select tag_id, tag_name
$rows = array(
1 => 'Mentions CEO',
2 => 'Mentions Sponsor',
3 => 'Review',
4 => 'Great Quotes',
'test' => 'Another nasty hack someone tried on you',
);

echo "<form action = '' method= 'POST'>";

foreach($rows as $key=>$value)
echo "<input type=checkbox id=tag_id[$key] name=tag_id[$key]>$value <br />" . PHP_EOL ;

echo "<input type=submit></form>";

Hi,

The number of checkboxes are dynamic depending on the amount of orders in the system so this will always vary.

All my checkboxes are named like so:

<input type=“checkbox” name=“printCheckOutstanding” value=“<?=$row[‘ID’]?>” />

So do you mean to say i should be doing it like this instead?

<input type=“checkbox” name=“printCheckOutstanding[<?=$row[‘ID’]?>]” />

I can then do a var_dump of $_POST and manually check a few of the check boxes to see what values are being returned?

Is that the way to go?

I will have a play around with your code and see where i get with that also :wink:

Thanks again,

Yes, I think you have got it.

If you have a range of checkboxes then all you can checking on the backend is “which ones have been checked?” and that is boolean depending on if isset()


if(isset($_POST['printOutstanding']) && isset($_POST['printCheckOutstanding'])): 

            foreach($_POST['printCheckOutstanding'] AS $k=>$v) : 
                echo $k; // should give you the id of checked elements
            endforeach; 

endif; 

Brilliant,

I have got it working as so:


    public function indexAction(){

        if(isset($_POST['printOutstanding']) && isset($_POST['printCheckOutstanding'])):
            foreach($_POST['printCheckOutstanding'] AS $k=>$v) :                
                $printInvoice = $this->printAction($v);
            endforeach;
        endif;
    }

$printInvoice contains HTML and php getting values from a database, so that all works brilliantly now :slight_smile:

There one more thing i need to do with this, i need to print out the value of printInvoice…

Is there a way to do this in php? I mean to physically print it out and send it to the printer?

Try this search [google]php send direct to printer[/google], if not start a new thread to garner more interest.

Will do,

Thanks Cups :wink: