Welcome Guest, Not a member yet? Register   Sign In
New to CI, have some questions
#1

[eluser]Kilroy[/eluser]
Hello!

I recently discovered CodeIgniter, and for the last couple of days I've been playing around a bit with it. My plan is to find a nice php framework that allows me to separate presentation, data and logic and eventually migrate an entire website to it.

However, I encountered problems right away. For creating the main menu, I have a system where the "current" link gets a class tag, like this: <a href="something.php" class="active">Some link</a>. This is based on the filename, and the logic looks something like this: if($filename == 'something.php') echo 'class="active';

Now, I wouldn't know how to do this with CI, since it always loads from index.php. My first idea was to compare it to the first segment of the URI instead of the filename, but that wouldn't work either, since having a default controller permits URIs without a first segment.

My second idea was to set a variable in the controller, so the view will know which controller called it. I haven't tried this yet, but I am sure it would work, even though it would not be a very elegant solution. How do you solve this?

Anyway, on to my next question. I have quite lots of things that simply need fetching from a database. Say I want to get the content-field from a table called 'news' where the id is 5. Right now, I do it this way (don't mind the tinymce-code):
Code:
$row = $this->db->where('id', 5)->get('news', 1);
$row = $row->row();
$data['news'] = $this->tinymce->echo_tinymce($row->content);
It gets sort of repetative if you have 5-6 (or even more) of these per controller. Tongue Is there a better way of accomplishing this?

Of course there aren't only drawbacks with CodeIgniter, it would probably be a huge lift for my administration system.

Just a last question. If I have tons of data that I need to pass to the view, is passing it through $this->load->view('someview', $data); the only way? It seems like this is a huge waste of memory.

Kilroy
#2

[eluser]xwero[/eluser]
1) you could create a helper function
Code:
function set_current($search,$match,$html = ' class="active"')
{
   if($search == $match)
   {
      echo $html;
   }
}
The usage will be
Code:
//controller
function page()
{
    // load helper here or in the construct method or autoload
    $data['current'] = 'page';
    $this->load->view('page',$data);
}
// view
<ul>
  <li><a href="page"&lt;?php set_current('page',$current); ?&gt;></li>
  <li><a href="page2"&lt;?php set_current('page2',$current); ?&gt;></li>
  <li><a href="page3"&lt;?php set_current('page3',$current); ?&gt;></li>
</ul>

2) It's best to put your database queries in models so more than one controller/library can call them
Code:
// model
function news_content($id)
{
  $row = $this->db->where('id', $id)->get('news', 1);
  $row = $row->row();
  return $row->content
}
// controller
$this->load->model('News','',true);
$data['news'] = $this->tinymce->echo_tinymce($this->News->news_content(5));
#3

[eluser]adamp1[/eluser]
Hi and welcome to the Community (hopefully for a while if you get to see why we love CI).

However, I encountered problems right away. For creating the main menu, I have a system where the “current” link gets a class tag
Normally for the thing with the menu being made active. I do as you say. For each one I just search the uri for the controller name and apply an 'active' class to the link.

But as you said having a default controller gets around this, well why not instead of using $this->uri->uri_string() use $this->uri->ruri_string(). That way you get the routed uri address and this will always have a controller in it.

I have quite lots of things that simply need fetching from a database.
As said use models, means the same code is only written once.

If I have tons of data that I need to pass to the view, is passing it through $this->load->view(’someview’, $data); the only way? It seems like this is a huge waste of memory.
I suppose if you didn't want to pass a whole new variable and waste double the space you could do:
Code:
$this->load->view(’someview’, &$data);
Pass it by reference?
#4

[eluser]xwero[/eluser]
Of course adamp1 that is even simpler
Code:
function set_current($match,$html = ' class="active"')
{
   $ci = & get_instance
   if($ci->uri->uri_string() == $match) // or ruri_string()
   {
      echo $html;
   }
}
The controller will not need to pass any value, The view would be
Code:
<ul>
  <li><a href="page"&lt;?php set_current('/controller/page'); ?&gt;></li>
  <li><a href="page2"&lt;?php set_current('/controller/page2'); ?&gt;></li>
  <li><a href="page3"&lt;?php set_current('/controller/page3'); ?&gt;></li>
</ul>
[/code]
#5

[eluser]adamp1[/eluser]
Thing is won't it have to use $this->uri->ruri_string() otherwise if no controller is given then the default route will be taken, and uri_string() won't return anything. But ruri_string() will.
#6

[eluser]pickledegg2[/eluser]
I'm learning jquery for adding styles to html on the fly, so far I've been blown away by it. You can style an entire document without adding any classes into the html directly, very flexible.

I've PM'ed you Kilroy
#7

[eluser]adamp1[/eluser]
Problem is what if they don't have javascript enabled? Then your document has no styling and it means even more overhead.
#8

[eluser]Kilroy[/eluser]
Thanks for your quick replies! Smile

I am so new I apparently forgot all about Models. :red: Thanks for the tip! If I use models, I can reduce those three lines of code to just one:
Code:
$data['news'] = $this->news->get_latest();
A lot cleaner!

As for the menu thing, I didn't know about ruri_string, and thank you again for the tip on that function!

I used to utilize a function to generate the menu, it looks like this:
Code:
function menu_generator($menu_id, $menu) // menu_generator("menu", array("Start" => "index.php", "Users" => "users.php", "Functions" => "functions.php"));
{
    echo "<ul id=\"{$menu_id}\">\n"; // initialize the list with <ul id="$menu_id">
    
    foreach($menu as $key => $value) // loop through the menu
    {
        $css_active = basename($_SERVER['PHP_SELF']) == $value ? ' class="active"' : '';
        
        echo "\t<li{$css_active}><a href=\"{$value}\">{$key}</a></li>\n";
    }
    
    echo "</ul>\n";
}
With ruri_string, it would be easy to rewrite it. Smile

Oh, and thank you adamp1 for the tip on using references to pass the data, it never even crossed my mind.
#9

[eluser]pickledegg2[/eluser]
Quote:Problem is what if they don’t have javascript enabled?

True enough!
#10

[eluser]adamp1[/eluser]
The replies are so quick I know for me because I'm sat here having to proof read a 100 page document. Boring as hell so helping problem solve is far more fun Smile




Theme © iAndrew 2016 - Forum software by © MyBB