English 中文(简体)
从 php 中的文件夹读取文件, 从 json 文件中列出条目
原标题:Read files from folder in php and list out entries from json file
  • 时间:2012-05-25 16:32:31
  •  标签:
  • php
  • json

我试图用 php 将所有图像装入文件夹中的所有图像, 然后构建一个表格, 从json 文件上拉出文本, 并将其放在每个图像的旁边。 目标是让 json 看起来像这个 。

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

Car1、 Car2 名称将最终匹配文件夹中真实图像的名称 。 所以我要抓取 Json 文件中的图像和正确部分, 并构建一个列出所有图像的表格 。 到目前为止, 我拥有下面的 php, 但是不能真正确定 如何将其组合起来, 正如您在下面看到的那样, 它现在只是单独的。 对于如何将下面的php 组合到一起来实现我描述的结果, 有什么建议吗?


6/1 编辑( 使用下面的答案的新代码) 。 这是在一页上, 我希望全部输出, 而 & amp; 字母变量是从另一页的窗体中传出。 但是当该表单提交时, 当此页面点火时, 没有什么发生 。 我做错了什么吗?

$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> ;
最佳回答

我使用phpjson_decode来方便使用, 使用打印_ r 来演示, 您可以使用一个前方循环来正确打印出来 。

$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> ;
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签