Welcome Guest, Not a member yet? Register   Sign In
How to output database to table formated into rows and columns
#1

[eluser]brandontran[/eluser]
I have a database with 8 entries. How can I get the following database output to show a table with 2 rows?

I am trying to display those entries like:
Code:
<table><tr>
<td>title 1</td>
<td>title 2</td>
<td>title 3</td>
<td>title 4</td>
</tr><tr>
<td>title 5</td>
<td>title 6</td>
<td>title 7</td>
<td>title 8</td>
</tr></table>

My controller looks like this:
Code:
&lt;?
class Main extends Controller {
    function Main()
    {
        parent::Controller();    
        $this->load->helper("url");
    }
    
    function index()
    {
        $this->load->database();
        $data['query'] = $this->db->get('photo', 8,0);        
        $this->load->view('main/home' );
    }
}

My view looks like this:
Code:
&lt;? foreach ($query->result() as $row): ?&gt;
<td>&lt;?=$row->photo?&gt;</td>
&lt;? endforeach; ?&gt;

My second question is, I feel that maybe the database call needs to go into a model. While the current setup works, is it correctly setup with the codeigniter concept?

Thanks for the help guys!
#2

[eluser]WanWizard[/eluser]
Whether or not to put database I/O in a model is up to you. CI gives you that freedom. I usually do so, unless it's obvious that the code is never going to be reused.

As for your view, you need to insert table rows for every four records. The easiest way is to use a row counter, and do a modulo 4 on the counter to determine when to insert a new table row. Don't forget to start with a <tr> (which is not in your example), and to add a </tr> at the end to close the last row.
#3

[eluser]huzzel[/eluser]
Hi,

i prefer this :-)
Code:
// generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('No', 'Name', 'Gender', 'Date of Birth (dd-mm-yyyy)', 'Actions');
        $i = 0 + $offset;
        foreach ($persons as $person){
            $this->table->add_row(++$i, $person->name, strtoupper($person->gender)=='M'? 'Male':'Female', date('d-m-Y',strtotime($person->dob)),
                anchor('person/view/'.$person->id,'view',array('class'=>'view')).' '.
                anchor('person/update/'.$person->id,'update',array('class'=>'update')).' '.
                anchor('person/delete/'.$person->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')"))
            );
        }
        $data['table'] = $this->table->generate();
#4

[eluser]WanWizard[/eluser]
I can imagine. Smile

However, CI doesn't have a 'table' library out of the box, so unless you can provide the OP with this library, your response isn't of much use...

And I see no answer to the OP's question, which is how do I display records in a table in groups of 4?
#5

[eluser]brandontran[/eluser]
Thanks a ton!

Here is the solution I came up with for my view:
It works for me. I tried to get the database call into a model, yet I am a little unclear on how to do that just yet. Will have to wait for tips on how to do that. I read somewhere it's good to do database calls in the models and not the controller.

Code:
<table><tr>
    &lt;? foreach ($query->result() as $i=>$row): ?&gt;
    <td>&lt;?=$row->file?&gt;<br/>Photo</td>
    &lt;? if ($i == "3"): echo "</tr><tr>"; endif; ?&gt;
    &lt;? endforeach; ?&gt;
</tr></table>
#6

[eluser]WanWizard[/eluser]
The main reason for putting your I/O code in a model is that is it easy to reuse, and it hides complexity from your controllers. It also allows you to make changes to your database environment, without affecting your controller code (as long as you don't change the return values of the methods in your model).

In your case, create a model, and in that model a method called get_my_pics(). You could pass some variables to the method if you want to make selections, run the query, and have the method return the $query->result().

In the above example, you could then use:
Code:
&lt;? foreach ($this->mymodel->get_my_pics() as $i=>$row): ?&gt;

Also, I don't think comparing $i to "3" (which is a string) is a good idea. And what happens if $i gets to 7? Another issue is that if the query returns an exact multiple of four, you end up with an empty table row with no cells, which doesn't validate.

I would do something like:
Code:
<table>
    &lt;? foreach ($query->result() as $i=>$row): ?&gt;
    &lt;? if ($i % 4 == 0): echo "<tr>"; endif; ?&gt;
    <td>&lt;?=$row->file?&gt;<br/>Photo</td>
    &lt;? if ($i % 4 == 3): echo "</tr>"; endif; ?&gt;
    &lt;? endforeach; ?&gt;
    &lt;? if ($i % 4 != 3): echo str_repeat("<td></td>", 3-($i % 4)); endif; ?&gt;
</table

This is from the top of my head, there are probably better solutions. Also, I never mix html and php (I don't use views), so I'm not sure of the correct syntax.
#7

[eluser]brandontran[/eluser]
[quote author="WanWizard" date="1251572903"]The main reason for putting your I/O code in a model is that is it easy to reuse, and it hides complexity from your controllers. It also allows you to make changes to your database environment, without affecting your controller code (as long as you don't change the return values of the methods in your model).

In your case, create a model, and in that model a method called get_my_pics(). You could pass some variables to the method if you want to make selections, run the query, and have the method return the $query->result().

In the above example, you could then use:
Code:
&lt;? foreach ($this->mymodel->get_my_pics() as $i=>$row): ?&gt;

Also, I don't think comparing $i to "3" (which is a string) is a good idea. And what happens if $i gets to 7? Another issue is that if the query returns an exact multiple of four, you end up with an empty table row with no cells, which doesn't validate.

This is from the top of my head, there are probably better solutions. Also, I never mix html and php (I don't use views), so I'm not sure of the correct syntax.[/quote]

There is a limit on the pull. It's an 8 photo template for the site. So I just needed it split at 4 entries, new row and 4 more entries. So it won't ever go over 8 unless we redesign the template. Now that fact you made about $i being a string. In the old php method of coding with while loops $i=0; and $i++ it was a variable so that was my thinking. But it concerns me it being a string in this aspect. I need to look into that to make sure and find out what the right technique will be. We will be doing some testing to make sure it processes fast before all is said and done.
#8

[eluser]alboyd[/eluser]
[quote author="WanWizard" date="1251560657"]I can imagine. Smile

However, CI doesn't have a 'table' library out of the box, so unless you can provide the OP with this library, your response isn't of much use...

And I see no answer to the OP's question, which is how do I display records in a table in groups of 4?[/quote]

Say What?

But nevertheless that post was a little "out there"...
#9

[eluser]WanWizard[/eluser]
Ah, the thing is called 'HTML table class' in the docs. Overlooked it.

I don't use the traditional CI views (I don't mix HTML and PHP in my projects), so I have never used any HTML related libraries.

My apologies... :red:




Theme © iAndrew 2016 - Forum software by © MyBB