繁体   English   中英

如何仅显示帖子的第一行?

[英]How to show only the first row of a post?

好的,下面的代码工作正常。 除了我给用户最多下载了4张图片外,产品预览页应该只显示其中一张图片,例如产品页面中数组中的第一个图片,但是如果用户上传了4张图片,它将显示所有行。 我该如何限制呢? 谢谢。

<?php           
        echo 'Your posts:' . '<br>' . '<hr>'; 

        $posts = "SELECT * FROM posts,image_data WHERE userid='$user' AND user_posts.userid = image_data.client_id ORDER BY date_created ASC";
        $posts_result = $mysqli->query($posts);     
        while ($posts_result_rows = $posts_result->fetch_assoc()) {
            $its_id = $posts_result_rows["userid"];
                        $its_title = $posts_result_rows['post_title']; 
                        $its_image = $posts_result_rows['file'];
                        $its_description = $posts_result_rows['post_description'];
        ?>
        <table border=0 cellpadding=5 cellspacing=0 >
            <tr>
                        <?php echo '<br>' . $its_title . ' <br>' . $its_description . '<br>'; ?>
                        <img src="<?php echo "users_data/users_posted_data/".$its_image; ?>" alt="<?php echo $its_title; ?>" width="120" height="120"><hr>                   
            </tr>
        </table>
        <?php 
            }
        ?>          

按降序排列以获得最新行

 $posts = "select * from (SELECT * FROM posts,image_data WHERE userid='$user' AND posts.userid = image_data.client_id ORDER BY date_created DESC)temp group by userid ";
            $posts_result = $mysqli->query($posts);  

下面提到的SQL查询工作正常: http : //sqlfiddle.com/#!9/1a0e9/1

select * from 
(SELECT * FROM posts,image_data WHERE userid=1 
 AND posts.userid = image_data.client_id 
 ORDER BY date_created DESC)temp group by userid;

尝试添加LIMIT 1

$posts = "SELECT * FROM posts,image_data WHERE userid='$user' AND user_posts.userid = image_data.client_id ORDER BY date_created ASC LIMIT 1";
        $posts_result = $mysqli->query($posts);  
<?php           
        echo 'Your posts:<br><hr>'; 

        $posts = "SELECT * FROM posts,image_data WHERE userid='$user' AND user_posts.userid = image_data.client_id ORDER BY date_created ASC";
        $posts_result = $mysqli->query($posts);     
        while ($posts_result_rows = $posts_result->fetch_assoc()) {
            $its_id = $posts_result_rows["userid"];
                        $its_title = $posts_result_rows['post_title']; 
                        $its_image = $posts_result_rows['file'];
                        $its_description =     $posts_result_rows['post_description'];

            echo "<table border=0 cellpadding=5 cellspacing=0 ><tr><td>"; 
            echo '<br>' . $its_title . ' <br>' . $its_description . '<br>';

        }
    ?>   
<img src="<?php echo "users_data/users_posted_data/".$its_image; ?>" alt="<?php echo $its_title; ?>" width="120" height="120"><hr>                   
        </td></tr>
    </table> 
    $posts = "SELECT * FROM posts WHERE userid='$user' ORDER BY date_created ASC";
    $posts_result = $mysqli->query($posts);     
    while ($posts_result_rows = $posts_result->fetch_assoc()) 
    {
        $posts1 = "SELECT * FROM image_data WHERE client_id='$user' and file != '' ORDER BY image_id  DESC";
        $posts_result1 = $mysqli->query($posts1);   
        $posts_result_rows1 = $posts_result1->fetch_assoc()         

        $its_id = $posts_result_rows["userid"];
        $its_title = $posts_result_rows['post_title']; 
        $its_image = $posts_result_rows1['file'];
        $its_description =     $posts_result_rows['post_description'];

        ?>
    <table border=0 cellpadding=5 cellspacing=0 >
        <tr>
            <td>
                <?php echo '<br>' . $its_title . ' <br>' . $its_description . '<br>'; ?>
                <img src="<?php echo "users_data/users_posted_data/".$its_image; ?>" alt="<?php echo $its_title; ?>" width="120" height="120" />
                <hr>   
            </td>       
        </tr>
    </table>
    <?php
    }
   ?>

第二个图像查询在while循环内写入,以仅获取一个图像并检查文件列是否为空。 不知道您的列名是什么,将您的列名放在表结构中。

重要的部分是增加limit 1

SELECT * FROM posts,image_data WHERE userid='$user' AND user_posts.userid = image_data.client_id ORDER BY date_created ASC limit 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM