Part of the EllisLab Network
   
 
CI Screencast 2 - Dynamic Routing, Models, and Navigation
Posted: 29 March 2007 02:17 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  03-26-2006

As promised, here’s the second in my series of screencasts to develop a basic CMS…
You can get all the links from my blog post.

Hopefully I’ve sorted out:
Audio syncing,
Audio Volume

Next time I’ll plug my laptop in before I get started :D

sorry this has taken so long to get ready.

Please provide any feedback you can.

The next one will be a bit more ‘meaty’. (User logins, and now a new ‘modular’ directory structure)
There is a little skipping on this audio, but overall, I think it’s much better than the first.

A few times I come across errors and have to do a little debug… but I’d rather do that than have ‘perfect’ development.
At least this way you get to see some ‘real’ development.

so…. go get the screencast!

Elliot

 Signature 

On the first day, God created CodeIgniter… Then he could really get some work done!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | Web Development Screencasts
Twitter | Flickr

Profile
 
 
Posted: 29 March 2007 04:36 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Elliot, you’re a natural with teaching. Keep up the great work! Thanks for your continued contributions.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 29 March 2007 06:07 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  81
Joined  02-06-2007

good job on this
i’m starting to like the idea for using db driven pages smile

i got some questions about the nav
if u want to add sub links
how would u do it?

something like this maybe?

if ( $nav['parent'] != NULL )
{
   $this
->nav->getParent()
   <
li>bla bla</li>
}

and i get confused too if we have a user system
how we can set the links to show to only certain groups

i’m not used to do navs from db has u can see raspberry

Profile
 
 
Posted: 29 March 2007 07:34 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  03-26-2006

Well, I will be introducing sub-pages in time…
it works off a parent_id int field in the pages db.

Basically…

I’d do something like this:

// First off, top level pages only (no parent)

$this->db->where('active', true);
$this->db->where('parent_id', 0);
$query = this->db->get('pages');

if (
$query->num_rows() > 0)
{
    
// Build a top-level array up
    
$top = $query->result_array();
    
    foreach (
$top as $item)
    
{
        
// Add to a new array with the id as the index..

        
$this->template['top_nav'][$item['id']] = $item;

    
}
    
    $this
->db->where('active', true);
    
$this->db->where('parent_id >', 0);
    
    
$query2 = $this->db->get('pages');
    
    if (
$query2->num_rows() > 0)
    
{
        $sub
= $query2->result_array();
        
        foreach (
$sub as $item )
        
{
            
// Test if parent is active and in the top level.

            
if ( isset($top_nav[$item['parent_id']] )
            
{
                 
// Add to subnav array with index as the parent's id

                 
$this->template['sub_nav'][$item['parent_id'][] = $item;
            
}
        }
    }
}

That will give you 2 arrays: top_nav and sub_nav… each indexed by the parent’s id…

Then the view will be something like this:

<ul id="nav">
<?php foreach ($top_nav as $item): ?>
<li><?=$item['menu_title']?>
    <?php
if (isset($sub_nav[$item['id']]): ?>
        
<ul>
        
<?php foreach ($sub_nav[$item['id']] as $sub_item): ?>
            
<li><?=$sub_item['menu_title']?></li>
        
<?php endforeach; ?>
        
</ul>
     
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>

Although it’s far from polished, you can see the basic idea.

Hope this helps (until I bring this out in a screencast)

Elliot

 Signature 

On the first day, God created CodeIgniter… Then he could really get some work done!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | Web Development Screencasts
Twitter | Flickr

Profile
 
 
Posted: 29 March 2007 08:43 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  240
Joined  11-10-2006

Good one. I especially love the part where you run for your charger! :D

Some critique, though:
—> Try to use less “negating” statements. For example you could rewrite the in_array thing in your routes with

if(in_array($file, $exclude)) continue;

—> I think it’s good to show your mistakes while coding, but you should think about some things BEFORE screencasting such as the substring function you’re using ... You want to take off the extension of a file - then the command is not substr($file, 0, strlen($file)-4) (imagine .phtml - I know this is not valid in CI, but anyway) but rather something like

substr($file, 0, strpos($file, "."));

Or maybe strpos()-1, I’m too lazy to try. It looks for the first appearance of “.”.
—> Try to keep your coding style consistent, especially regarding indentation, blank lines, etc. ...
—> If you try to educate people, be a good teacher and make them good programmers: If your page->get() method returns an array in case of success it should return an empty array in case of error (stay with your datatype!). And don’t use constructs like if(isset($blah) && !empty($blah)) ... empty return false if something is not set (!isset). Lastly, try to avoid result_array() and use the object method (result()) as often as possible - after all, we’re OOP, right? wink

But, as the others say, it’s different from other screencasts, which makes it automatically more interesting to watch! smile
Cheers

 Signature 

blog

Profile
 
 
Posted: 29 March 2007 08:57 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  03-26-2006

Hi Clemens…
Thanks for the feedback.

Try to use less “negating” statements. For example you could rewrite the in_array thing in your routes with

I tend to write things as they make sense… for example:
in the real world, you would say:
“if you don’t have any milk, buy some from the shop”,
not
“if you have milk, don’t buy some from the shop”

which is why i would write the first of the following statements:

while (stuff) {

   
if ( !$milk )
   
{
      buy some
   }

}
while (stuff) {

   
if ($milk) continue;

   
buy some

}

I tend to write things in a way that makes more sense if it were real english, I guess everyone’s just different.

Thanks for the rest of the feedback, I’ll look into it, (particularly the result_array…
Overall, I’m pretty young and still refining my skills.
This screancasting is as much a learning experience for me as it is a teaching one.

Elliot

 Signature 

On the first day, God created CodeIgniter… Then he could really get some work done!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | Web Development Screencasts
Twitter | Flickr

Profile
 
 
Posted: 29 March 2007 09:16 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  248
Joined  02-10-2007

Thanks Elliot. Love your screencasts. I find it interesting just to see someone else’s CI coding practices.


Here’s another tip Elliot.

—> controllers/pages.php (around line 25):

if (!$path = $this->uri->segment(1)) {
    $path
= 'home';
}

Can be optimized to:

$path = $this->uri->segment(1, 'home')

If uri->segment(1) would return FALSE, the function will now return the second argument you provided to it. Just another small detail that helps keeping your code neat.


Concerning result_array(), I’m wondering what’s wrong using that function instead of just result()? Personally I do prefer to work with query results as arrays. Seems like a more flexible solution. If you could provide some clear advantages of working with the OOP results, Clemens, please do so.

 Signature 

Kohana rocks!

Profile
 
 
Posted: 29 March 2007 09:18 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  240
Joined  11-10-2006

Been there, know that! smile That’s why I give you feedback. I started teaching Java and PHP when I started studying at my Uni (at 18) and I have been teaching for the past 2 years.
Cheers

 Signature 

blog

Profile
 
 
Posted: 29 March 2007 10:04 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  03-26-2006

Thanks for this optimization:

$path = $this->uri->segment(1, 'home')

I completely forgot about that!!

Elliot

 Signature 

On the first day, God created CodeIgniter… Then he could really get some work done!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | Web Development Screencasts
Twitter | Flickr

Profile
 
 
Posted: 30 March 2007 12:21 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  3
Joined  03-24-2007

Thanks for this part Elliot!
I have a problem, and i can’t solve it yet.
I have followed Your instructions, and when You have added the admin folder, and pages.php in it and finalized, my page was not found.
I copied the routes.php from Your uploaded version, but no success.
Maybe my Apache’s settings are wrong? I tried any option in CI, URI Protocol, but nothing.
Any idea? I missed something?
Thanks for Your time.

Profile
 
 
Posted: 30 March 2007 05:21 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  81
Joined  02-06-2007

in the files that are for download there is a bug raspberry
its missing the word index in line 93 at routes

change it for this -> $route[’(.*)’] = “pages/index/$1”;

elliot forgot to change it when he was trying to put scaffolding working.
if u do this, the download files work just fine smile

Profile
 
 
Posted: 30 March 2007 11:09 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Avatar
Total Posts:  3
Joined  03-24-2007

I did it Wemago, but no success.  confused

I have found a solution: (Suggested in Recursive Routing topic.)

in routes.php I have changed:

$route[$module . "/(.*)"] = $module . "/$1";

into:

$route[$module . "(.*)"] = $module . "$1";
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120442 Total Logged-in Users: 45
Total Topics: 126530 Total Anonymous Users: 6
Total Replies: 665324 Total Guests: 354
Total Posts: 791854    
Members ( View Memberlist )