php - How to count all rows from mysql but display only 2 results and count all results -
i trying display data database. more precisely images. problem when have extract more 2 images. want display first 2 images , print example added 49 images or how many images had extract mysql. script here:
public function getimages($id, $username){ global $con; $stm = $con->prepare("select * image postid = :id , 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 $image){ echo'<div class="frametwoimages"><img src="../upload/img/'.$username.'/'.$image['path'].'"></div>'; } echo '</div>'; } else { // here want display 2 images write added (number of images extracted) } }
you can set variable increment , exit out of loop after 2 iterations, so:
else { echo '<div class="frame">'; $row = $stm->fetchall(); $i = 0; foreach($row $image){ echo'<div class="frametwoimages"><img src="../upload/img/'.$username.'/'.$image['path'].'"></div>'; if ($i >= 2) { echo 'only first 2 results showing. ' . $imagenum . ' results have been omitted.'; break; } else { $i++; } } }
Comments
Post a Comment