Praveen Ezlilan
Praveen Ezlilan

Reputation: 19

How to solve an illegal string offset error during printing of array values?

I face this error

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'company'

Filename: sales/print_file.php

In my Controller

$data=array("invoice_no"=>$invoice_no,
    "phone"=>$phone,
    "date"=>$date,
    "att"=>$Att,
    "company"=>$company,
    "area"=>$body,
    "subtotal"=>$subtotal,
    "tax"=>$tax,
    "total"=>$total,
    );

    .
    .
    $result=$this->other_tasks_model->inserts($data);
    .
    .
    $data["item1"] = $data;
    .
    .
    if($result==true)
        {
        $this->_tpl_super_admin('sales/print_file', $data);//for template changes based on user login
     }
     else{
     _error handling_
     }

In my model

public function inserts($data)
{
  $this->db->insert("invoice",$data);
  return $this->db->affected_rows() !=1 ? false:true;
}

In my view

  <?php 
    if(isset($item1)){
      foreach($item1 as $row){;
        echo "<h2>".$row['company']."</h2>";
        echo $row['date']."<br>"; 
        echo "#".$row['invoice_no'];
  ?>

Upvotes: 0

Views: 861

Answers (1)

Reuben Gomes
Reuben Gomes

Reputation: 878

<?php 
  if(isset($item1)){
  foreach($item1 as $row){;
  echo "<h2>".$row['company']."</h2>";
  echo $row['date']."<br>"; 
  echo "#".$row['invoice_no'];
  ?>

as you have a 1 dimensional array

you do not require a foreach loop

either you do 2 thing

  • Remove the foreach part

    or

  • you modify $data being set to the view that way you can keep the foreach

loop

 $data[]= $data;(this will make it a 2 dimensional array)
$data["item1"] = $data;
.

Upvotes: 0

Related Questions