Foreach loop not display result in Codeigniter View - php

I'm trying get multiple records of attendance comment by studentIDs, and ClassID but when i print Array
Array ( [0] => Array ( [0] => stdClass Object ( [attendanceID] => 89 [comment] => This is Yusra ) ) [1] => Array ( [0] => stdClass Object ( [attendanceID] => 90 [comment] => this is zara butt ) ) [2] => Array ( [0] => stdClass Object ( [attendanceID] => 91 [comment] => this is ibrahim ) ) [3] => Array ( [0] => stdClass Object ( [attendanceID] => 92 [comment] => comment ) ) [4] => Array ( [0] => stdClass Object ( [attendanceID] => 93 [comment] => ) ) [5] => Array ( [0] => stdClass Object ( [attendanceID] => 94 [comment] => ) ) [6] => Array ( [0] => stdClass Object ( [attendanceID] => 95 [comment] => ) ) [7] => Array ( [0] => stdClass Object ( [attendanceID] => 96 [comment] => ) ) )
but i print in view using foreach loop nothing show.. don't know where i'm doing wrong
Controllor
$studentID = []; // Multiple Student ids
$comments = [];
$this->data['set'] = $id; //class id
$this->data['students'] = $this->student_m->get_order_by_student(array('classesID' => $id, 'schoolyearID' => $schoolyearID));
foreach($this->data['students'] as $key => $val){
$studentID[] = $val->studentID; //
}
foreach ($studentID as $student) {
$comments[] = $this->sattendance_m->get_comment($id,$student);
}
$this->data['comments']= $comments;
print_r($comments);
Model
function get_comment($id,$student) {
$this->db->select('attendanceID,comment');
$this->db->where('classesID', $id);
$this->db->where_in('studentID', $student);
$this->db->order_by($this->_primary_key,"desc");
$this->db->limit(1);
$this->db->from($this->_table_name);
$query=$this->db->get();
return $query->result();
}
View
<?php
if(count($comments)) {
foreach ($comments as $key => $row) {
echo $row->comment;
}
}?>

As you can see output of print_r you have multiple comments per student. So you need to do inner loop again
if(count($comments))
{
foreach ($comments as $key => $row) {
foreach ($row as $value) {
echo $value->comment;
}
}
}

this should be
<?php
$i=0;
if($comments)
{
foreach ($comments as $key => $row) {
echo $key[$i++]->row
}
}else{
echo "null";
}
?>

Related

convert nested foreach tree into recursive php function

Need to make recursive function from the below code. tried multiple ways but failed. because I don't know how much depth will the tree have. depth may be varying according to the data.
$tot_terms = [];
foreach ($tree as $key => $val) {
$tot_terms[$val->depth][] = $val->tid;
if (!empty($val->children)) {
foreach ($val->children as $key1 => $val1) {
$tot_terms[$val1->depth][] = $val1->tid;
if ($val1->children) {
foreach ($val1->children as $key2 => $val2) {
$tot_terms[$val2->depth][] = $val2->tid;
if ($val2->children) {
foreach ($val2->children as $key3 => $val3) {
$tot_terms[$val3->depth][] = $val3->tid;
if ($val3->children) {
foreach ($val3->children as $key4 => $val4) {
$tot_terms[$val4->depth][] = $val4->tid;
if ($val4->children) {
foreach ($val4->children as $key5 => $val5) {
$tot_terms[$val5->depth][] = $val5->tid;
}
}
}
}
}
}
}
}
}
}
}
and the result should be like this
Array
(
[0] => Array
(
[0] => 20011
[1] => 19991
[2] => 20000
)
[1] => Array
(
[0] => 20010
[1] => 19995
[2] => 19994
[3] => 19990
[4] => 20005
[5] => 19985
[6] => 19999
[7] => 19998
)
[2] => Array
(
[0] => 20012
[1] => 20006
[2] => 19996
[3] => 19993
[4] => 19989
[5] => 19988
[6] => 20004
[7] => 19984
[8] => 20001
)
[3] => Array
(
[0] => 20007
[1] => 20009
[2] => 20008
[3] => 19992
[4] => 19987
[5] => 19986
[6] => 19983
[7] => 19982
[8] => 20003
[9] => 20002
)
[4] => Array
(
[0] => 19997
)
[5] => Array
(
[0] => 19981
)
)
Tried according to other post but it is failing. input $tree is something that is array and an object. will post in next comment if required as total body count is more than its expected
We can use a handler function to store data. Note that we are passing the array by reference.
function func( $item, &$data ) {
if ( is_empty( $item ) ) return;
if ( ! is_object( $item ) ) return;
foreach ($item as $key => $val) {
$data[$val->depth][] = $val->tid;
func( $val->children, $data );
}
}
function func_handler( $item ) {
$data = [];
func( $item, $data );
return $data;
}

PHP Soap Request foreach

I have a Souap Request and I do not know how I can go through all VoucherCodeItems with foreach.
I tried this, but it dosn't work:
foreach($response->VoucherCodeCollection->VoucherCodeItem AS $key => $val) {
echo "Feld $key hat den Wert: $val<br>";
}
This is what i get when make a print like:
print_r($response);
Result
stdClass Object
(
[TotalResults] => 2
[VoucherCodeCollection] => stdClass Object
(
[VoucherCodeItem] => Array
(
[0] => stdClass Object
(
[Id] => 215523
[ProgramId] => 6767
[Code] =>
[Title] => Adventskalender
[Description] => Im Adventskalender
)
[1] => stdClass Object
(
[Id] => 215453
[ProgramId] => 8476
[Code] =>
[Title] => Wir schenken dir 15 EUR!!
[Description] =>
)
)
)
)
You can't directly echo out the object:
Try:
foreach ($response->VoucherCodeCollection->VoucherCodeItem as $key => $val) {
echo "Feld $key hat den Wert: {$val->Title}<br>";
}

PHP array values into key values

I have below array
<?php
echo "<pre>";
print_r($row);
?>
Array
(
[0] => Array
(
[contact] => ugin
)
[1] => Array
(
[contact] => gsGR
)
[2] => Array
(
[addtnlcontact] => ghjey
)
)
Array
(
[0] => Array
(
[relation] => uncle
)
[1] => Array
(
[relation] => mrsyjsrky
)
[2] => Array
(
[relation] => cghjrtdj
)
)
If i print echo "<pre>"; print_r($row[0]);
i have below array
Array
(
[contact] => ugin
)
Array
(
[relation] => uncle
)
i need above should be in $row[0] => contact => ugin
relation => uncle
tried below code:
$addcont[] = $this->input->post(sheepItForm1_addtnlcontact);
$this->conditionloop($addcont[0],'contact');
$relation[] = $this->input->post(sheepItForm1_relationship);
$this->conditionloop($relation[0],'relation');
public function conditionloop($val,$name)
{
foreach($val as $key => $res)
{
if(!empty($res)) {
$row[$key][$name] = $res;
//echo "<pre>"; print_r($row[$key]);
}
}
echo "<pre>"; print_r($row[0]);
}
please help to solve this

(PHP) Generic code to get different structure of arrays (objects)

I have 2 structures of arrays (objects) returned :
1) If only 1 data returned :
stdClass Object
(
[result] => stdClass Object
(
[recordData] => stdClass Object
(
[fieldNames] => Array
(
[0] => CUSTOMER_ID_
[1] => EMAIL_ADDRESS_
[2] => FIRST_NAME
[3] => LAST_NAME
[4] => SEX
[5] => DATE_OF_BIRTH
)
**[records] => stdClass Object
(
[fieldValues] => Array
(
[0] => CUSTOMER_ID_001
[1] => email001#test.com
[2] => FIRST_NAME_001
[3] => LAST_NAME_001
[4] => M
[5] => 01/01/1991
)
)**
)
)
)
2) If more than 1 data returned :
stdClass Object
(
[result] => stdClass Object
(
[recordData] => stdClass Object
(
[fieldNames] => Array
(
[0] => CUSTOMER_ID_
[1] => EMAIL_ADDRESS_
[2] => FIRST_NAME
[3] => LAST_NAME
[4] => SEX
[5] => DATE_OF_BIRTH
)
**[records] => Array
(
[0] => stdClass Object
(
[fieldValues] => Array
(
[0] => CUSTOMER_ID_001
[1] => email001#test.com
[2] => FIRST_NAME_001
[3] => LAST_NAME_001
[4] => M
[5] => 01/01/1991
)
)
[1] => stdClass Object
(
[fieldValues] => Array
(
[0] => CUSTOMER_ID_002
[1] => email002#test.com
[2] => FIRST_NAME_002
[3] => LAST_NAME_002
[4] => F
[5] => 02/02/1992
)
)
)**
)
)
)
Currently i am using this code :
foreach ($memberlist->result->recordData->records as $list)
{
$out .= "<tr>";
foreach ($list as $memberarray)
{
foreach ($memberarray as $member)
{
$out .= "<td>" . $member . "</td>";
}
}
$out .= "</tr>";
}
but it does not work when only 1 data returned.
How can I modify my code to get both structures?
Thank You.
I believe this should do the trick:
// If it is already an array, leave it as an array, if it's not, place it in an array
$member_records = ( is_array( $memberlist->result->recordData->records ) ? $memberlist->result->recordData->records : array( $memberlist->result->recordData->records ) );
foreach ($member_records as $list)
{
$out .= "<tr>";
foreach ($list as $memberarray)
{
foreach ($memberarray as $member)
{
$out .= "<td>" . $member . "</td>";
}
}
$out .= "</tr>";
}

Remove duplicates from an object array

I have an object array that looks like this:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 13
)
)
[2] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I am looking to identify that the ClassScheduleID of 2263 is a duplicate, and remove the duplicate entry's entire object from the array. So that I get:
Array
(
[0] =>Object
(
[ClassScheduleID] => 2263
[Name] => Workout 1
[Location] => Object
(
[BusinessID] => 1
)
)
[1] =>Object
(
[ClassScheduleID] => 2264
[Name] => Workout 2
[Location] => Object
(
[BusinessID] => 22
)
)
I tried the solution proposed here
How to remove duplicate values from a multi-dimensional array in PHP
but the count() remained the same
$count = 0;
foreach($arrayObj as $key => $value) {
if ($value['ClassScheduleID'] == 2263) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
It works: http://ideone.com/fork/zrdDtu
Edit: Modified to delete any duplicates:
foreach($arrayObj as $key => $value) {
$count = 0;
foreach($arrayObj as $nkey => $nvalue) {
if ($value['ClassScheduleID'] == $nvalue['ClassScheduleID']) {
$count++;
}
if ($count > 1){
unset($arrayObj[$key]);
$count--;
}
}
}
var_dump($arrayObj);
See it here: http://ideone.com/fork/85RCst
function get_unique_array($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = get_unique_array($value);
}
}
return $result;
}
Demo: http://3v4l.org/WOTHi#v430

Categories