html - do...while loop iterates once too many, while loop iterates once too few - php -
i have been building photo galleries many of own , client websites time now. mysql database-driven, use mysqli extension php, , galleries have 'gallery' page , 'thumbnails' page jquery lightbox used serve final image once thumbnail clicked. have @ least half dozen such galleries on server alone, , each new one, start code base of last gallery built.
the "gallery" page in every 1 of existing galleries uses do...while loop display galleries in database (usually paginated) after results selected prepared statement. in every case, do...while loop runs through selected records , displays data without hitch.
then started new gallery today , developed problem out of nowhere. way because have used code 1 of other site galleries , 1 works perfectly.
details:
galleries
table in new database has 7 columns, 1 of type
, column contains either word 'photo' or 'video'. testing there 5 dummy galleries in database, 3 photo , 2 video. default setting page select records, unless either of "photos" or "videos" links clicked, in case pass query string , different select query used select , display photo or video galleries.
the query code (which works desired):
if (($gtype != 'photo') && ($gtype != 'video')) { $getgalleries = "select gallery_id, gallery_type, gallery_thumb, gallery_part, gallery_name, gallery_about galleries gallery_live = ?"; $stmt = $conn->prepare($getgalleries); $stmt->bind_param('i', $live); $stmt->bind_result($id, $type, $thumb, $part, $name, $about); $stmt->execute(); $stmt->store_result(); $stmt->fetch; } else { $getgalleries = "select gallery_id, gallery_type, gallery_thumb, gallery_part, gallery_name, gallery_about galleries gallery_type = ? , gallery_live =?"; $stmt = $conn->prepare($getgalleries); $stmt->bind_param('si', $gtype, $live); $stmt->bind_result($id, $type, $thumb, $part, $name, $about); $stmt->execute(); $stmt->store_result(); $stmt->fetch(); }
and html/php do...while loop:
<?php { ?> <div class="galleries"> <div class="thumb_<?php echo $type; ?>"> <img src="graphics/<?php echo $type; ?>.jpg"> </div><!-- .thumb_photo / _video --> <p class="about_title"> <?php echo $name; ?> </p> <p class="about_text"> <?php echo $about; ?> </p> </div><!-- .galleries --> <?php } while ($stmt->fetch()); ?>
i put sample page here show problem having.
so opening page displays contents of galleries
table, 5 galleries total. time using do...while loop have in every other gallery, do...while loop iterates 1 many times, leaving empty <div>
, missing .jpg
image @ top 5 galleries displayed below it. when either "photo" or "video" links clicked, page reloads , shows correct galleries , nothing extra. clicking "all" link reopens page, 5 galleries , <div>
@ top. when change loop while loop, default page free of <div>
great...but when either "photo" or "video" links clicked, serve 1 fewer record each.
i have been headscratching 1 day. it's not though have never done before, yet lo , behold, have mysterious problem.
again, i've used technique many times, have missing simple. sincerely appreciated. thanks!
update: forgot mention 1 item. out of frustration, added query count number of entries in database, , came 5. led me think queries not issue, rather was, somehow in code.
i think issue might have
$stmt->fetch;
inside of if (($gtype != 'photo') && ($gtype != 'video')) {
block,
where should have
$stmt->fetch();
Comments
Post a Comment