2

PHPを使用してフォルダー内のすべての画像を読み込んでから、テーブルを作成し、jsonファイルからテキストを取得して、各画像の横に配置しようとしています。目標は、このような json を持つことです。

{
    "Car1": {
        "year":"2012"
    },
    "Car2": {
        "year":"2011"
    },
    "Car3": {
        "year":"2009",
        "milage":"10,204"
    }
}

Car1、Car2 の名前は、最終的にはフォルダ内の実際の画像の名前にも一致します。そこで、json ファイルの画像と正しいセクションを取得し、それらをすべてリストした表を作成したいと思います。これまでのところ、私は以下のphpを持っていますが、以下に示すように、それをすべてまとめる方法が本当にわかりません。以下のphpを組み合わせて、私が説明した結果を達成する方法について何か提案はありますか?


6/1 編集 (以下の回答を使用した新しいコード)。これは、このすべてを出力したい場所のページにあり、 &letter 変数は別のページのフォームから渡されます。しかし、そのフォームが送信されてこのページが起動しても、何も起こりません。私は何か間違ったことをしていますか?

$letter = $_POST['letter'];

//Call the path of the cars for the chosen letter
$path = "/images/PartsCars/".$letter."/";
$temp_files = scandir($path);

//Call the path for the json file in the chosen letter subfolder
$data = json_decode($string, true);

//Sort the pictures in this folder alphabetically
natsort($temp_files);

echo '<table cellspacing="5" cellpadding="5">';

//Loop through all pictures and json elements to build out the page
foreach($temp_files as $file) 
{
    if($file != "." && $file != ".." && $file != "Thumbs.db" && $file != basename(__FILE__)) 
    {
        echo '<tr>';
        echo '<td><a href="'.$url.$file.'" title="'.$file.'"><img src="'.$url.$file.'" alt="'.$file.'" style="width:300px;height:200px;"/></a></td>';
        $info = pathinfo($file);
        $file_name =  basename($file,'.'.$info['extension']);
        echo '<td>'.print_r($data['$file_name']).'</td>';
        echo '</tr>';
    }
}

echo '</table>';
4

1 に答える 1

8

私は使いやすさのためにphpjson_decodeを使用しており、デモにはprint_rを使用しています。foreachループを使用して適切に印刷できます。

$path = "./images/PartsCars/A/";
$temp_files = scandir($path);
$string = file_get_contents("/images/PartsCars/A/sample.json");
data = json_decode($string, true);

natsort($temp_files);

echo "<table>";

foreach($temp_files as $file) 
{
    if($file != "." && $file != ".." && $file != "Thumbs.db" && $file != basename(__FILE__)) 
    {
        echo '<tr>';
        echo '<td><a href="'.$url.$file.'" title="'.$file.'"><img src="'.$url.$file.'" alt="" /></a></td>';
        $info = pathinfo($file);
        $file_name =  basename($file,'.'.$info['extension']);
        echo '<td>'.print_r(data['$file_name']).'</td>';
        echo '</tr>'
    }
}
echo '</table>';
于 2012-05-25T17:22:53.697 に答える