tgavin Posted March 7, 2008 Share Posted March 7, 2008 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 https://forums.phpfreaks.com/topic/94901-limit-size-of-array/ Share on other sites More sharing options...
laffin Posted March 7, 2008 Share Posted March 7, 2008 <?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 https://forums.phpfreaks.com/topic/94901-limit-size-of-array/#findComment-486129 Share on other sites More sharing options...
tgavin Posted March 7, 2008 Author Share Posted March 7, 2008 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 https://forums.phpfreaks.com/topic/94901-limit-size-of-array/#findComment-486181 Share on other sites More sharing options...
laffin Posted March 7, 2008 Share Posted March 7, 2008 <?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 https://forums.phpfreaks.com/topic/94901-limit-size-of-array/#findComment-486197 Share on other sites More sharing options...
tgavin Posted March 7, 2008 Author Share Posted March 7, 2008 Oops. I still had the id number in the checkbox name's brackets. name="item[2]" thanks for everything! Link to comment https://forums.phpfreaks.com/topic/94901-limit-size-of-array/#findComment-486249 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.