1

Is it possible to echo database data into 2 html table colums like a loop? I can't make it work. Or do I need a different approach?

I need this to echo like a loop into 2 columns <?php echo $row['data']; ?>

This is what I have:

HTML Table
Col 1    |    Col 2
1. aaaa
2. bbbb
3. cccc
4. dddd
5. eeee
6. ffff
7. gggg
8. hhhh
9. iiii
10. jjjj

This is what I want:

HTML Table
Col 1         |     Col 2
1. aaaa             6. ffff
2. bbbb             7. gggg
3. cccc             8. hhhh
4. dddd             9. iiii
5. eeee             10. jjjj
2
  • stackoverflow.com/questions/13838755/… Commented Apr 30, 2019 at 2:16
  • @Chocoprins Nice scenario . . . I answered Your expected output let me check if you have any concern come back with comments . . . Commented Apr 30, 2019 at 3:46

2 Answers 2

1

Here code is for creating dynamic column and data, that might any length,

Splitting into 5 groups and make it as column value. Worked output is below

<?php
 //$row=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
 $row=array('aaa','bbbb','cccc','ddddd','eeeee','ffff','gggg','hhhh','iiii','jjjjj','kkkk','llll','mmmm','nnnnn','ooooo');
?>
<table style="width:50%">
<?php
echo'<tr>';
$i=0;
foreach ($row as $key => $value) {
    if(($key)%5==0)
    {
        $i++;
        echo'<th>Col'.$i.'</th>';
    }
    $a[$i][]=$value;        
}
echo'</tr>';
$forcount=count($a);
$innerforcount=count($a[1]);
for ($j=0; $j <$innerforcount ; $j++) {
    echo'<tr>';
    for($i=1;$i<=$forcount;$i++)
        echo'<td>'.$a[$i][$j].'</td>';
    echo"</tr>";
}
?>
</table>

//Output

 Col1   Col2    Col3    Col4
1   6   11  16
2   7   12  17
3   8   13  18
4   9   14  19
5   10  15  20

//Sample out with text

 Col1   Col2    Col3
aaa     ffff    kkkk
bbbb    gggg    llll
cccc    hhhh    mmmm
ddddd   iiii    nnnnn
eeeee   jjjjj   ooooo
Sign up to request clarification or add additional context in comments.

2 Comments

Rasa thank you very much but the array's are not actualy number but texts in 20 steps.
@Chocoprins18 Edited my answer check
1

It's absolutely possible.

All you need to do is echo out alternating classes, which can be done by making use of a counter which increments as you loop over the rows. For example:

<?php

$count = 0;
foreach ($rows as $row) {
   $count++; // Increment a counter
   if ($count % 2 == 0 && $count != count($rows)) {
     echo "<div class='odd'></div>";
   }
   else {
     echo "<div class='even'></div>";
   }
}

?>

And from here you can use CSS to style the rows into two columns:

.odd, .even {
  width: 50%;
  float: left;
}

Or if you want to do this with pure CSS, you can make use of flexbox. All you need is display: flex and flex-wrap: wrap on the parent, along with flex: 50% on the children:

.container {
 display: flex;
 flex-wrap: wrap;
}

.container div {
  flex: 50%;
}
<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
  <div>9</div>
  <div>10</div>
</div>

Which will also allow you to fill the left-hand column first if you want, by making use of flex-flow: column wrap and a fixed height:

.container {
 display: flex;
 flex-flow: column wrap;
 height: 100px;
}
<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
  <div>9</div>
  <div>10</div>
</div>

5 Comments

Thank you for your explanation. I understand your explanation 100%. I want to integrate this into DOMPDF so your entire example must be inside $html = 'YOUR EXAMPLE'; And it is here that it starts to get tricky in my way. So I don't know how to handle this and make it work.
DomPDF is basically a web parser; you simply add your entire HTML structure into it. And that includes the <head> tag, which in turn can include in-line CSS styling. You'd want something like $html = "<html> <head> <style> {{STYLES HERE}} </style> </head> <body> ... </body>. You'll probably want to save your entire 'body' as an independent (string) variable, and inject it as a part of the $html string: $html = <html> ... </head><body>" . $body . "</body>";.
Yes but your example does not work inside the $html
Hmm, according to this answer, DomPDF does not support flexbox (or floats). You're likely stuck with inline-block elements. As I'm not familiar with DomPDF myself, I can't help much with the specific formatting you'll need sorry, but the concept is the same. I'll add the tag to the question.
Ok no problem. Can you tell me where I need to put the <?php echo $row['data']; ?> in your example. Im not 100% sure but the example is a counter so I don't have actually numbers but text in steps, so around 20 steps. The focus must be now on the table row instead of the counter if im right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.