CodeIgniter Forums
How to do this? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to do this? (/thread-53503.html)

Pages: 1 2


How to do this? - El Forum - 07-26-2012

[eluser]Mahn[/eluser]
Ahoy,

I'm currently trying to make a small forum software, so on the homepage I want to list categories and the forums that belong to the category underneath.

I'm used to making a while loop inside a while loop, so I have no idea how to do this with CodeIngiter.

Please let me know if that didn't make much sense Smile


How to do this? - El Forum - 07-26-2012

[eluser]InsiteFX[/eluser]
foreach



How to do this? - El Forum - 07-27-2012

[eluser]Mahn[/eluser]
Yes, but the query will be different each time. Something like
Code:
SELECT * FROM forums WHERE category_id = $category['id']



How to do this? - El Forum - 07-27-2012

[eluser]DarkManX[/eluser]
you wont be able to avoid learning php. you should start by pure php without frameworks etc. there are enough good tutorials.


How to do this? - El Forum - 07-27-2012

[eluser]Mahn[/eluser]
@DarkManX: Why would you assume that? I've done this many times before with procedural PHP. I just can't make sense of how to do this with a model.


How to do this? - El Forum - 07-27-2012

[eluser]InsiteFX[/eluser]
Controller
Code:
$data['cats'] = 'get the query from your model';
$this->load->view('your view', $data);

// View
<ul>
    &lt;?php foreach ($cats as $cat): ?&gt;
        <li>&lt;?php echo $cat; ?&gt;</li>
    &lt;?php endforeach; ?&gt;
</ul>

Something like that.



How to do this? - El Forum - 07-28-2012

[eluser]Mahn[/eluser]
@InsiteFX: I've already got the categories done, it;s just the forums that are a bit tricky because the query changes every time.


How to do this? - El Forum - 07-28-2012

[eluser]DarkManX[/eluser]
The model just returning you the date from the db. fetch them all in to an array and give it all to the view.


How to do this? - El Forum - 07-28-2012

[eluser]Mahn[/eluser]
@DarkManX: Yes, but the query changes for every category, so I can't just query for everything. The query will look something like:
Code:
SELECT * FROM forums WHERE category_id = $category['id']



How to do this? - El Forum - 07-28-2012

[eluser]DarkManX[/eluser]
Just get all categories and create a db-request for each of them. as i told, you wont be able to avoid learing php.

Code:
$all_cats = // get all cat, how every you want/got to
foreach($all_cats as $cat){
   $data['all_my_stuff'][] = // getting ur stuff with $cat
}
$this->load->view('view',$data);

this way you could use $all_my_stuff in the view.