前提・実現したいこと
PHP超初心者です。
見よう見まねでECサイトを構築していますが、エラーが出ており定義していた写真が表示されません。
どうかご教示お願いいたします。
発生している問題・エラーメッセージ
Warning: Illegal string offset '$code' in C:\xampp\htdocs\ec0406\t_index.php on line 32
該当のソースコード
PHP
1t_index.php 2 3<!DOCTYPE html> 4<html> 5<head> 6<meta charset="utf-8"> 7<title>TOP | フラワーショップ </title> 8<link rel="stylesheet" href="css/flower.css"> 9</head> 10<body> 11<header> 12<h1>フラワーショップ </h1> 13</header> 14<main> 15<!--検索フォーム--> 16 <area1> 17 <form action = "index.php" method="post"> 18 <input type="text" name="keyword"> 19 <input type="submit" value="検索"> 20 </form> 21 </area1> 22<!--/検索フォーム--> 23<!--商品一覧表示--> 24 <area2> 25 <table> 26 <?php 27 $flowers = ""; 28 foreach ((array)$flowers as $f) { 29 ?> 30 <tr> 31 <td> 32 <?php 33 require_once 'common.php'; 34 echo imgTag($f['code']) 35 ?> 36 </td> 37--------------------------------------------------------------------------------- 38common.php 39 40<?php 41 session_start(); 42 43 $host = 'localhost'; 44 $dsn = 'mysql:host=localhost;dbname=ec0406;charset=UTF8'; 45 $user = 'root'; 46 $password = '12345'; 47 48 function connect(){ 49 return new PDO($dsn, $user, $password); 50 } 51 52 function imgTag($code) { 53 if (file_exists("img/$code.png")) $name = $code; 54 else $name = 'noimage'; 55 return '<img src="img/' . $name . '.png" alt="">'; 56 } 57 ?>
補足情報
練習用として以下のバージョンを使用しております
PHP Version 5.6.24
XAMPP v3.2.2
何か書籍など参考にされているのでしょうか。
回答1件
あなたの回答
tips
プレビュー