LoginSignup
1
0

More than 3 years have passed since last update.

Warning: Illegal string offset ' ' in... の解決方法

Posted at

今回は、SQLから取得したデータをsmartyを使って、foreachでtable表示させようとした際に発生したエラーが解決した方法をご紹介します。

発生したエラー

Warning: Illegal string offset '出力変数の各カラム名' in...

下記のサイトを参考に解決したのですが、全て英語で書かれた記事なので、正直正確なエラー発生の原因がわかっていません。
どなたか詳しい原因が分かる方いましたらコメントにて教えて頂けると幸いです。
私の場合は、どうやら出力変数のキーを文字列で指定していた為に発生したようです。

・参考サイト
https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php#:~:text=The%20error%20Illegal%20string%20offset,string%20as%20a%20full%20array.&text=For%20those%20who%20come%20to,about%20it%2C%20as%20I%20was

エラーが発生したコードと修正コード

list2.php(データ取得)修正前

<?php
require_once('../db_connect2/db_connect2.php');
require( dirname( __FILE__ , 3).'/libs/Smarty.class.php' );

$smarty = new Smarty();

$smarty->template_dir = dirname( __FILE__ , 3).'/templates';
$smarty->compile_dir  = dirname( __FILE__ , 3).'/templates_c';
$smarty->config_dir   = dirname( __FILE__ , 3).'/configs';
$smarty->cache_dir    = dirname( __FILE__ , 3).'/cache';

$smarty->escape_html  = true;

try
{
  $sql = 'SELECT stock_id,purchase_date,deadline,stock_name,price,number FROM stocks WHERE 1';
  $stmt = connect()->prepare($sql);
  $stmt->execute();
  $rec = $stmt->fetch(PDO::FETCH_ASSOC); //ここで、結果セットに返された際のカラム名で添字を付けた配列を返したことが原因。
  $dbh = null;
}
catch(Exception $e)
{
  echo "エラー発生:" . htmlspecialchars($e->getMessage(),ENT_QUOTES, 'UTF-8') . "<br>";
  print'ただいま障害により大変ご迷惑をお掛けしております。';
  exit();
}

$smarty->assign('stock', $rec);
$smarty->display('list.tpl');
?>

修正コード

try
{
  $sql = 'SELECT stock_id,purchase_date,deadline,stock_name,price,number FROM stocks WHERE 1';
  $stmt = connect()->prepare($sql);
  $stmt->execute();
  $rec = $stmt->fetch(PDO::FETCH_ASSOC); //ここで、結果セットに返された際のカラム名で添字を付けた配列を返したことが原因。
  $dbh = null;
}

↓

try
{
  $sql = 'SELECT stock_id,purchase_date,deadline,stock_name,price,number FROM stocks WHERE 1';
  $stmt = connect()->prepare($sql);
  $stmt->execute();
  $dbh = null;
}

list.tpl(出力)修正前

<table class = "sorttbl" id = "myTable" border = "2">
    <tr>
      <th class = "radio-th"></th>
      <th class = "purchase_date-th">購入日</th>
      <th class = "stock_name-th">商品</th>
      <th class = "price-th">値段</th>
      <th class = "number-th">数量</th>
      <th class = "deadline-th" onclick = "w3.sortHTML('#myTable','.item', 'td:nth-child(6)')">消費期限&nbsp;<i class = "fa fa-sort"></i></th>
    </tr>

    //出力変数のキーを文字列で指定しているのが原因
    {foreach $stock as $s}
      <tr class = "item">
        <td><input type = "radio" name = "stockid" value = "{$s.stock_id}"></td>
        <td>{$s.purchase_date|date_format:'%Y-%m-%d'}</td>
        <td>{$s.stock_name}</td>
        <td>¥{$s.price}</td>
        <td>{$s.number}</td>
        <td>{$s.deadline|date_format:'%Y-%m-%d'}</td>
      </tr>
    {/foreach}
  </table>

修正コード

{foreach $stock as $s}
  <tr class = "item">
    <td><input type = "radio" name = "stockid" value = "{$s.stock_id}"></td>
    <td>{$s.purchase_date|date_format:'%Y-%m-%d'}</td>
    <td>{$s.stock_name}</td>
    <td>¥{$s.price}</td>
    <td>{$s.number}</td>
    <td>{$s.deadline|date_format:'%Y-%m-%d'}</td>
  </tr>
{/foreach}

↓

{foreach $stock as $s}
  <tr class = "item">
   <td><input type = "radio" name = "stockid" value = "{$s[0]}"></td>
   <td>{$s[1]|date_format:'%Y-%m-%d'}</td>
    <td>{$s[3]}</td>
    <td>¥{$s[4]}</td>
    <td>{$s[5]}</td>
    <td>{$s[2]|date_format:'%Y-%m-%d'}</td>
  </tr>
{/foreach}
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0