Jump to content

limit size of array


tgavin

Recommended Posts

I have a page with tens, or hundreds of products on it. The user can only pick 2.

I'm trying to figure out a way to limit the amount of items posted to two. The code below doesn't work properly (at least, I don't think it does!), but should give an idea of what I'm looking for.

 

<?php
if(isset($_POST['submit'])) {
$items = array();
foreach($_POST['item'] as $i=>$item){
	$c=0;
	while($c <= 2) {
		$items[$i] = $_POST['item'][$i];
		$c++;
	}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="item[1]" value="1" /> lamp<br />
<input type="checkbox" name="item[2]" value="2" /> chair<br />
<input type="checkbox" name="item[3]" value="3" /> foobar<br />
<input type="submit" name="submit" value="submit" />
</form>

 

Link to comment
Share on other sites

 

<?php
if(isset($_POST['submit'])) {
$items = array();
        $itemcount = count($_POST['item']);
        if($itemcount >2) $itemcount=2;
        if($itemcount) {
              for($i=0;$i<$itemcount;$i++)
                 $items[]=$_POST['item'][$i];
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="item[]" value="1" /> lamp<br />
<input type="checkbox" name="item[]" value="2" /> chair<br />
<input type="checkbox" name="item[]" value="3" /> foobar<br />
<input type="submit" name="submit" value="submit" />
</form>

 

 

That shud work :)

It can be written smaller, but to kep things readable :)

Link to comment
Share on other sites

Thank you.

 

Something's not working correctly though. If I print_r($items) I get

Array ( [0] => [1] => 1 )

 

I haven't been able to get it to show the item id numbers in the array. It always stops at 1. Also, I think this may have show a flaw in my plan. I want to be able to take the two items and insert them into mysql.

INSERT INTO (table) VALUES ($item[$i],$item[$i])

Link to comment
Share on other sites

<?php
if(isset($_POST['submit'])) {
$items = array();
	$itemcount = count($_POST['item']);
	if($itemcount >2) $itemcount=2;
	if($itemcount) {
		  for($i=0;$i<$itemcount;$i++)
			 $items[]=$_POST['item'][$i];
	}
	if($ic=count($items)) {
		echo "$ic items<BR>";
		foreach($items as $key => $value)
			echo "$key) $value<BR>";
	} else
		echo "No Items<BR>";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="item[]" value="1" /> lamp<br />
<input type="checkbox" name="item[]" value="2" /> chair<br />
<input type="checkbox" name="item[]" value="3" /> foobar<br />
<input type="submit" name="submit" value="submit" />
</form>

 

Add some debugging info to get a better idea of whats going on :)

 

the $items array will hold 0,1 or 2 items. the item id shud come from the value of a specific checkbox (not the name).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.