douying0108 2014-10-17 11:25
浏览 9

是否在循环中的屏幕上打印图像文件(对应于线程ID)?

This Question Has Gotten Updated:

I have these columns stored in the table "thread":

id - title - caption - image_file_id - hashtag_id - date_created

An example for "image_file_id", it are the ID numbers of the "images" table for the image files:

583, 584, 585

The script should get the "title" and the "caption" from the "thread" table and print them with a while loop, then it also should get the image files from the "images" table and also print them corresponding to the thread ID.

E.g.

Thread ID: 35
<h1>Title</h1>
<p>Caption</p>
1. <img src=""/>
2. <img src=""/>
3. <img src=""/>
4. <img src=""/>

Thread ID: 36
<h1>Title</h1>
<p>Caption</p>
1. <img src=""/>
2. <img src=""/>

EDIT:

After the suggestion by "Steve" I have gotten it to work with this script here:

<!DOCTYPE html>
<html>
    <head>
        <title>

        </title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css" />
    </head>
    <body>
<?php

    include("connect.php");

?>
<?php

    $tqs = "SELECT * FROM `thread`";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    // $row = mysqli_fetch_assoc($tqr);


    // The ID numbers of the image files.


    // This prints e.g.:
    // Array ( [0] => 583 [1] => 584 [2] => 585 )
    // print_r($exploded);

    // echo "<br/><br/>";

    //This prints e.g.:
    // 3
    // print_r(count($exploded));


    // Select the image files by the ID numbers

    // Iterate through he image files:




while ($row = mysqli_fetch_assoc($tqr)){

    echo "<div class='content'>";
    echo "<div class='title_caption'>";
    echo "<h1>" . $row['title'] . "</h1>";
    echo "<p>" . $row['caption'] . "</p>";
    echo "</div>";
    echo "<div class='parent-container'>";

    // Have the image files printed on screen here!

    $exploded = explode(", ", $row['image_file_id']);

    foreach($exploded as $id){
        $tqs_two = "SELECT `image_file` FROM `images` WHERE `id` = '" . $id . "'";
        $tqr_two = mysqli_query($dbc, $tqs_two) or die(mysqli_error($dbc));
        $row_two = mysqli_fetch_assoc($tqr_two);
        echo "<img src='http://localhost/gallerysite/multiple_image_upload/thumbs/" . $row_two['image_file'] . "' />";
    }

    echo "</div>";
    echo "</div>";

}

?>

    </body>
</html>

I would like to ask if this script is correct, as said it is working.

This script goes row by row, though my question would be: how can I ensure that the image files are correctly printed on screen corresponding to the "thread ID"?

  • 写回答

1条回答 默认 最新

  • dparivln22034 2014-10-17 13:15
    关注

    You can simplify this by using IN query, to reduce you overall number of queries:

    $tqs = "SELECT * FROM `thread`";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); 
    
    while ($row = mysqli_fetch_assoc($tqr)){
    
        echo "<div class='content'>";
        echo "<div class='title_caption'>";
        echo "<h1>" . $row['title'] . "</h1>";
        echo "<p>" . $row['caption'] . "</p>";
        echo "</div>";
        echo "<div class='parent-container'>";
    
        // No need to explode, just use IN query
        $tqs_two = "SELECT `image_file` FROM `images` WHERE `id` IN ({$row['image_file_id']})";
        $tqr_two = mysqli_query($dbc, $tqs_two) or die(mysqli_error($dbc));
        while($row_two = mysqli_fetch_assoc($tqr_two)){
            echo "<img src='http://localhost/gallerysite/multiple_image_upload/thumbs/" . $row_two['image_file'] . "' />";
        }
    
        echo "</div>";
        echo "</div>";
    }
    

    Ideally you would have the image ids stored in a normalized fashion - eg using a lookup table, but then you could do the whole thing with one query.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。