Condition on request SQL - php

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.

Related

Advanced Search Function with PHP

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.
}

Change Request MySQL

I have a table MySQL, contains 3 parameters:
(`ID_Article`, `Designiation`, `ID_LRU`)
I fill it by a data in Excel file from PHP it contains also the same parameters:
`(`ID_Article`, `Designiation`, `ID_LRU`)`
The parsing file is work fine. the field Designiation is the description of Articles and many Articles have the same description so the field Designiation is repeated many time in my Excel file.
The problem is when I fill my table MySQL it takes just one records. For example: I have the 2 records in my Excel file:
DIEDIVBIEN DIVERSBIEN Composant
DIEDIVBIEN DIVERSBIEN Composant
Here in my table SQL I find just one line:
DIEDIVBIEN DIVERSBIEN Composant
My request is:
if ($articleid == 'DIEPRESTATION' || $articleid == 'DIEDIVBIEN' || $articleid == 'DIEDIVERS' ){
if(!($this->_db->query("INSERT INTO `article` (`ID_Article`, `Designiation`, `ID_LRU`) VALUES ('".$articleid."', '".$designation."', '".$IdLRU."');")))
{
if ($LRU != 'new'){
return $this->_db->query(" UPDATE `FLOOSE`.`article` SET `ID_LRU` = '".$IdLRU."' WHERE `article`.`ID_Article` = '".$articleid."' AND `article`.`Designiation` = '".$designation."' LIMIT 1 ;");
} else {
return false;
}
}
else{
return TRUE;
}
}
How I can change my request to take all the record in the Excel file ?

Creating a loop that checks several tables and counts the results depending on value

I'm not sure what to do when my if statement is true and how to count each hit.
My code first checks a table to get all the companies that are active.
I then want to go to each table for that company that is active, get the last entry, and count how many values for Status are either not equal to 3(!=3) or equals 2(==2).
I have no problems with this code giving me values from the row for each company but I'm not sure how to count this.
// Get a list of active companies
$sql_companyinfo="SELECT Name FROM Company_Info Where Active=True ORDER BY Name";
$result_companyinfo = mysqli_query($conn, $sql_companyinfo);
// Create variables and Loop for each active Company
while($prop_info = mysqli_fetch_assoc($result_companyinfo)) {
$prop_name = $prop_info["Name"];
$company_table = ("Company_" . "$prop_name");
// Query Table and get the last record added
$result_company = mysqli_query($conn,"SELECT * FROM $company_table WHERE ID=(SELECT MAX(ID) FROM $company_table)");
$row1 = mysqli_fetch_array($result_company);
//Status from Last Row Inserted
$Row_Status = $row1['Status'];
// This is where I get lost. For each company I need to know how many companies where $Row_Status != 3 or == 2
if ($Row_Status != '3') {
// Start with 0 and add 1 for each match
for($n3 = 0; $array[$n3]; $n3++) {
$n3_result = $n3;
}
} elseif ($Row_Status == '2') {
// Start with 0 and add 1 for each match
for($e2 = 0; $array[$e2]; $e2++) {
$e2_result = $e2;
}
{
echo "ERROR: Could not execute";
}
}
}
echo $n3_result;
echo $e2_result;
How to creating a loop that checks several tables and counts the results depending on value?
See you can do like this na:-
$Row_Status = '2';
if ($Row_Status != '3') {
echo 'ok1';
} else {
if ($Row_Status == '2') {
echo 'ok2';
} else {
echo 'ok3';
}
}

How to get which condition has failed if any in a single sql query

This might be odd or not... but I need to find out which condition out of 3 has failed if any...
I explain...
In my query I have 3 condition if all goes fine then no problem, but if one of those conditions fail I need to know which condition or conditions have fail so that I can build a proper response...
Example:
if(strlen($k) == 16 && filter_var($em, FILTER_VALIDATE_EMAIL)) {
$con = $c->Con();
$q = $con->query("SELECT COUNT(*) FROM table WHERE v = '$k' AND ema = '$em' AND ve = '0'");
$r = $q->fetch_row();
if($r > 0) { $q = $con->query("UPDATE table SET ve = '1' WHERE v = '$key' AND emails = '$em'");
if($q){
$mss = 'Ready'; // MS 1
}
} else {
$mss = 'It was ready'; // MS 2
}
} else {
$mss = 'Something is wrong!'; // MS 3
}
So the first query has 3 conditions if all are ok then go to the next "query", BUT, how do I know is one of the conditions fail?... what if [v] is not a match, or if the email is not a match or if ve is not a match... that way I can show a proper message on the MS 2.... instead of a general message...
if v is not equal to $k then $mss = 'Your key is not a match';
if emails is not equal to $ema then $mss = 'Your email was not fund';
if ve is not equal to 0 then $mss = 'Your key has no value';
the thing is that I would like to keep it as short and clean as possible, I can do a separate query for each one of the conditions which is a lazy way to do it but effective...
I can come up with a clunky solution that mostly shifts the work to PHP.
Change you query to SELECT the rows using ORs. To return the set of possible rows.
SELECT v, ema, ve FROM table WHERE v = '$k' OR ema = '$em' OR ve = '0';
Now go through this result set to check the conditions.
Pseduocode
while( $row = fetch_row($result) ){
if( 1 and 2 and 3 ){
//SUCCESS
}
elseif( NOT 1 ){
//1 is the problem
}
elseif( NOT 2 ){
//2 is the problem
}
elseif( NOT 3 ){
//3 is the problem
}
}
1,2,3 are the condition (e.g. 1 = ($row['v'] == $k))
You'll have to figure out what to do about multiple rows, ideally the schema would prevent that.

Return value in foreach loop in CodeIgniter

I am having some trouble in returning values from model to controller Using CodeIgniter. I am passing two arrays from controller to model. I am checking whether both the arrays are not empty and looping them using foreach loop to fetch some values from database and returning the query to controller. Here's my current code
if (!empty($search_for_area) && !empty($search_for_requirement))
{ foreach($search_for_requirement as $search_value_1)
{
foreach($search_for_area as $search_value_2)
{
if($search_value_2 != null && $search_value_1 != null)
{
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
print_r($nearbytution->result());
}
}
}
//Line 1
}
print_r($nearbytution->result()); works fine and i am able to view the results. Where should i put return $nearbytution; so that i can get all the fetched values? I tried it in Line 1 but i was getting only values of last array value.
function returnStuff($search_for_area,$search_for_requirement) {
$arr_area = array();
$arr_filter = array();
if ( ! empty($search_for_area) and ! empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$arr_area[] = $this->db->escape($search_value_2);
$arr_filter[] = $this->db->escape_like_str($search_value_1);
}
}
}
}
$str_area = 'NULL';
if ($arr_area)
$str_area = implode(', ', $arr_area);
$str_filter = "'^-$'";
if ($arr_filter)
$str_filter = "'(".implode('|', $arr_filter).")'";
$query = $this->db->query("
SELECT name, area, contactno
FROM tut_listing
WHERE area IN ({$str_area}) AND categoryfilter REGEXP {$str_filter} and partner > '' group by name
");
return $query->result();
}
Seriously, do consider this approach. You only need to bother the poor Mysql server with one call and you get all the data you want at the same time.
Apart from that, practice consistent style in your code. It will save both your time and your hair in the long run.
CodeIgniter escape()
MySQL REGEXP
Try this in the loop:
$nearbytution[] = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name')->result();
return $nearbytution;may then be placed at your "Line 1". It will contain an array of query results.
Unless I misunderstand your question why don't you just store all the results in a big array and then return that array?
function returnStuff($search_for_area,$search_for_requirement) {
$data = array();
if (!empty($search_for_area) && !empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
$data[] =$nearbytution->result());
}
}
}
}
return $data;
}
Now $data will be an array of results, each containing the query results. If your result set has to be cleaned up (to remove duplicates for instance) you can just loop through it and do that cleanup.
What you could also maybe do is build a large SQL query in your foreach loops and then just do that one big query and return the results.

Categories