Jump to content

Array validation help


EchoFool

Recommended Posts

I have a array which i want to validate now i tried my initial idea but realised there was a slight flaw with it as it will still load the table even if the array is in valid which looks a bit unprofessional with a blank table with error written next to it.

 

So i need a way "before" the for loop on the array to check the array for validation then run the for loop....

 

 

foreach ($_POST['Checkbox']  as $Value => $ItemID){

 

This is the for loop, but before the loop begins i need to do another for loop before it to check the array values are numerical and not blank, and a query is needed to check against the database that the values match the user's account.. i can make the query and validation no problem, but how can i for loop to check this before doing the next for loop which involves the table...won't the position of the array be at the end rather than the beginning when i start the second for loop on the array?

Link to comment
https://forums.phpfreaks.com/topic/93743-array-validation-help/
Share on other sites

Not with a foreach loop. It is perfectly fine to run it twice. You can also do this though:

 

<?php
$output = "";
$error = false;
foreach ($_POST['Checkbox']  as $Value => $ItemID){
  // Validate here
  if(/*doesn't validate*/){
    $error = "This is the error I found";
    break;
  }
  // All good, now add to $output
  $output .= "This is the stuff you normally print, but instead we will add it to a variable";
}
if($error){
  //Do error handling
}else{
  print $output;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/93743-array-validation-help/#findComment-480349
Share on other sites

Not with a foreach loop. It is perfectly fine to run it twice. You can also do this though:

 

<?php
$output = "";
$error = false;
foreach ($_POST['Checkbox']  as $Value => $ItemID){
  // Validate here
  if(/*doesn't validate*/){
    $error = "This is the error I found";
    break;
  }
  // All good, now add to $output
  $output .= "This is the stuff you normally print, but instead we will add it to a variable";
}
if($error){
  //Do error handling
}else{
  print $output;
}
?>

 

I already have this idea but before the for loop begins i got:

 

<div class=tablecenter>
	<table width="500" border="1" cellspacing="0" cellpadding="0" align="center">

<tr>
		<td width="200" align="center" class="blackBold">Item Name</td>
		<td width="200" align="center" class="blackBold">Quantity To Send</td>
		<td width="200" align="center" class="blackBold">Remove Amount</td>
		<td width="200" align="center" class="blackBold">Remove Quantity</td>
</tr>
<?php			
foreach ($_POST['Checkbox']  as $Value => $ItemID){

 

if i was to get an error in the for loop it would error and show the table above which doesn't look too neat this is why i needed to seperate.

Link to comment
https://forums.phpfreaks.com/topic/93743-array-validation-help/#findComment-480355
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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