• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outputting results to a table

#1
[eluser]zuluanshee[/eluser]
I have a list of countries, europe, say. I want to output them in alphabetical order, but within each column so the table will look like this
Code:
<table>
<tr>
  <td>Austria</td>
  <td>Denmark</td>
  <td>France</td>
  <td>Hungary</td>
</tr>
<tr>
  <td>Belgium</td>
  <td>England</td>
  <td>Germany</td>
  <td>Iceland</td>
</tr>
<tr>
  <td>Czech Republic</td>
  <td>Finland</td>
  <td>Greece</td>
  <td>Ireland</td>
</tr>
</table>


I guess i understand how to program it, but how do I use the table library? I looked at the documentation but i don't know which stuff happens in the model and which happens in the view.

Here is what I have:
model
Code:
&lt;?php

class CountriesModel extends Model
{
    function getCountries()
    {
        $query = $this->db->query("SELECT country_name FROM countries WHERE continent_name = 'Europe'");
        
        if($query->num_rows() > 0)
        {    
            foreach($query->result() as $row)
            {
                $data[] = $row;
            }
        }    
        return $data;
    }
}
?&gt;
vew
Code:
&lt;?php foreach($countries as $row):?&gt;                
  <p>&lt;?php echo $row->country_name; ?&gt;</p>            
&lt;?php endforeach; ?&gt;

controller
Code:
&lt;?php

class Continents extends Controller
{
    function Continents()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->model('countriesmodel');
        $data['countries'] = $this->countriesmodel->getCountries();
        $this->load->view('continentview', $data);
    }    
}
?&gt;

#2
[eluser]zuluanshee[/eluser]
I found a workaround, but its unsatisfactory, because it contains echoed tags '<td>';
I know you're not supposed to echo html, but I can't think of a better way off the top of my head.

view
Code:
<table id="countries">
  &lt;?php $row_size = 6; $current = 1;?&gt;
  &lt;?php foreach($countries as $row):?&gt;
  &lt;?php if($current == 1)
       {
     echo '<tr>';
                                
       }                    
  ?&gt;
    <td>&lt;?php echo $row->country_name; ?&gt;</td>
  &lt;?php echo $current++;
        if($current == $row_size)
        {
      echo '</tr>';
      $current = 1;
                            }
  ?&gt;        
  &lt;?php endforeach; ?&gt;
</table>

#3
[eluser]jedd[/eluser]
The Table library is a curious thing - it generates HTML in the Controller. For this reason (plus it's not that good at doing anything out of the ordinary) a lot of people ignore it, and end up writing their own view partials that generate the table(s) they want. Over time you can abstract them out such that they become nicely generalised, but there's nothing hugely wrong with writing a view file that just generates this particular table.

#4
[eluser]alboyd[/eluser]
So what is your question?

is it to do with the three columns and having them as a b c // d e f // g h i like that or??

It should be fairly easy to do that. I would take your countries data in the view and figure out how many columns you want, then based on the number of countries in the countries array you can figure out how many to put in each column, then you just do a foreach and output them, creating a new column when a counter reaches the predetermined value (number of countries per column).

Just make sure also that your query orders the countries in alphabetical order..

let me know if you need some more explanation or if I am not understanding your question...

EDIT: and as Jedd said - I wouldn't bother using the table library/helper IMO it's just easier to write the table HTML yourself. There should be no need for you to "echo" HTML though?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.