An uncaught Exception was encountered php codeigniter - php

I want to array value push another single array value, how the single data push another array.
This my below code:
$this->db->select('emp_info.empid');
$this->db->from('emp_info');
$this->db->where("emp_info.empid", 2);
$this->db->where("emp_info.comcod", $compcod);
$this->db->where("emp_info.status", 1);
$empdata = $this->db->get();
$data_day = array();
$Dated1 = $this->model_employes->getDatesFromRange($fdate, $tdate);
foreach ($empdata->result() as $emp) {
foreach ($Dated1 as $key => $day) {
$dayid = date("Ymd", strtotime($day));
$data_day[] =
array(
'empid' => $emp->empid,
'shortcode' => '',
'attdate' => $day,
'dayid' => $dayid
);
}
}
$m_dayid = array();
foreach ($data_day() as $monthday) {
array_push($data_day, $monthday->dayid);
}
echo "<pre>";
print_r($m_dayid());
echo "</pre>";

Change the code as follows Instead of $data_day() into $data_day and $m_dayid() into $m_dayid
<?php
$m_dayid = array();
foreach ($data_day as $monthday) {
array_push($data_day, $monthday->dayid);
}
echo "<pre>";
print_r($m_dayid);
echo "</pre>";
?>

Related

I want to insert data into a table using loop I have Multi -D array using PHP

**
data did not insert into table, please help
I am not understand
which loop to use
I want to insert data into a table using loop I have Multi -D array
using PHP
**
<?php
$data = array('eid' => array(124,658,457,145),
'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
'name' => array('Chiku','Lipu','Babu','Dipu')
);
foreach($data as $key => $value){ ///////////
$k = $key;
$v = $value;
echo "<pre>";
print_r ($k);
echo "<br>";
foreach($v as $ke => $va){ /////////////
//echo "<pre>";
//print_r ($va);
$ke;
$va;
//echo "<pre>";
print_r ($va);
}
$count = count($va);
echo "<br>";
echo $count;
}
echo "<pre>";
//print_r ($k);
//echo "<pre>";
//print_r ($v);
$k = $k;
$v = implode('',$v);
//echo $v;
$con = mysqli_connect('localhost','root','','loop');
$sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($v)";
$exe = mysqli_query($con,$sql);
?>
it's not a best way to do this, but still, you can try following
$data = array('eid' => array(124,658,457,145),
'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
'name' => array('Chiku','Lipu','Babu','Dipu')
);
if (isset($data['eid']) && is_array($data['eid']) && sizeof($data['eid']) > 0)
{
$con = mysqli_connect('localhost','root','','loop');
foreach ($data['eid'] as $key => $value)
{
if (isset($data['data']) && isset($data['data'][$key]) && isset($data['name']) && isset($data['name'][$key]) )
{
$sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($value,$data['data'][$key],$data['name'][$key])";
$exe = mysqli_query($con,$sql);
}
}
}

While loop and match records in one foreach with another

Here is the code that I will be using:
$wp_query = new WP_Query([
'post_type' => 'office',
'post_status' => 'any',
'posts_per_page' => -1,
]);
$offices = [];
if (count($wp_query->posts) > 0) {
$offices = $wp_query->posts;
}
$settings['started'] = time();
foreach ($offices as $office) {
$key['_office_id'] = get_post_meta($office->ID, '_office_id', true);
}
foreach ($records as $record) {
$record_key = strtoupper(str_replace(' ', '',
trim($record->location_id) . trim($record->business_unit)));
}
$key outputs the following:
Array
(
[_office_id] => ATLANTAFHUSA
)
Array
(
[_office_id] => AUSTINFHUSA
)
$record_key outputs the following:
ATLANTAFHUSA
AUSTINFHUSA
Here is what I'm trying to achieve .. I want to create a while loop inside the records foreach that will look at the $record_key and if there is a match with $key['_office_id'] to go ahead and echo that out.
What I attempted inside the $records foreach:
while ($record_key == $key['_office_id']) {
if ($record_key == $key['_office_id']) {
echo $record_key . " has a matching record.";
} else {
echo $record_key . " does not have a matching record.";
}
}
$keys = [];
// Store all office ids
foreach ($offices as $office) {
$keys[] = get_post_meta($office->ID, '_office_id', true);
}
foreach ($records as $record) {
$record_key = strtoupper(str_replace(' ', '',
trim($record->location_id) . trim($record->business_unit)));
// Check if current `record_key` is in `$keys` array
if (in_array($record_key, $keys)) {
echo 'Record key is in keys';
} else {
// do something else
}
}

How can I get array of workers ? Can't find my mistake i am newbee

Can't decode JSON to foreach
Need help
<?php
$workers = 'https://api.ethermine.org/miner/***/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
foreach ($workersJson['data']['worker'] as $worker) {
$output = $worker['worker'][1];
echo $output;
}
?>
How can I get array of workers ? Can't find my mistake i am newbee ☺
When visiting the url, I can see the structure is this :
{"status":"OK","data":[{"worker":"gtx1050tix4","time":1520599200,"lastSeen":1520599074,"reportedHashrate":45518082,"currentHashrate":43333333.333333336,"validShares":39,"invalidShares":0,"staleShares":0,"averageHashrate":43559413.580246896}]}
If you want to get gtx1050tix4 then the answer is
foreach ($workersJson['data']['worker'] as $worker) {
$output = $worker;
echo $output;
}
And if you want to get the whole data and output from there, the answer is
foreach ($workersJson['data'] as $data) {
$worker = $data['worker'];
$time = $data['time']; // any key from json
echo $worker;
}
Hopefully this clears stuff up for you
PLease try this:
<?php
$workers = 'https://api.ethermine.org/miner/YOURAPIKEY/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
//print_r($workersJson);
foreach ($workersJson['data'] as $worker) {
$output = $worker['worker'];
echo $output;
}
?>
You can do something like:
$workers = 'https://api.ethermine.org/miner/YOURAPIKEY/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
foreach ($workersJson['data'] as $worker) {
//$worker["worker"] <-- accessing gtx1050tix4
//$worker["time"] <-- accessing 1520599200
//etc
echo "<pre>";
print_r( $worker );
echo "</pre>";
}
This will result to:
Array
(
[worker] => gtx1050tix4
[time] => 1520599200
[lastSeen] => 1520599074
[reportedHashrate] => 45518082
[currentHashrate] => 43333333.333333
[validShares] => 39
[invalidShares] => 0
[staleShares] => 0
[averageHashrate] => 43559413.580247
)

Looping through a JSON in PHP

I'm trying to loop through a JSON that I post to my PHP backend. The JSON looks like this:
[
{
"number":"5613106"
},
{
"number":"56131064"
},
{
"number":"56131063"
}
]
I post it from Postman like so:
I want to be able to print each number that I've posted individually using
echo $number;
Right now when I use the following it just prints the last number:
$number = $json['number'];
echo $number;
My function:
public function check_users_post()
{
$json = $this->request->body;
print_r($json);
$this->response($json, REST_Controller::HTTP_OK);
}
The output:
Array
(
[0] => Array
(
[number] => 5613106
)
[1] => Array
(
[number] => 56131064
)
[2] => Array
(
[number] => 56131063
)
)
Hope this generic iterator will help you.
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n";
} else {
echo "$key => $val\n";
}
}
Use
json_decode()
PHP built in function for decoding json back to PHP variable or array and then loop through each index and print number.
Got it done like this:
public function check_users_post()
{
$json = $this->request->body;
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($json),
RecursiveIteratorIterator::SELF_FIRST);
$phone_numbers = "";
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
} else {
$phone_numbers = "$phone_numbers" . ", " . "$val";
}
}
$phone_numbers = substr($phone_numbers, 2);
$phone_numbers = "(" . $phone_numbers . ")";
$query = $this->db->query("SELECT * FROM users WHERE user_number in $phone_numbers;");
$result = $query->result();
$this->response($result, REST_Controller::HTTP_OK);
if (mysql_num_rows($result)==0) {
$data = [ 'message' => 'No users returned'];
$this->response($data, REST_Controller::HTTP_BAD_REQUEST);
} else {
$this->response($result, REST_Controller::HTTP_OK);
}
}
Description
First stringify your JSON, than use JSON_DECODE() function which gives you the array afterwards iterate on this array using foreach loop which will give you the each key which you want to echo.
Code
$str = '[
{
"number":"5613106"
},
{
"number":"56131064"
},
{
"number":"56131063"
}
]';
$array = json_decode($str, true);
echo "<pre>";
foreach ($array as $key => $key_value) { // then loop through it
echo "<br>";
echo $array[$key]['number'];
echo "<br>";
}
Output
5613106
56131064
56131063

array result using foreach statement display in json in php

I am getting the result from web service SOAP client. I have created an array to get the result and display it using json format. I am getting few of my results properly. I have SerialEquipment parameter which is array and i need to get the result using foreach loop. I am doing an mistake there. I dont know how can i assign my $vehiclResult array in this for each statement. So that all the results at last i will collect and display using json using vehicleResult array.My mistake is in the foreach loop.
structure for SerialEquipment parameters:
Code:
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$vehiclResult = array(
'WE_Number' => $vehicle['WE Number'] ."<br>",
'Vehicle Type'=> $vehicle['Vehicle Type'] . "<br>",
'HSN' => $vehicle['HSN'] . "<br>",
'TSN' => $vehicle['TSN'] . "<br>"
);
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['SerialEquipment'] = $key. "<br>";
$vehiclResult[$key]['Code'] = $obj->Code. "<br>";
$vehiclResult[$key]['Desc Short'] = $obj->Desc_Short. "<br>";
$vehiclResult[$key]['Desc Long'] = $obj->Desc_Long. "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc). "<br>";
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc). "<br>";
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
}
You need to check if your array have the key so:
if(!isset($vehiclResult[$key]))
if not, you need to create it:
$vehiclResult[$key] = array(); // as an array
Also, you don't really need to make a description of your "item". You can Parse your JSON on the result page to output some text.
You can do something like.
Do something like:
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['serial'] = $key;
$vehiclResult[$key]['code'] = $obj->Code;
$vehiclResult[$key]['short_desc'] = $obj->Desc_Short;
$vehiclResult[$key]['long_desc'] = $obj->Desc_Long;
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2;
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode;
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc;
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode;
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc);
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode;
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc);
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
If you would keep your "text" and your <br> code, do the samething but add what you want to output after the "="
EDIT
** A HAVE CHANGE THE CODE PREVIOUSLY..
if you want to test your $vehiclResult, try something like:
foreach($vehiclResult as $key=>$value){
if(!is_array($value))
var_dump($value);
else {
foreach($value as $key2=>$value2){
if(!is_array($value2))
var_dump($value2);
else {
foreach($value2 as $key3=>$value3){
var_dump($value3);
}
}
}
}

Categories