简体   繁体   English

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

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

Okay guys, the code below works fine. 好的,下面的代码工作正常。 Except that since I have given users up to 4 image downloads, the product preview page should only display 1 of the images like the first one in the array in the product page but it shows all rows if the user uploads like 4 images. 除了我给用户最多下载了4张图片外,产品预览页应该只显示其中一张图片,例如产品页面中数组中的第一个图片,但是如果用户上传了4张图片,它将显示所有行。 how can I limit this? 我该如何限制呢? thanks. 谢谢。

<?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 
            }
        ?>          

Order by in descending order for getting latest row 按降序排列以获得最新行

 $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);  

Below mentioned SQL query is working fine: http://sqlfiddle.com/#!9/1a0e9/1 下面提到的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;

try adding LIMIT 1 尝试添加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
    }
   ?>

The second image query write inside while loop for fetching only one image and also check file column is not empty. 第二个图像查询在while循环内写入,以仅获取一个图像并检查文件列是否为空。 Idon't know what's your column name put your column name according your table structure. 不知道您的列名是什么,将您的列名放在表结构中。

重要的部分是增加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