Echo database results in multiple table rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Emmetje
    New Member
    • Feb 2013
    • 4

    Echo database results in multiple table rows

    I've a piece of code with outputs database results in a table. This is working fine but I want to have max 5 cols and start a new row after that. Would be great if someone could help me out. Many thanks in advance!

    Code:
    $opties.='<b>'.$option['name'].':</b><br /><table><tr>';
    
          foreach ($option['option_value'] as $option_value) { 
    
            $opties.= '<td><img src="image/cache/'.$option_value['image'].'" /><br />'.$option_value['name'].'</td>';
    
          }
           $opties.='</tr></table>';
    Last edited by Meetee; Feb 11 '13, 10:06 AM. Reason: Use code tags [/CODE] around code
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use a variable to keep a column count and if the count is five, write a new row and start the count over.

    Comment

    • Emmetje
      New Member
      • Feb 2013
      • 4

      #3
      Thanks for your answer. I tried to use some code snippets I've found on the internet but couldn't get anything to work. :( Could you help me out here? Thanks in advance!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Please post the code you've tried and we can help you fix the problems in your implementation.

        Comment

        • Emmetje
          New Member
          • Feb 2013
          • 4

          #5
          Sure here it is:

          Code:
          $opties.='<b>'.$option['name'].':</b><br /><table><tr>';
          $cnt=0;
          foreach ($option['option_value'] as $option_value) { 
              if($cnt % 5 == 0 && $cnt > 1) 
                  $opties .= '</tr><tr>';
              $opties .= '<td><img src="image/cache/'.$option_value['image'].'" /><br />'.$option_value['name'].'</td>';
              $cnt++;
          }
          if(count($option['option_value']) % 5 > 0)
              $opties.='</tr>';
          $opties.='</table>';
          There seems to be something wrong with the rows or cols. It's quit hard to debug because the results of this are send to tcpdf for generating a pdf catalog.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Just saying something is wrong doesn't actually give me any info to work with. You have to tell me what's wrong.

            As far as your code goes, the one glaring error I see is that line 9 shouldn't be there because you will always need a closing tr tag.

            Comment

            • Emmetje
              New Member
              • Feb 2013
              • 4

              #7
              You did it!! Commented out line 9 and it works! Many, many thanks for this, you made my day!

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                No problem, glad to help.

                Comment

                Working...