currently I have a form that allows users to fill in 3 input fields. 3 of them are text fields (title, author and ISBN) and 1 of it is a select option (categories).
What I would like to achieve is to allow users to fill in any number of the 4 fields, and return the respected values. This means that if users fills in 2 input fields, there will be 2 conditions to check in the backend. 3 inputs filled means 3 conditions.
What I have currenly is this (an example, not the actual code itself):
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
}else if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
}else if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
}else if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
}else{
echo "nothing";
}
This set of code works when I only fill in one specific field. For example if I fill in only the author input, and leave the other 3 options blank, I will be returned wwith the values I want. However, if I attempt to fill in 2 or more fields at once, the returned value is incorrect.
So how else can I come up with an if else statement that will check which fields are filled, and set the conditions correctly based on the inputted fields?
Thank you all for your help in advanced! Much appreciated! :)
Changing to below code will work. The problem is when you use ELSE you are limiting your conditional statements to one result only.
Optionally, you can use switch/case statement if you find IF dirty.
But it may produce duplicates, so you need to work this out. (i dont know your code, it is just an assumption)
$atLeast1Result = false;
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if(!$atLeast1Result ) {
echo "nothing";
} else {
$returningResult = array_unique($returningResult); // this might now work on all versions, as i dont know what this array is.
}
Related
The input record includes fifteen fields named 'student01', 'student02', student03' ... 'student15'. I need to do the same thing with the value stored in each of the fields. There's got to be a better way than:
if ($student01 != '') {
// process the info in $student01
}
if ($student02 != '') {
// process the info in $student02
}
...
if ($student15 != '') {
// process the info in $student15
}
I was thinking that PHP's variable variables might be the solution, but haven't figured out the right syntax.
Help appreciated.
Thanks!
Change the number 3 in the for statement to be 1 more than the number of students you are going to have.
for ($snum = 1;$snum < 3;$snum++) {
#echo ${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)}; // left to show it works
if (${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)} != '') {
echo 'Do something';
}
}
I have a table SQL and I fill it from an Excel File. The problem is many fields are repeated. For example:
FOURNITURE MFC SERIE
FOURNITURE MFC SERIE
FOURNITURE MFC SERIE
Here in my table, I find just one line. It jumps up all the rest. However, I need to get all the lines.
My request is:
if ($articleid == 'DIEPRESTATION' || $articleid == 'DIEDIVBIEN' || $articleid == 'DIEDIVERS' ){
if(!($this->_db->query("INSERT INTO `article` (`ID_Article`, `Designiation`, `Ident`, `ID_LRU`) VALUES ('".$articleid."', '".$designation."', '".$ident."', '".$IdLRU."');"))){
if ($LRU != 'new'){
return $this->_db->query(" UPDATE `FLOOSE`.`article` SET `ID_LRU` = '".$IdLRU."' WHERE `article`.`ID_Article` = '".$articleid."' AND `article`.`Designiation` = '".$designation."' AND `article`.`Ident` = '".$ident."' LIMIT 1 ;");
} else {
return false;
}
} else{
return TRUE;
}
}
How can I change it or take a test to recover all the lines?
Thank you.
A very simple query that I cannot seem to figure out...
I'm using the CodeIgniter framework.
I'm retrieving data from my database and accessing the cols within the returned row via:
$item->available
where 'available' is the column of type int.
Now, I'd like to check whether the returned integer is 1 or not.
I believed this would be a simple case of
if ($item->available == 1) {
echo "Available";
} else {
echo "Sold";
}
}
However, this is not working. Can somebody please offer me some direction?
== will only equivelent, so true == 1 '1' == 1 etc.. to match type use === this will ensure only (int)1 === (int)1
And for consistancy, use type-casting to ensure type like so...
if ((int)$item->available === 1) {
echo "Available";
} else {
echo "Sold";
}
I hope this makes sense in relation to the title and what I'm trying to achieve, so here goes...
I have a form that displays between 1 to 30 fields to be entered - the number of fields is determined by the user at a previous stage (it will not always be the same amount).
If a user has 5 fields to fill out, they must all contain data - the same if they set 15 fields or 30 fields.
What I want to be able to do is loop through the POST variables in the form, make sure they are all set and either insert the data to the database, or display an error.
I was going to do 30 if statements with nested if statements:
if ($numberOfFields == 1){
if (!$_POST["field1_text"]){$error = 1;}
};
if ($numberOfFields == 2){
if (!$_POST["field1_text"]){$error = 1;}
if (!$_POST["field2_text"]){$error = 1;}
};
But this seems a very long winded way and I was wondering if anyone had any suggestions or pointers.
I was wondering if something like this would work:
for ($q = 1; $q <= $numberOfFields; $q ++){
if (!$_POST["field'".$q."'_text"]){
$error = 1;
}
}
But I'm getting an error referencing the variable/field name using the $q. Should this be [$q] or something else?
I'm struggling to find any answers, but probably not asking the right question, but any help would be appreciated.
Thanks
It could be done like this, using a foreach instead of a for:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$error = false;
foreach($_POST as $key => $value)
{
if(strpos($key, 'field') === 0)
{
if($value == '')
{
$error = true;
break;
}
}
}
if($error)
{
// not all fields have a value - show message
}
}
It would be much easier if you used an input array on the form, instead of manually populating the input names with a number concatenated. Example:
<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />
On the PHP side, simply loop over them:
foreach($_POST['field'] as $field)
{
if($field == '')
{
// error - doesn't have a value
}
}
Use this:
for ($q = 1; $q <= $numberOfFields; $q++){
if (!$_POST["field".$q."_text"]){
$error = 1;
}
}
In your own code you had weird extra '
I hope someone here can help me out because I'm stumped on this weird bug in my PHP code.
First, here's the relevant part of the code:
//STEP 1b - PREPROCESSING OF SUBMITTED FORM DATA (table level)
while($count_table++ != $num_tables && $submit != ""){
$table_show[$count_table] = mysql_escape_string($_POST[table_show_.$count_table]);
$ne_page[$count_table] = mysql_escape_string($_POST[ne_page_.$count_table]);
}
//STEP 2 - SUBMITTED?
if($submit!=""){
//The form has been submitted
//STEP 3 - VALIDATION
//Reset counts
$count_column = 0;
$count_table = 0;
//Check for empty fields
while($count_table++ != $num_tables){ if($table_show[$count_table] != ""){ //While there are tables, validate only if they are in included in NexEdit
This is where, in a scenario I'm testing, the if returns a false value. As a result, it should just go straight back to the while in front of it (I close the while and if at the same point further on).
if($ne_page[$count_table] == ""){
$error = "You forgot to give a NexEdit name to one or more of the tables you want to include in NexEdit.";
}
echo "Debug";
As expected, this is never echoed, because PHP never entered the if earlier on in the first place.
while($db_field[++$count_column]){ //Stay inside the loop until we run out of db fields
if($db_field[$count_column] == "" || $db_type[$count_column] == "" || $db_table[$count_column] == "" || $ne_name[$count_column] == "" || $ne_type[$count_column] == "" || $ne_order[$count_column] == ""){ //Check if all information is entered if the column is selected to be included in NexEdit
$error = "You didn't enter all required information. Required fields are indicated with (*).";
This is where the bug happens: even though PHP shouldn't have entered the if earlier on (as demonstrated with the debug echo), it still sets the value of $error here.
}
}
}}
//More code...
What am I missing? I've been looking at this for hours and even asked a friend developer for help, but I just can't find what I did wrong.
All code in one block:
//STEP 1b - PREPROCESSING OF SUBMITTED FORM DATA (table level)
while($count_table++ != $num_tables && $submit != ""){
$table_show[$count_table] = mysql_escape_string($_POST[table_show_.$count_table]);
$ne_page[$count_table] = mysql_escape_string($_POST[ne_page_.$count_table]);
}
//STEP 2 - SUBMITTED?
if($submit!=""){
//The form has been submitted
//STEP 3 - VALIDATION
//Reset counts
$count_column = 0;
$count_table = 0;
//Check for empty fields
while($count_table++ != $num_tables){ if($table_show[$count_table] != ""){ //While there are tables, validate only if they are in included in NexEdit
if($ne_page[$count_table] == ""){
$error = "You forgot to give a NexEdit name to one or more of the tables you want to include in NexEdit.";
}
echo "Debug";
while($db_field[++$count_column]){ //Stay inside the loop until we run out of db fields
if($db_field[$count_column] == "" || $db_type[$count_column] == "" || $db_table[$count_column] == "" || $ne_name[$count_column] == "" || $ne_type[$count_column] == "" || $ne_order[$count_column] == ""){ //Check if all information is entered if the column is selected to be included in NexEdit
$error = "You didn't enter all required information. Required fields are indicated with (*).";
}
}
}}
//More code...
My first thought would be a mismatched curly brace somewhere.
You say that "it should just go straight back into the while in front of it". Unless I am mistaken, it looks like that while loop closes before the if statement.
Would a more full/unchopped version of the method be possible to post? It may help diagnose
The value of $error is never cleared during the while loop(s). So, where you think it's being filled is actually the result of one of the previous iterations of the while loop.