Renjitha Renjitha - 3 months ago 10
PHP Question

How do I convert associative looping structure to codeigniter active records

How to convert these looping structure to codeigniter Active records....

while($row = mysqli_fetch_array($sql)){
$result_set[] = $row;
}


for($i=1;$i<=$nrows;$i=$i+1)
{
$k = 0;

if($i>0&& $i<=$count)
{

$id=$result_set[$k]['id'];
$date=$result_set[$k]['date'];
}
$k = $k + 1;

}

// codeignitor code

foreach($query->result() as $row) {
$result_set[] = $row;
}

$k = 0;
for ($i = 1; $i <= $nrows; $i = $i + 1) {

if ($i > 0 && $i <= $count) {
$id = $result_set[$k]['id'];
$date = $result_set[$k]['date'];
}
}


Its shows the following error


A PHP Error was encountered

Severity: Notice

Message: Undefined index: id

Message: Undefined index: date


How to solve this issues...

Thanks in advance

Answer

Looks like your array contains an object:

Array ( 
  [0] => stdClass Object ( 
    [id] => 5 
    [date] => 2016-08-17 
  ) 
)

So, these indices should be accessed this way:

$id   = $result_set[$k]->id;
$date = $result_set[$k]->date;
Comments