dragon2309 Posted February 15, 2013 Share Posted February 15, 2013 Hey there everyone, I've been playing with PHP and SQL for a few years, mainly as a hobby, so I'm not an expert, and it takes me a while to get most things working. This has me stumped... I'm pretty sure (like 80% sure) that it was working fine, then I just noticed the bug and have no idea how to fix it.. I run an SQL query, that when run in the db returns 6 records, all 6 records are to be echoed to the page as per this loop: $i = 1; while ($row = $result->fetch()) { if ($i <= 3) { $output[] = '<div id="featuredinner">'; $output[] = "\n"; $output[] = '<a href="images/'.$row['img'].'_lrg.jpg" rel="lightbox" title="'.$row['desc'].'"><img src="images/'.$row['img'].'_lrg.jpg" width=150px /></a><br />'."\n".' '.$row['title'].' - <strong>£'.$row['price'].'</strong><br /> <a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a>'; $output[] = "</div>\n "; } else { $output[] = '<br /><br /><br />'; $i = 0; } $i++; } I use the same loops on multiple other pages echoing records from the same table (just with different credentials) and they seem to be workign fine. It's jsut this one.. The bug is that the 4th record is never returned, 1, 2 and 3 are echoed on one line as desired, then its ment to drop a line and echo 4, 5 and 6, it doesnt, all i get is 5 and 6 and then a blank space. Ideas?? As said the exact same piece of code for the loop and if statement is used on other pages (different product categroies) perfectly, the only difference is that the other sections have more items in, (some have 18-19 things in that are echoed on page 3 per line perfectly fine.. Page is live at http://www.thechocolatehut.co.uk/truf_milk.php if you want to see, ignore the DQL query being echoed to apge, I wanted to see what query the page was generating. Thanks all, Dave. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 15, 2013 Share Posted February 15, 2013 Because when $i is greater than 3, you ONLY add the new lines and set $i to 0. Then you go to the next row. You could save a lot of work by using modulo % Quote Link to comment Share on other sites More sharing options...
dragon2309 Posted February 15, 2013 Author Share Posted February 15, 2013 Small update, the code is clearly bugged, as it does the same thign on the other pages, I was just being dim.... It misses the 4th record from every line. So its not echoing records 4, 8, 12 etc.. Quote Link to comment Share on other sites More sharing options...
dragon2309 Posted February 15, 2013 Author Share Posted February 15, 2013 Hi Jessica, thanks for your reply I've not heard of or used modulo, I'll look into that for an upgrade later on, for now I need this working at least to some extent. Are you saying I need to add the record echoing code into the else statement aswell?? Quote Link to comment Share on other sites More sharing options...
dragon2309 Posted February 15, 2013 Author Share Posted February 15, 2013 Okay, now fixed thanks to the advice from Jessica. Many thanks, I'll look into modulo now Thankyou! Dave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.