Inserting array into database and making sure the qty is greater than 0

Hi all,

I am developing a shop system and at the moment i am wanting for the user to select how many of each item they want to order,

now on my form submission i want it to make sure they have entered is greater than 0 meaning it will loop through each of the quanties so for example it will be like this

Attack Items
Name price Strength Buy
Broken Stick $100 0
Knife $1,000 1
Staff $2,000 2
Long Sword $5,000 5
Lance $10,000 10
Broadsword $25,000 3 0
Steed $50,000 6 4
Excalibur $200,000 25 6
Chariot $450,000 600
Blackpowder Missile $1,000,000 1,000 1

So Knife would be 0 for qaunty and all the others the same,

not sure on how i can make sure there r no 0’s selected on the quanty box,

So how can i do that this is my php code this is what i had.


$by=$_POST['buy'];
		if($by>0)
		{
			//
			foreach ($by as $f)
			{
				echo $f."<br />";
			}
			//
		}

So basically what i am wanting is to have any of those forms field having any number that is not 0 but of course when i print out $f its including the 0’s so when i print out $f it should only print out the ones i have placed a number from 1 to 9 only expection is if the number is in this format 1,000 or 1,000,000 then that would be allowed?

How can achieve this?

Thanks,William

You just want to not print lines where $f is 0?

if (!empty($f) && $f > 0) {
  echo $f . "<br />";
}

u would surely need to iterate the items to do some proccess ( database, session … etc ) u would check first if


foreach (... AS $f ){
if ($f > 0){
//do process
}
}

hint : u could check by javascript before submitting the form that all values greater than 0 like :


<script type="text/javascript">
function checkFields(){
var flds = document.getElementById('buy');
for(var i=0; i<flds.length(); i++){
if (flds[i].value <= 0) return false;
}
}
</script>
<form ... onSubmit="return checkFields();">
....
</form>

<< fast code not tested