check value for multiple array in loop - php

i have three array sure[],maybe[] and notify[] that are displaying in a loop like this:
sure[0] maybe[0] notify[0]
sure[1] maybe[1] notify[1]
sure[2] maybe[2] notify[2]
sure[3] maybe[3] notify[3]
And so on...
Now what I want that is in form post I get all arrays and there should be minimum one value of sure[0] or maybe[0] or notify[0] true or checked means horizontally`.
In this process will be same for all next rows and each row must contain minimum one true or checked value although can all selected or checked.
I am trying to solve this problem from past three days .
How can I do this please check it and give me idea.
thanks in advance

You can check whether at least one of the three is set by doing this:
$oneSet = $sure[0] || $maybe[0] || $notify[0];
If you want to do this in a loop, you can do something like this (assuming each list is equally long)
$oneSetInEach = true;
for( $i = 0 ; $i < count($sure) ; $i++ ) {
$thisSet = $sure[$i] || $maybe[$i] || $notify[$i]; // check if one is set here
$oneSetInEach = $oneSetInEach && $thisSet; // if all the previous ones are ok, and this is ok, we are still ok
}
// $oneSetInEach will now be true or false depending on whether each row has at least 1 element set.

If all array count() are same
<?php
$i = 0;
while ($i < count($sure)) {
if ((isset($sure[$i]) AND $sure[$i] == true) OR ( isset($maybe[$i]) AND $maybe[$i] == true) OR ( isset($notify[$i]) AND $notify[$i] == true)) {
// your code
}
$i++;
}
?>

Maybe:
$res = true;
for( $i=0 ; $i<count($sure) ; $i++ ) {
$res &= $sure[$i] || $maybe[$i] || $notify[$i];
}

Related

Getting multiple checkbox values in loop

Below is my code
if (!empty($_POST['ok'])) {
$errorMessage = array();
$loopcount = 0;
$i = 0;
foreach ($_POST['theDate'] AS $i => $theDate) {
if ($_POST['EW'][$i] == 'EW') {
$ew = "yes";
} else {
$ew = "no";
}
$i = $i + 1;
echo $ew;
}
}
its pulls the checkbox value of below and assigns it yes or now if value == ew
E/W<input name="EW[]" ID="EW[]" value="EW" type="checkbox" />
the issue is if check these
row checked
1 no
2 yes
3 no
3 yes
the out result when submitted is
row checked
1 yes
2 yes
3 no
4 no
It seems to stick anything checked as ew to the top and I don't get why here is a live working example that when submit is clicked echos with output.... all fields need to be filled but is u just add a number i will work
http://runningprofiles.com/tests/addbet.php
The checkbox is not submitted, if it does not have a value, thus, the 'yes' are the only ones in the loop. the index numbers for that field only from the submitted ones.
edit - clearification: if not checked - it its value does not get submitted.
foreach ($_POST['theDate'] AS $i => $theDate)
You missing the { at the end
like this
foreach ($_POST['theDate'] AS $i => $theDate){

if condition comes from a string variable

I am making a quiz online for my students and I have a variable
$no_of_questions = 7;
and based on that value I need to generate the condition of my IF statement
if ($row['q1'] === NULL OR $row['q2'] === NULL OR $row['q3'] === NULL OR ....)
That is php code so I can't echo and I can't put a string into IF and work as wanted. Any ideas on how to solve it?
You can simply achieve this with a for()-loop.
for($i = 1; $i <= $no_of_questions; $i++) {
if($row['q'.$i] === NULL) {
//do something if one question is NULL
}
}
From what i understand from your question. This will work.
$i=1;
if( $a['q'.$i] === NULL)

Do not save empty rows

I have a table that contains a list of all products and before each one there is a field "number" in which the user will put the quantity then click on save.
This code save all rows in data base using mutiple-insert but I want that if quantite is null or empty, so do not save this row , I want just save
$managerAchat = new AchatManager($db);
if(isset($_POST['ajouter']))
{
$size = count($_POST['quantite']);
$i = 0;
while ($i < $size)
{
if(isset($_POST['quantite'][$i]))
{
$nomProd = $_POST['nomProd'][$i] ;
$prixProd = $_POST['prixProd'][$i] ;
$quantite = $_POST['quantite'][$i] ;
$date = date('d/m/Y');
$AchatObject = new Achat(array(
'nomProd' => $nomProd ,
'prixProd' => $prixProd ,
'quantite' => $quantite ,
'date' => $date ,
)) ;
$managerAchat->insert($AchatObject);
++$i;
}
}
}
It seems you just need to change:
if(isset($_POST['quantite'][$i]))
to:
if(isset($_POST['quantite'][$i]) && $_POST['quantite'][$i] > 0)
You can also use for example array_filter() to loop only over the keys that have an amount set:
if(isset($_POST['ajouter']))
{
$has_amounts = array_filter($_POST['quantite']);
foreach (array_keys($has_amounts) as $key)
{
$nomProd = $_POST['nomProd'][$key];
// etc.
I'm not 100% sure this is correct as your question and code are not clear. But, you can check to see if $_POST['quantite'][$i] is empty (zero and null will both be considered empty) and skip them if they are:
if(isset($_POST['quantite'][$i]) && !empty($_POST['quantite'][$i]))

How to determine if variables are set in an array where dependencies exist

I have an array of data which, amongst other elements, contains a start date field, an end date field and a duration field. During validation I need to check that at least two of them are set, as you can infer the third from the two you have. Currently my if clause is as follows:
if ((!isset($data['start']) && !isset($data['duration']))
|| (!isset($data['start']) && !isset($data['end']))
|| (!isset($data['end']) && !isset($data['duration']))
It works fine, but my question is whether there is a more elegant way of addressing this issue?
The esoteric part of me is also interested as to whether there could be a function that given n fields and x minimum required would provide a valid/invalid state? And further to that whether given n fields and a set of relationships between those fields could determine whether the dependencies have been fulfilled?
This array contains many other elements that pertain to the other form fields and so unfortunately merely counting the number of elements is not a valid solution.
How about this:
$ctr = 0;
$ctr += (isset($data['start'])) ? 1 : 0;
$ctr += (isset($data['end'])) ? 1 : 0;
$ctr += (isset($data['duration'])) ? 1 : 0;
if ($ctr >= 2)
{
// do stuff
}
A function per your request, though you need to seed it:
$fields = array('start', 'end', 'duration');
$minimum = 2;
function check_fields($data, $fields, $minimum)
{
$ctr = 0;
foreach ($fields as $field)
{
$ctr += (isset($data[$field])) ? 1 : 0;
}
return ($ctr >= $minimum) ? true : false;
}
if (check_fields())
{
// do stuff
}

Algorithm in finding valid and non-duplicate entries in php

I am currently using php to help me find the valide and non-duplicated entry,
which i need
a list of valid and non-duplicated
entry a list of invalid input (unique)
a list of duplicate input
My approach is first create 5 array 2 for orginal ,1 for no mistake (empty),
1 for valid (empty) , 1 for duplicate (empty)
First using one of orginal array, for each one element : check valid
and check duplicate, if invalid , put into invalid array , and check duplicate by using inarray
after all, i get one array of invalid and duplicate , then using the orginal array, check which element is not in that two array. And job done.
My problem is, it seems quite inefficiency, how can i improve it? (Perferable if using some famous algorithm)
Thank you.
// get all duplicate input and store in an array
for ($row = 1; $row <= $highestRow; $row++) {
for ($y = 0; $y < $highestColumn; $y++) {
$val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
//use reg exp to check whether it is valid
if ($y == $mailColumn && !preg_match($pattern,$val))
{$invaild[]=$row;}
//if valid, test whether it is duplicate
elseif ($y == $mailColumn && in_array($val,$email))
{$duplicate[]=$val;
$duplicate[]=$row;}
if ($y == $mailColumn)
{$email[]=$val;
$email=array_unique($email);}
}
}
// unique invalid array since i just need invalid inputs, not the invalid + duplicate input
$invaild=array_unique($invaild);
try this:
<?php
echo "<pre>";
$array1 = array("a#b.com","c","c","d#e.com","test1","","test3","test2","test3");
$array_no_mistake = array_filter($array1,function($subject){if(trim($subject)=="") return true;});
$array_uniq = array_diff(array_unique($array1),$array_no_mistake);
$array_dups = array_diff_assoc(array_diff($array1,$array_no_mistake),$array_uniq);
$array_valid = array_filter($array_uniq,function($subject){
if (preg_match('/\A(?:[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z/i', $subject)) {
return true;
} else {
return false;
}
});
$array_invalid = array_diff_assoc($array_uniq,$array_valid);
print_r($array1);
print_r($array_no_mistake);
print_r($array_uniq);
print_r($array_dups);
print_r($array_valid);
print_r($array_invalid);
?>
1) It would seem you are only interested in the email columns so i think there is no point in iterating over all of the other columns (so the inner loop is basically redundant).
2) You can use associative arrays in order to store emails as indexes and later on efficiently look for duplicates by checking for the existence of the index/email in the array.
Here's an example:
$valid = array();
$invalid = array();
$dups = array();
for ( $row = 0; $row < $highestRow; $row++ )
{
$email = $sheet->getCellByColumnAndRow( $mailColumn, $row )->getValue();
if ( !preg_match( $pattern, $email ) )
{
$invalid[] = $row;
}
else if ( isset( $dups[ $email ] ) )
{
$dups[ $email ][] = $row;
}
else
{
$dups[ $email ] = array();
$valid[] = $row
}
}
At the end of this, $invalid will hold a list of all of the invalid rows, $dups will hold an array of arrays, each indicating the rows in which the current email is the index and its value is an array which lists the rows that share this email. If the array at a certain index is empty, the email is not duplicated.
$valid will hold the numbers of the valid rows.
Now fancy algorithm, sorry...

Categories