Welcome Guest, Not a member yet? Register   Sign In
Simple select statement and displaying results Problem
#1

[eluser]bibbles10504[/eluser]
I have a pretty simple question, but for some reason I am drawing a blank. I have the following code in my view file, and I want to display the results in a two column table, so the first entry would be on the left, the next would be on the right then the next one after that would be below the first row, and eventually I will use the pagination class (haven't gotten that far yet) For some reason I can not figure out how to get the results to display in a 2 column format... only one. Any help would be greatly appreciated.

Ideally I would like to have 4 columns...

Thanks!

Code:
<table>
&lt;?php
         $sql = "SELECT * FROM PortItems WHERE Cat = 'Web Design'";
        $query = $this->db->query($sql);
        foreach ($query->result() as $row)
        {
        echo("<tr>");
        echo("<td>");
            
            echo $row->Title;
            echo ("<br/>");
            ?&gt;
            <img >Thumb;?&gt;" src="../uploaded/portfolio/thumbs/&lt;?php echo $row->Thumb;?&gt;"   alt="">
            &lt;?php
            echo("<br/>");
            echo $row->DescText;
        echo("</td>");
        echo("<td>");
        
        // Display next picture here
        echo("</td>");
        echo("</tr>");
        }
        ?&gt;
</table>
#2

[eluser]mddd[/eluser]
Some pointers:
1. It is better to do your database stuff in the controller or model and use the view only to do output.
2. You could solve the problem by using DIV elements in stead of TD's in a table. If you make the DIVs almost 50% wide and give them float: left; the will nicely follow each other without you having to worry about them being first or second on the row. Basic example of output:
Code:
<div class="itemlist">
<div class="item">Here is your first item</div>
<div class="item">Here is your second item, it could be on the same line</div>
<div class="item">Here is your third item, it could be on a new line</div>
<div class="item">Here is your fourth item</div>
</div>
3. If you want to keep the table structure, you should check after displaying an item to see if its count is odd or even. If it is even, you can put /TD /TR TR TD, if it is odd, just put /TD TD because there will be one more on the line. A basic example:
Code:
<table>
<tr><td>
&lt;?php
$counter=0;
foreach($items as $item)
{
$counter++;

// output your $item here: echo $item->title etc.

if ($counter%2)
{
  // it is an odd numbered item, so it will be followed by another on this line
  echo '</td><td>';
  // was this the last item? then insert a blank item and end the row
  if ($counter==sizeof($items)) echo '&nbsp;</td></tr>';
} else {
  // it is an even numbered item, so it will end a line
  echo '</td></tr>';
  // are there any more items? then start the new line
  if ($counter<sizeof($items)) echo '<tr><td>';
}
// ready. end the table
?&gt;
</table>




Theme © iAndrew 2016 - Forum software by © MyBB