LoginSignup
0
0

More than 5 years have passed since last update.

phpでGETを利用したデータの受け渡しについての質問

Posted at

現在XamppのApache環境でphpの練習として、GETのデータの受け渡しの理解を深める途中でうまく表示されず悩んでいます。アドバイスいただけたら幸いです。
内容は、csvファイルからフルーツの商品情報を取得して、3種類のフルーツ情報を一品ごとにブラウザ表示させ、URLのgetパラメータでidを直接指定して、表示内容をかえるというものです。
下記がソースコードです。

<body>
    <h1>商品情報</h1>
    <?php 
    //ファイル名を取得
    $csv_path = './data/data.csv';
    //dateフォルダにcsvファイルがあるか確認する
    //あればファイルを開く
    if(file_exists($csv_path)){
        //ファイルの内容を読み込む
        $file_read = file($csv_path);
    }

    $view_fruits = array();
    foreach ($file_read as $fruits){
        $fruits = mb_convert_encoding($fruits, 'utf-8', 'sjis-win' );
        $fruits = explode(',', $fruits);
        //番号、商品、価格の内容を配列に格納
        $f = array();
        $f['id']      = $fruits[0]; 
        $f['product'] = $fruits[1];
        $f['price']   = $fruits[2];

        if ($f['id'] == $_GET['id']){
            $view_fruits = $f;
        }

    }
    ?>

    <table>
        <tr>
            <th>商品</th>
            <td><?php echo $f['product']; ?></td>
        </tr>
        <tr>
            <th>価格</th>
            <td><?php echo $f['price']; ?></td>
        </tr>

    </table>
</body>

【ブラウザの表示】 
image.png

【URL】※id=3をid=2またはid=1 に変更しても表示内容が変わりません。
http://localhost/fruits_csv/test.php?id=3

【csvの内容】 
image.png

0
0
3

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
0
0