简体   繁体   English

如何从mysql计算所有行,但仅显示2个结果并计算所有结果

[英]How to count all rows from mysql but display only 2 results and count all results

I trying to display my data from database. 我试图显示数据库中的数据。 More precisely images. 更精确的图像。 Problem is when i have to extract more than 2 images. 问题是当我必须提取两个以上的图像时。 Then i want to display only first two images and print for example added 49 images or how many images i had extract from mysql. 然后,我只想显示前两个图像并进行打印,例如添加49个图像或从mysql中提取了多少图像。 My script is here: 我的脚本在这里:

public function getImages($id, $username){
    global $con;
    $stm = $con->prepare("SELECT * FROM image WHERE postId = :id AND author = :username");
    $stm->execute(array(":id" => $id, ":username" => $username));
    $imagesNum = $stm->rowCount();
    if($imagesNum == 1){
        $image = $stm->fetch(PDO::FETCH_OBJ);
        echo '<div class="frame"><div class="frameOneImage"><img src="../upload/img/'.$username.'/'.$image->path.'"></div></div>';
    } else if($imagesNum == 2){
        echo '<div class="frame">';
        $row = $stm->fetchAll();
        foreach($row as $image){
            echo'<div class="frameTwoImages"><img src="../upload/img/'.$username.'/'.$image['path'].'"></div>';
        }
        echo '</div>';
    } else {
        // HERE I WANT TO DISPLAY ONLY TWO IMAGES BUT IT WRITE ADDED (NUMBER OF ALL IMAGES EXTRACTED)
    }
}

I can think of two ways to achieve that : 我可以想到两种方法来实现:

First solution 第一个解决方案

Do a query with a subquery. 用子查询进行查询。 The main query will return just two rows and the subquery will return the count of total images. 主查询将仅返回两行,而子查询将返回总图像数。

Something like this : 像这样的东西:

SELECT *, (SELECT COUNT(*) FROM image WHERE postId = :id AND author = :username) as totalRows FROM image WHERE postId = :id AND author = :username LIMIT 2

Second solution 第二解决方案

Do your query just like you're doing it right now and do a loop which prints only two images. 像现在一样执行查询,并执行仅打印两个图像的循环。

 $row = $stm->fetchAll();
 $totalRows = $stm->rowCount();

 // This loop will print only two rows
 for($i = 0; i < 2; $i++) {
     echo $row[$i]; 
 } 
 echo "Total rows " . $totalRows;

Personally I would prefer the first one since it will only returns two rows plus the number of total items in the database instead of the total rows and just printing two of the second solution. 我个人比较喜欢第一个解决方案,因为它只会返回两行加上数据库中项目总数,而不是总行数,并且只打印第二个解决方案中的两个。

You can set a variable to increment and then exit out of the for loop after two iterations, like so: 您可以将变量设置为递增,然后在两次迭代后退出for循环,如下所示:

else {
    echo '<div class="frame">';
    $row = $stm->fetchAll();
    $i = 0;
    foreach($row as $image){
        echo'<div class="frameTwoImages"><img src="../upload/img/'.$username.'/'.$image['path'].'"></div>';
        if ($i >= 2) {
            echo 'Only first two results showing. ' . $imageNum . ' results have been omitted.';
            break;
        } else {
            $i++;
        }
    }
}
public function getImages($id, $username){
    global $con;
    $stm = $con->prepare("SELECT * FROM image WHERE postId = :id AND author = :username");
    $stm->execute(array(":id" => $id, ":username" => $username));
    $imagesNum = $stm->rowCount();
    $row = $stm->fetchAll();
    for($i=0; $i<2; $i++){
        if(isset($row[$i])){
             echo '<div class="frame"><div class="frame($imagesNum == 1 ? 'OneImage' : 'TwoImages')"><img src="../upload/img/'.$username.'/'.$row[$i]->path.'"></div></div>';
        }
    }
    if($imagesNum > 2) {
        echo '<span>'.$imagesNum.' images total</span>';
    }
}

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

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