CodeIgniter Forums
Dynamic Content in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Dynamic Content in CI (/thread-25348.html)

Pages: 1 2


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]seismic[/eluser]
Can somebody give me a link to guide how to put dynamic content in the content-div (in view file) I mean how to put the "variable content" (article01, article02, ... ) properly? How is it done in CI way?
I've check out the guide here and i couldn't find any directives to implement this common thing.
Should I handle the content files in the view folder and load them by passing $variable (taken from the link ?page=article01) through (my default) controller into the index.php (main html file) in the view?
So then choosing it normally by switch/if and php include?
What about the Model part?

How do you make it?


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]Phil Sturgeon[/eluser]
Take a look here. Should solve your problems.


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]seismic[/eluser]
Blah... this gives nothing, even some directions!
You're really unhelpful giving such reference.


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]theprodigy[/eluser]
Not exactly sure what you are asking.

There is a section in

http://ellislab.com/codeigniter/user-guide/general/views.html

called "Adding Dynamic Data to the View"

Will that help?


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]stuffradio[/eluser]
Pff, this is basic and in the User guide Smile


Code:
//mycontroller.php

class Mycontroller extends Controller
{

    function Mycontroller()
    {
      parent::Controller();
    }

    function index()
    {
      $data['my_var'] = "some value";

      $this->load->view('myview', $data);
    }

}

Code:
//myview.php

echo "This is the value of " . $my_var . "!";



Dynamic Content in CI - El Forum - 12-09-2009

[eluser]seismic[/eluser]
I already read carefuly the user guide Smile Anyway Theprodigy thnx.
There is no mention how to input the "variable text" or the "variable file" in the div of template.
Stuffradio what about if "some value" will be a big article (one of many) taken from the file(html essence with h1,h2,p etc symbols) and it depends from the parameter taken from the link? (i know how to get the variable from the link already).
For example i have 2 articles here.

Default Controller:
Code:
<?php
class Test extends Controller {  
  function index($var) {
    parent::Controller();    
    $this->load->helper('url');
    $this->load->view('template/view_index', $var);
  }
}
?>


view/view_index.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;title&gt;metrohacker&lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /&gt;
&lt;meta name="description" content="Your website description goes here" /&gt;
&lt;meta name="keywords" content="your,keywords,goes,here" /&gt;
&lt;/head&gt;

&lt;body&gt;
<div id="outer">

<div id="left">
Main Menu
<ul>
&lt;!-- How do you create the links here with files: article01, article02, ... ?
i would like to make link like: www.example.com/article01 --&gt;
<li><a href="php echo base_url(); ?&gt;article01">ArticleHardware</a></li>
<li><a href="php echo base_url(); ?&gt;article02">ArticleSoftware</a></li>
</ul>
</div>


<div id="right">
&lt;!-- How do you put the content of various files depends of parameter here? --&gt;
&lt;?PHP
$this->load->view( $var ) ?&gt;
</div>

</div>
&lt;/body&gt;
&lt;/html&gt;

Should I store article/files in the view folder (in eg. view/articles/article01)?
What extension should i use? What extension do you prefer?
What about Model doing here? Should i use the Model class somewhere here?


Dynamic Content in CI - El Forum - 12-09-2009

[eluser]stuffradio[/eluser]
KISS (Keep it simple stupid)

Models -> Handles all database transactions
Views -> All the stuff that gets outputted to your brwoser
Controllers -> Controls what happens, logic can happen in here too.

You can store content for certain pages in a database.

Say you have page A, in your browser path you can go

http://mysite.com/index.php/page/index/A

You take the value from the 3 segment, that's the value A.

You go to the model and fetch the data for page A. You store the values of page A in variables and load them onto the view.

Try playing around with that concept, I have to go to school now... I'm sure others can help if you have other questions while I am gone.


Dynamic Content in CI - El Forum - 12-11-2009

[eluser]seismic[/eluser]
stuffradio THNX!

This is how i make it:
Controller:
Code:
&lt;?php
class Test extends Controller {
  function Test() {
    parent::__construct();    
  }      

  function index() {
    parent::Controller();
  }

  function funkcja($arg) {  //$arg is parameter taken from url (for eg. article number)
    $this->load->model('model_test');
    $tablica['zmienna'] = $this->model_test->getDane($arg);
    $this->load->view('szablon_test/widok_index', $tablica);
  }
}
?&gt;

Model:
Code:
&lt;?php
class Model_test extends Model {

  function Model_test() {
    parent::Model();
    $this->load->database();
  }

  function getDane($arg){
  $query = $this->db->query('SELECT content FROM site_pages WHERE id_page='.$arg.';');
  $row = $query->row(); //takes only one result row
  $wartosc = $row->content;
  return $wartosc;
  }
}

View:
Code:
&lt;HTML&gt;&lt;head>&lt;/head&gt;

&lt;body&gt;
<div id="outer">

<div id="left">
<div class="navheader">Main Menu</div>
    <ul>
    <li><a href="&lt;?php echo base_url();?&gt;test/funkcja/7">Article about Software</a></li>
    <li><a href="&lt;?php echo base_url();?&gt;test/funkcja/8">Article about Hardware</a></li>
    </ul>
</div>
</div>

<div id="right">
&lt;?PHP
echo $zmienna;
?&gt;
</div>

</div>

&lt;/body&gt;
&lt;/html&gt;

Maybe someone has idea to make better solution?


Dynamic Content in CI - El Forum - 12-11-2009

[eluser]Phil Sturgeon[/eluser]
Ok fine, I'll throw in.

Now you have your articles/pages/whatever loading dynamically using URI parameters, you now need to link to these dynamically. The best way to do this, is to retrieve a list from the database, which should hold id and title already.

model:

Code:
// add me
  function get_all($arg)
  {
    $query = $this->db->select('id, title')->get('site_pages');
    return $query->result();
  }

view:

Code:
&lt;HTML&gt;&lt;head>&lt;/head&gt;

&lt;body&gt;
<div id="outer">

<div id="left">
<div class="navheader">Main Menu</div>
    <ul>
    &lt;?php foreach( $pages as $page): ?&gt;
    <li>&lt;?php echo anchor('test/funkcja/'.$page->id, $page->title);?&gt;</li>
    &lt;?php endforeach; ?&gt;
    </ul>
</div>
</div>

<div id="right">
&lt;?PHP
echo $zmienna;
?&gt;
</div>

</div>

&lt;/body&gt;
&lt;/html&gt;

All of these basic concepts are shown here and in a thousand other places on the internet.


Dynamic Content in CI - El Forum - 12-11-2009

[eluser]stuffradio[/eluser]
Some things I notice in your model is this

Code:
$this->load->database();

You don't need that because in your autoload I assume you have database in the autoload file.
This is what I have

Code:
$autoload['libraries'] = array('database', 'session', 'security');

Security is a custom library I made.

Also, in your controller... aren't you mixing PHP 4 with 5?

And another thing, you don't put parent::Controller() in your index function, that does the same thing as the __construct function in the Test function.