Jump to content


Photo

Echoing SQL Results - Odd outcome..


  • Please log in to reply
4 replies to this topic

#1 dragon2309

dragon2309

    Member

  • Members
  • PipPip
  • 14 posts

Posted 15 February 2013 - 01:29 PM

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".'&nbsp;'.$row['title'].' - <strong>&pound;'.$row['price'].'</strong><br />&nbsp;<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a>';
   $output[] = "</div>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
   } 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.thechocol...k/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.

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 15 February 2013 - 01:31 PM

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 %
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 dragon2309

dragon2309

    Member

  • Members
  • PipPip
  • 14 posts

Posted 15 February 2013 - 01:35 PM

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..

#4 dragon2309

dragon2309

    Member

  • Members
  • PipPip
  • 14 posts

Posted 15 February 2013 - 01:36 PM

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??

#5 dragon2309

dragon2309

    Member

  • Members
  • PipPip
  • 14 posts

Posted 15 February 2013 - 01:57 PM

Okay, now fixed thanks to the advice from Jessica. Many thanks, I'll look into modulo now ;)

Thankyou!

Dave




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com