sum values from db with array_sum() stuck at 3 loop - loops

after couple of days i finally got some values from 1st and 2nd line but after that at 4th line i got stacked and nothing works.
in need to get data from db where:
obrat | memb | tab3. | history
| member1 | 0. | 10
200 | member2 | member1. | 3
100 | member3 | member1. | 4
so if i will select from tab3 users which have my memb i need to sum their obrat and history. And doo the same think with them - select from tab3 where names are member1 and member2 and sum that.
my code:
while ($row = $resulto->fetch_assoc()) {
$memb2 = $row['memb'];
// second line member sel.
$resulto2 = $mysqli->query("SELECT * FROM users WHERE tab3='$memb2' ") or die($mysqli->error());
//pokud druhá linie
if ($resulto2->num_rows > 0) {
while ($row2 = $resulto2->fetch_assoc()) {
$memb3 = $row2['memb'];
} // should be 3. line
// get numbers from 2. line
$resulto2 = $mysqli->query("SELECT * FROM users WHERE tab3='$memb2' ") or die($mysqli->error());
$d = 0;
$rows2 = array();
$rows3c = array();
while ($rowd = $resulto2->fetch_array()) {
array_push($rows2, $rowd['obrat']);
array_push($rows3c, $rowd['history']);
}
$array2 = array_sum($rows2);
$count3 = array_sum($rows3c);
}
}
//end / get values first line
$resulto = $mysqli->query("SELECT * FROM users WHERE tab3='$memb' ") or die($mysqli->error());
$d = 0;
$rows = array();
$rows2c = array();
while ($rowd = $resulto->fetch_array()) {
array_push($rows, $rowd['obrat']);
array_push($rows2c, $rowd['history']);
}
$array = array_sum($rows);
$count2 = array_sum($rows2c);
}
some idea where is the problem?

or next way iam trying to :
$resulto2 = $mysqli->query("SELECT * FROM users WHERE tab3='$memb' ") or die($mysqli->error());
if ($resulto2->num_rows > 0) {
while ($row2 = $resulto2->fetch_assoc()) {
$memb2 = $row2['memb'];
$resulto2a = $mysqli->query("SELECT * FROM users WHERE tab3='$memb2' ") or die($mysqli->error());
while ($row2a = $resulto2a->fetch_assoc()) {
$memb3 = " OR memb='".$row2a['memb']."'";
//here i need to get one value which is sum from all users selecting
}
}

Related

Mysqli Insert Inside While Loop and For loop

I am stuck in this code, the problem is that I am trying to insert values inside the while loop and For loop, I have 584 Records and i need to insert 15 records into another table
<?PHP
$payroll_date = '2020-01-15';
$date_from = '2019-12-21';
$date_to = '2020-01-05';
$query = "SELECT * FROM adb_crosschex GROUP BY userid";
$result = mysqli_query($db, $query);
if ($result)
{
$row = mysqli_num_rows($result);
/* ######################################################################### */
$x = 0;
$sqltoDTR = "SELECT * FROM adb_crosschex GROUP BY userid";
$dtrResult = mysqli_query($db, $sqltoDTR);
while($rows = mysqli_fetch_array($dtrResult))
{
$x++;
$begin = new DateTime($date_from);
$end = new DateTime($date_to);
$pay_period = date("m-d", strtotime($date_from))." - ".date("m-d", strtotime($date_to));
$idcode = $rows['idcode'];
$controlno = $rows['userid'];
$acctname = $rows['acctname'];
$transdate = $rows['transdate'];
$branch = $rows['branch'];
for($i = $begin; $i <= $end; $i->modify('+1 day')) {
$date_trans = $i->format("Y-m-d");
/* result
2019-12-21
2019-12-22
2019-12-23
2019-12-24
....... until reaching the $end
*/
/* mysql Insert goes here */
}
}
/* ######################################################################### */
mysqli_free_result($result);
}
mysqli_close($db);
?>
the problem with the code is that it inserted data like forever i tried to leave it for 30 minutes and i get like 1 Million plus into my table hahahaha I supposed to get 584 x 15 = 8760 records Please Help
Ok i think i solve it for the code
for($i = $begin; $i <= $end; $i->modify('+1 day')) {
$date_trans = $i->format("Y-m-d");
myQuery($idcode, $payroll_date, $pay_period, $branch, $controlno, $date_trans, $acctname);
}
i try to put the insert query outside the loop and it goes like this
function myQuery($idcode, $payroll_date, $pay_period, $branch, $controlno, $date_trans, $acctname)
{
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "INSERT INTO `tbl_dtr`(idcode,payroll_date,pay_period,branch,controlno,date_trans,acctname)VALUES('$idcode', '$payroll_date', '$pay_period', '$branch', '$controlno', '$date_trans','$acctname')";
if ($db->query($sql) === TRUE) { echo "-<br>"; } else { echo $db->error; }
}

PowerShell New Pipline variable with String/ArrayValue in "{}"

I need a solution to create an array in a certain format.
Background I export a SharePoint list and get my fields from an XML file.
The command (via PnP) to export is as follows:
$AllSPListItems = Get-PnPListItem $SharePointListName
$ItemsSelection = #{ L = "ID"; E = { $_["ID"] } },#{ L = "SP_SiteURL"; E = { $_["SP_SiteURL"] } }
$AllSPListItems | Select $ItemsSelection | Export-Csv -Path "C:\Temp\XYZ.csv"
That works, but here's what I want:
$ItemsSelection = #{ L = "$FieldDisplayName[0]"; E = { $_[$FieldURLName[0]] } },#{ L = "$FieldDisplayName[1]"; E = { $_[$FieldURLName[1]] } } }
I get the $FieldDisplayName and $FieldURLName variables from my XML.
Unfortunately I only get the following output:
#{ L = "ID"; E = { $_[$FieldURLName[0]] } },#{ L = "SP_SiteURL"; E = { $_[$FieldURLName[1]] } } }
Name Value
---- -----
E $FieldURLName[0]
L ID
E $FieldURLName[1]
L SP_SiteURL
So how can I get the value of "E", not the text?
The variables does not resolves.
Thanks a lot!
So how can I get the value of "E", not the text?
You get the text of the E scriptblock because it hasn't executed yet - that only happens once you use it with Select-Object.
To get the correct label names, remove the " quotes around the label expression:
$ItemsSelection = #{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } },#{ L = $FieldDisplayName[1]; E = { $_[$FieldURLName[1]] } } }
$AllSPListItems | Select $ItemsSelection | Export-Csv -Path "C:\Temp\XYZ.csv"
Ok i have a solution, is unattractive but apparently there is no other possibility.
I use the switch Case:
switch ($FieldDisplayName.Length){
1 {$items | Select #{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } } | Export-Csv -Path "C:\install\ExportV1.csv"}
2 {$items | Select #{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } },#{ L = $FieldDisplayName[1]; E = { $_[$FieldURLName[1]] } } | Export-Csv -Path "C:\install\ExportV1.csv"}
}

Get multiple values from a function in yii2

I have a function in my Model which is passed a single value and in which I have a query which is giving me two results.
public static function updImgtbl($id)
{
$total_images = 0;
$images_uploaded = 0;
$sqlquery = "SELECT SUM(CASE WHEN ims.`installation_id` = $id THEN 1 ELSE 0 END) AS total_images,
SUM(CASE WHEN ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id THEN 1 ELSE 0 END) AS images_uploaded
FROM `installation_images_site` ims
INNER JOIN `installations` ins ON ins.`id` = ims.`installation_id`
WHERE (ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id)
OR (ims.`installation_id` = $id)
GROUP BY ims.`installation_id`";
$pt = Yii::$app->db->createCommand($sqlquery)->queryAll();
foreach ($pt as $val)
{
$total_images= $val['total_images'];
$images_uploaded = $val['images_uploaded'];
}
return ["total_images" => $total_images, "images_uploaded"=>$images_uploaded];
}
This function is called from an API
$response = Installations::updImgtbl($install_id);
print_r($response);
exit();
The response is below
Array
(
[total_images] => 4
[images_uploaded] => 1
)
But I want to get these values in a variable in my API. How can I achieve it?
You can simply:
list($total_images, $images_uploaded) = array_values($response);
Then, you have total_images value in $total_images variable and images_uploaded in $images_uploaded variable.
I mean:
list($total_images, $images_uploaded) = Installations::updImgtbl($install_id);
echo $total_images; // 4
echo $images_uploaded; // 1
You can read more about list in official doc.
Another way is using extract() function. For example:
$response = Installations::updImgtbl($install_id);
extract($response);
echo $total_images; // 4
echo $images_uploaded; // 1

extracting Array data from SharedObject

Im having a game where after I finish a certain level I put that level`s name In an Array and save that array to an SharedObject(SO). But somehow/somewhere I mess up things, because after closing and opening the game again, I load the SO data (to unlock the levels that I have passed) and it is (the loaded data) always messed up and not loading what it should load(sometimes it even trows some errors at me.)
The code is not that difficult but I have been looking at it for far to long and I need new eyes to see the obvious mistakes in it.
***********EDIT**********
So the code for the SO is this
public class LevelsData extends SharedObject
{
public static var LEVELS_DATA:SharedObject = SharedObject.getLocal("save_poi71");
public static function get LoadNamesOfLevelPassed():Array
{
var myLevelsArr:Array = [];
if( LEVELS_DATA.data.PlayedLevels_arr == undefined)
{
myLevelsArr.length = 0;
}
else
{
myLevelsArr = LEVELS_DATA.data.PlayedLevels_arr;
}
return myLevelsArr;
}
public static function set SaveLevel_Names( nameOfLevelPassed:Array ):void
{
LEVELS_DATA.data.PlayedLevels_arr = nameOfLevelPassed;
LEVELS_DATA.flush();
}
}
and the code in the game is this
.
.
.
.
private function didWePlaySameLevel():void
{
var namesPlayed_arr:Array = LevelsData.LoadNamesOfLevelPassed;
var namesTotal:int = namesPlayed_arr.length;
if( namesTotal == 0 )
{
trace(" ScreenInGame_2 | didWePlaySameLevel | namesTotal == 0 so we push Level_1");
namesPlayed_arr.push( Moving_Picture_Name );
LevelsData.SaveLevel_Names = namesPlayed_arr;
sameLevel = false;
}
else if( namesTotal != 0 )
{
for( var i:int = 0; i < namesTotal; i++)
{
trace(" ScreenInGame_2 | didWePlaySameLevel | " + Moving_Picture_Name + " ?==? " + namesPlayed_arr[ i ] );
trace(" ScreenInGame_2 | didWePlaySameLevel | namesPlayed_arr.length = " + namesPlayed_arr.length);
if( Moving_Picture_Name != namesPlayed_arr[ i ] )
{
trace(" ScreenInGame_2 | didWePlaySameLevel | " + Moving_Picture_Name + " != " + namesPlayed_arr[ i ] );
namesPlayed_arr.push( Moving_Picture_Name );
LevelsData.SaveLevel_Names = namesPlayed_arr;
sameLevel = false;
trace(" ScreenInGame_2 | didWePlaySameLevel | namesPlayed_arr.length = " + namesPlayed_arr.length);
}
}
}
}
.
.
.
if( levelsToUnlock < totalLevels && sameLevel == false )
{
trace("ScreenInGame | unlockOneMoreLevel | we UNLOCK 1 more level" );
unlockNextLevel();
}
***EDIT PROBLEM:
SomeHow i figured a way of solving my 1st problem, but now there is a new one and that is :
When the Array from the SO is != 0 i go into the else if statement. There i compare if any of the variables in the Array have the same name of the Moving_Picture_Name and if they dont then I set the sameLevel = false. The problem is that, if lets say I have passed/played 5 levels before( Level_1,Level_2,Level_3,Level_4,Level_5 ) and lets say I start playing Level_2 again it would show me this:
Level_2 != Level_1 - > and it will push itself into the Array and update the data
Level_2 == Level_2 - > it will do nothing
Level_2 != Level_3 - > and it will push itself into the Array and update the data
Level_2 != Level_4 - > and it will push itself into the Array and update the data
Level_2 != Level_5 - > and it will push itself into the Array and update the data
so it will actually push 4 Elements in the Array that exist already in that Array.
***THE QUESTION :
So how do I push the newLevel only if its name isnt in that Array(the one where I save the names), And how do I do nothing if the name exist in the Array already.
You may have to convert your array to ByteArray, and save that. When you read it back, you will have to do the reverse operation. StackOverflow example link

How to return value of 2 data in foreach which each data has 5 rows in model of codeigniter to view

I have a similar problem that the function only return the first loop data.
The result should be 2 loops and each loop has rows data.
For example, The first loop is A and has 5 rows data and second loop is B and has 5 rows.
The return value only A with 5 rows, it sould be 10 rows. 5 rows A + 5 rows B.
How is the code should be?
This function in model is bellow
function getDataForm($data_search)
{
$i=0;
if(!empty($data_search['arr_idacl']))
{
foreach($data_search['arr_idacl'] as $idx => $val)
{
if(!empty($operatingHour))
{
$sql = "SELECT be_acl_resources.description as description,DATE_FORMAT(tbl_camera_detail.start_time,'%d %M %Y ') as datadate, SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters*(-1),0))+SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits*(-1),0)) as total_enter FROM tbl_consolid JOIN tbl_consolid_detail ON tbl_consolid.id = tbl_consolid_detail.consolid_id JOIN tbl_config_data ON tbl_consolid_detail.config_data_id = tbl_config_data.id JOIN tbl_camera ON tbl_config_data.id_acl = tbl_camera.id_acl JOIN tbl_camera_detail ON tbl_camera.id = tbl_camera_detail.camera_id JOIN be_acl_resources ON tbl_consolid.id_acl = be_acl_resources.id WHERE tbl_camera.date >= '".$data_search['start_date']."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') >= '".$startOpratingHour."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') <= '".$operatingHour['finish_time']."' AND tbl_camera.date <= '".$data_search['end_date']."' AND tbl_consolid.id_acl = '".$val."' GROUP BY tbl_camera.date";
echo $sql;
$query = $this->db->query($sql);
$result_data[] = $query->result();
//return $result;
}
$i++;
}
return $result_data;
}
}
Declare $result_data = array(); just before if(!empty($data_search['arr_idacl'])) condition.
Replace $result_data[] = $query->result(); with $result_data[$i] = $query->result();
Update:
Just close to the answer. :) Try this:
Replace $result_data[] = $query->result(); with $result_data[$i][] = $query->result();

Resources