开发者

How to isolate first result row from MySQL query in PHP?

开发者 https://www.devze.com 2023-03-14 02:41 出处:网络
I have a query that I\'m running that is returning always 10 results. i.e: SELECT * FROM table LIMIT 10 I\'m then looping through the results. On each of them, I\'m resizing them into thumbnails

I have a query that I'm running that is returning always 10 results.

i.e:

SELECT
     *
FROM 
     table
LIMIT 10

I'm then looping through the results. On each of them, I'm resizing them into thumbnails (10 thumbnails). Before displaying the 10 thumbnails, for the very first result, I want to resize it to make it larger. The resizing is no issue. It's trying to figure out how to isolate the very first one before outputting the 10 thumbnails.

I'm doing something like below:

if($stmt->rowCount() != 0) {
        while ($row = $stmt->fetch(PDO::F开发者_运维技巧ETCH_ASSOC)) {
            $id = $row['id'];
            $filename = $row['filename'];
            // take the first $filename result and resize it to make it larger
            // resize $filename into thumbnails and output 10 thumbnails 

         }
}

Essentially, it's a leaderboard and all top 10 entries are displayed, but the first one is resized to be bigger to show who is currently dominating the top 10 slot.

Any suggestions on where/how I can isolate that very first result so I can resize it?

Below is an example of what I'm conceptually trying to explain:

How to isolate first result row from MySQL query in PHP?


My PHP is a bit scratchy, but why not just track which iteration you are on? Even simpler than this would be to keep a boolean value but I leave that as an exercise for the reader.

$x = 0
if($stmt->rowCount() != 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $id = $row['id'];
            $filename = $row['filename'];
            if ($x++ == 0)
                    // take the first $filename result and resize it to make it larger
            // resize $filename into thumbnails and output 10 thumbnails 
         }
}


$i=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $id = $row['id'];
        $filename = $row['filename'];
        if ($i++ == 0) {
            // take the first $filename result and resize it to make it larger
        }
        // resize $filename into thumbnails and output 10 thumbnails 

 }


have a simple boolean keep track of it, int would work, but no need to keep incrementing it

$firstReached = False;
if($stmt->rowCount() != 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        if(!$firstReached)
        {
          // do the thing for first row
           $firstReached = True;
        } 
        else 
        {
       // do the thing for remaining rows

     }
   }


Instead of testing in the loop whether you are processing the first image or not, process the first image separately. Including a test condition in a loop which effectively can only be fulfilled in only one iteration seems like a misconception to me.

Put the logic of resizing the image into its own function to make code-reuse easier.

Something like this:

if($stmt->rowCount() != 0) {
    // this gets the first row
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    // take the first $filename result and resize it to make it larger
    resizeImage($row['id'], $row['filename'], 'large');

    // we always want to create a thumbnail, hence do-while
    do {       
        // resize $filename into thumbnails and output 10 thumbnails 
        resizeImage($row['id'], $row['filename'], 'thumbnail');
    } while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
}

function resizeImage(id, name, type) {
    // whatever here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消