Welcome Guest, Not a member yet? Register   Sign In
Menu Model
#1

[eluser]Think Floyd[/eluser]
Hi all,

First of all I'd like explain that I'm new to the MVC design pattern, so apologies.

Anyhow, I have a Menu (drop down style) which I would like to include in all my views. I'm going to use a nested view approach, so at each controller I will just load one view - "container" or something of the like - which will include the Menu View.

OK, here's my problem:

The menu is dynamic: Cats & Subs are drawn from the DB. So if I create a model called MenuModel, should this return the "whole" menu (i.e everything including the HTML elements), or just the result from the query.

I understand you shouldn't use any View code within a model, but I'm unsure how to display the information from the Model in the View without doing something like this (bad pseudo code, ignore semantics):

Code:
Select * from cats
    <ul>
    foreach cat as cat{
        <li>
        echo $cat

        Select * from subs where cats_sub = cat
        foreach sub as sub {
            <ul><li> echo sub</li></ul>
        }
        </li>
    }
    </ul>


Any help appreciated.
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Why not use a model to get the data, and a library/helper to do any formatting and return the HTML?
#3

[eluser]Think Floyd[/eluser]
Thanks for the swift response.

I don't think I'm being all that clear.

I guess my true problem lies in in returning the data. I'm new to PHP and my multi-dimensional array knowledge needs polishing.

If I did an SQL JOIN in my query which would return the category -> and all sub categories of that category, I am then not sure how to iterate through this data in my view.

So I guess what I'm after would look something like this:

$data array(
category 1=> array (subcategory1, subcategory 2, etc)
category 2=> array (subcategory1, subcategory 2, etc)
)

Does that make sense?
#4

[eluser]TheFuzzy0ne[/eluser]
Are the categories only one level deep?

If you'd be so kind as to post your table schema, I'll give it a bash for you.
#5

[eluser]Jelmer[/eluser]
For more flexibility take a look at my Generic list generator, and the example I gave to create an input array recursively - which allows for unlimited levels.

http://ellislab.com/forums/viewthread/97102/#490938

It could become a bit load heavy when there are many levels, but that's what I created my cache library for.
#6

[eluser]Think Floyd[/eluser]
That's most generous.

I've written the code in a "scripting" style, but I'm really trying learn a framework and Design Pattern in the vein attempt to encapsulate and modularise my code.

DB structure:

TABLE: category

id
title

TABLE : subcategory
id
title

JOIN TABLE: categories_subcategories
category_id
subcategory_id

Code:
$sql = "Select * From categories ORDER BY title";
$res = mysql_query ( $sql );

// Category Links
echo "<ul id='catmenu' class='catmenu'>";
While ( $row = mysql_fetch_array ( $res ) )
{    
    $sql_sub = "Select sc.* From (subcategories sc, categories_subcategories csc) ";    
    $sql_sub .= "WHERE csc.subcategory_id = sc.id ";    
    $sql_sub .= "AND csc.category_id = " . $row ['id'] . " ";    
    $sql_sub .= "ORDER BY sc.title";    
    $res_sub = mysql_query ( $sql_sub );
            
    echo "<li><a href='" . $url_path . "/category/index.php?cat=" . $row ['><span>" . $row ['title'] . "</span></a>";
    echo "<ul>";
        
    // Sub Category Links
    if (mysql_num_rows ( $res_sub ))
    {
        While ( $row_sub = mysql_fetch_array ( $res_sub ) )
        {
        
            echo "<li><a href='" . $url_path . "/category/index.php?cat=" . $row ['sub=" . $row_sub ['id'] . "><span>" . $row_sub ['title'] . "</span></a>";
        }
    }
    echo "</ul>";
    echo "</li>";
}
echo "</ul>";
#7

[eluser]jedd[/eluser]
[quote author="Think Floyd" date="1236884865"]
DB structure:

TABLE: category

id
title

TABLE : subcategory
id
title

JOIN TABLE: categories_subcategories
category_id
subcategory_id
[/quote]

May I suggest that life would be simpler, and this is true IFF you are sticking with two levels here, to have two tables instead. Roughly like:
Code:
CREATE TABLE category
     id          SERIAL,
     title       VARCHAR

CREATE TABLE subcategory
      id         SERIAL
      cat_id     BIGINT    // FK to category.id of course
      title      VARCHAR

Your current schema only allows two levels deep anyway because of the limitations of the 'join' table.

If you wanted to get excited you could have a single table that had a parent ID column (0 denotes category, etc). This is pretty standard stuff, though.

Point being, your selects would be easier, and consequently your code would be easier (read: your problem would be easier).
#8

[eluser]Think Floyd[/eluser]
Thanks for your suggestion jedd. The categorisation is actually 4 levels deep, but I haven't mentioned this as it adds yet more confusion to the mix.

Once I've worked it out for 2 levels, the other 2 will be simple.

My problem lies in running the query in the model and returning the relevant category-subcategory association to the View.

It confuses me slightly :/
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="Think Floyd" date=""]Once I’ve worked it out for 2 levels, the other 2 will be simple[/quote]
Unfortunately this is not the case. You will then have to have a loop for each level of subcategories, and making expanding it makes it more complicated and more resource intensive.

You may wish to consider looking into MPTT (Modified Preorder Tree Traversal). You'll need to change your database layout, but the effect is that you can get everything you need in only two queries. [url="http://ellislab.com/forums/viewthread/74114/"]Here's a library for CodeIgniter[/url]
#10

[eluser]jedd[/eluser]
[quote author="Think Floyd" date="1236886811"]Thanks for your suggestion jedd. The categorisation is actually 4 levels deep, but I haven't mentioned this as it adds yet more confusion to the mix. [/quote]

I agree. It does.

Quote:Once I've worked it out for 2 levels, the other 2 will be simple.

I disagree. If you are using your approach above - having a 'join table' (probably a bad choice of word, given join means something specific in sql) to connect two levels, then you are either going to extrapolate that - and have THREE relation tables to connect these FOUR category tables - OR you are going to have to approach it differently.

I doubt you were seriously considering having 7 tables just to display a menu. (Were you?)

I think you need to re-think the schema now, and design for that. Rather than ponder an unattractive schema and then try to scale that up into something even more so later.

Quote:My problem lies in running the query in the model and returning the relevant category-subcategory association to the View.

I understand this. I think it would be easier if you approach the problem as a whole, and considered all aspects of the design at the same time. Especially true since you're not trying to maintain any extant code.

Your queries become almost criminally easy if you have a single table with three columns. Populating an array, and then displaying that array, both become comparably easy as a result.




Theme © iAndrew 2016 - Forum software by © MyBB