【问题标题】:Show all image inside folder that has some word?显示文件夹中包含单词的所有图像?
【发布时间】:2014-01-18 01:38:08
【问题描述】:

谁能帮帮我!我在表格上有姓名:"user01-Diana"

而且我的文件夹中很少有图像文件被重命名为 ex: "user01_20131231_1201.jpg""user01_20131231_1216.jpg""user02_20131231_1201.jpg"

我想要的是从表中读取单词 user01,然后使用它来显示文件夹中包含单词“user01”的所有图像并显示为 ASC。 有人可以帮助我吗?我不是很擅长 php。熬了一夜还是没明白。

【问题讨论】:

  • 请发布您到目前为止所尝试的内容。
  • user01user1 不同吗?因为您的示例根本无法解释问题所在。
  • 是名称,即“user01-Diana”和保存在同一个表中的图像
  • @Mike 我试着把它炸开,但还是不行。我是新手。
  • @MarcB 对不起,我的错,都是 user01。

标签: php database image


【解决方案1】:

根据问题中的新修改尝试以下代码。

首先在变量中获取要获取记录的名称,例如 user01-Diana

$name = "user01-Diana";

现在提取要搜索记录的文本,例如 user01

$name = substr($name, 0, strpos($name, '-'));

现在我们将创建一个模式,我们必须根据该模式匹配文件夹中的图像名称, 在这种情况下,我们的模式是,'/user01/'

$pattern = "/$name/";

现在在opendir函数中直接指定当前文件的路径

if ($handle = opendir('./userimg')) {
    //here we are matching user01 against the files in the directory
    while (false !== ($entry = readdir($handle))) {
        //those that get matched will be echoed below
        if(preg_match($pattern, $entry))
            echo "<br />".$entry;
    }
    closedir($handle);
}


旧代码符合以前的要求

首先你必须从user01-Diana中提取user1

你可以这样做,

$name = "user01-Diana";

$name = substr($name, 0, strpos($name, '-'));

现在您将在$name 变量中获得user01

之后,您可以使用以下查询。

$sel = "SELECT * FROM tablename where imagename like '".$name."%'";

如果您在同一个表中有类似下面的数据,

使用以下查询

Select 
    t1.imagename
from 
    Table1 as t1
LEFT JOIN
    Table1 as t2
ON
    t1.imagename LIKE concat("'", substr(t2.username, 1,(LOCATE("-",t2.username)-1)) , "%'") 
WHERE 
    t1.imagename IS NOT NULL
ORDER BY
    t1.imagename

【讨论】:

  • 天哪,你帮了我这么多@Anamika,我现在就试试。我稍后会发布结果。
  • 我尝试了你的代码,但它只显示该文件夹上的图像文件名列表。如何显示其中包含单词 user01 的所有图像并按 ASC 排序。
  • 我的代码现在是这样的,一切都像我想要的那样工作。 // Find all files in that folder $files = glob('../album/'.$idpas.'*'); // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort natcasesort($files); // Display images foreach($files as $file) { list($idpas,$haripas,$jampas)=split("_",$file); echo '&lt;img width="640" height="480" src="' . $file. '" /&gt;&lt;br/&gt;&lt;center&gt;Foto Pasien '.$namapas.' Tanggal: &lt;strong&gt;'.date("d M Y",strtotime($haripas)).'&lt;/strong&gt;&lt;/center&gt;&lt;br /&gt;';
【解决方案2】:

您应该有一个查询来从您的表格中选择图像,其中图像名称将以'user01' 开头。你的代码应该是这样的:

$db = new mysqli('localhost', 'user', 'pass', 'database');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "select column_name as image from your_table where column_name like 'user01%' order by column_name";
if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}


while($row = $result->fetch_assoc()){
    echo $row['image'] . '<br />';
}

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多