Marut Phuphaniat
Marut Phuphaniat

Reputation: 23

PHP Notice:Array to string conversion

I want to show all data from $result into my table but there's some error. I'm trying to access a php array. But it is throwing me Array to string conversion error. The notice is "Array to string conversion" my controller :

 public function actionStockitem() {

    $datepost = date('Y-m-d');
    $d1 = isset($data['d1']) ? $data['d1'] : $datepost;

    $sql = " SELECT s1.name as name          
        FROM
            (SELECT i.item_id,i.name,i.unitcost,
            (SUM(s1.qty)) as stock1,         
            ((SUM(s1.qty))* i.unitcost) as cost1     
             FROM (SELECT item_id,name,unitcost FROM mitem  ) as i
             JOIN mstocklist s1 ON s1.item_id=i.item_id
             JOIN mstock s2 ON s2.id=s1.stock_id
             WHERE s2.receive_date <='$d1'
            GROUP BY i.item_id
            ORDER BY i.name ASC ) s1
          LEFT OUTER JOIN
            (SELECT i.item_id,i.name,i.unitcost,
            (SUM(s1.qty)) as stock2,
            ((SUM(s1.qty))* i.unitcost) as cost2    
            FROM (SELECT item_id,name,unitcost FROM mitem  ) as i
            JOIN msublist s1 ON s1.item_id=i.item_id
            JOIN msub s2 ON s2.id=s1.sub_id
            WHERE s2.receive_date <='$d1'
            GROUP BY i.item_id
            ORDER BY i.name ASC ) s2 ON s2.item_id=s1.item_id 
          WHERE  ((s1.stock1)-IFNULL(s2.stock2,0))<>0
          ORDER BY s1.name,cost1  ASC ";

    $command = Yii::$app->db->createCommand($sql);
    $reader = $command->query();
    $inames = $reader->readAll();

    $temp = array();
    $iname = $inames;

    $temp['name'] = $iname;
    $result[] = $temp;

    return $this->render('stockitem', [
                'inames' => $inames,
                'sql' => $sql,
                'result' => $result,
    ]);
}

and this is the View :

<tbody>           
        <tr>
            <td>
                <?php
                $i = 1;
                foreach ($result as $data) {
                    ?>
                </td>
            </tr>
            <tr>
                <td align="center"><?php echo $i; ?></td>
                <td align="left" class="style3"><?php echo $data['name']; ?></td>  //error in this line
                <td align="center" class="style3"><?php echo ''; ?></td>
                <td align="center" class="style3"><?php echo ''; ?></td>
                <td align="center" class="style3"><?php echo ''; ?></td>
                <td align="center" class="style3"><?php echo ''; ?></td>
                <td align="center" class="style3"><?php echo ''; ?></td>
                <td align="center" class="style3"><?php echo ''; ?></td>

            </tr>
        </tbody>
        <?php
        $i++;
    }
    ?>

Please give me some advice

Upvotes: 1

Views: 1748

Answers (1)

JYoThI
JYoThI

Reputation: 12085

Your pushing data to inside the array needlessly . readAll() returns the result in single array .so just pass it to view . like this .

    $inames = $reader->readAll();

    // $temp = array();
    //$iname = $inames;

    // $temp['name'] = $iname;
    // $result[] = $temp;

    return $this->render('stockitem', [
                'inames' => $inames,
                'sql' => $sql,
                'result' => $inames,
    ]);

Upvotes: 1

Related Questions