Welcome Guest, Not a member yet? Register   Sign In
hooking data into an included php file.
#1

[eluser]deco10[/eluser]
I have a header that consists of mostly static content with some dynamic content.

In plain ol' php I would just have my header_view.php file and included it after the body tag in my index.php.

That works assuming I'm putting all the php code at the top of each php file which is what I would do prior to using CI.

What I've gathered so far is to use a hook, which I seem to have working, but its not doing what I want.

ideally, when the include "header_view.php"; statement is executed it would first run some php code to prepare the page for display.

Now that I revisit what I have done, its closer to working then i thought, I just don't know if my methodology is best practice.

The problem I have now is that I can't seem to pass the data into "header_view.php" because its not displayed with $this->load->view("header_view.php");
and I don't know how else to do it.

So I either need to know how to do it a completely different and proper way or I need to know how to get data into the header_view.php script.


Regards,
#2

[eluser]TheFuzzy0ne[/eluser]
Your best bet would be to post some code, then I'm sure that you'll get plenty of replies.
#3

[eluser]depthcharge[/eluser]
Hi,

Are you including the header with a regular include statement? , you can call load-view many times.

can you not use:
Code:
$data['title'] = 'My Page';
$data['keywords'] = 'My List of Key Words';
.....
.....

$this->load->vars($data);
$this->load->view('header_view.php');
$this->load->view('body_view.php');
$this->load->view('sidebar_view.php');
$this->load->view('footer_view.php');

For this i prefer to extend the controller class instead of using a hook, using a My_Controller.php.

I would recommend you use a template/view library too or build yourself.
#4

[eluser]deco10[/eluser]
I hope this covers it.


home.php
Code:
<div id="wrapper">
&lt;? include 'header_view.php'?&gt;
  
  <div id="content">
  <div id="siteNav">
    &lt;? include 'nav_view.php' ?&gt;
  </div>
  <div id="main">
   <h1>Welcome</h1>
     text text text
  </div>
</div>

header_view.php
Code:
<div id="header">
    <div id="dlr">text text</div>
  
        <div id="contact">
        Lorem Ipsum Blah blah blah
        </div>
  </div>
#5

[eluser]depthcharge[/eluser]
What data is it you are trying to show in header_view.php ?

You have no variable placeholders or php code there.

header_view.php
Code:
<div id="header">
    <div id="dlr">text text</div>
  
        <div id="contact">
        Lorem Ipsum Blah blah blah
        </div>
  </div>
#6

[eluser]deco10[/eluser]
It's some data that has been processed, and I'm still building, so there could be more. One thing I will need, and there may be a more effective way of doing this, but I need to print a class for the current page (its not shown in my code there for the sake of brevity, but its a tabbed nav bar)
#7

[eluser]depthcharge[/eluser]
Deco,

Ok, you have posted your code for your 2 views, the one that you are having problems passing data to has no variables in it?

You have probably fixed it by now, but if you want help, then you are going to have to post more code, maybe I mis-interpreted the problem you are having.
#8

[eluser]deco10[/eluser]
I misnamed the files in my post

As an example I'm trying to print out of an array in the header_view file

&lt;?= $lines['first'] ?&gt;



home_view.php
Code:
<div id="wrapper">
&lt;? include 'header_view.php'?&gt;
  
  <div id="content">
  <div id="siteNav">
  </div>
  <div id="main">
   <h1>Welcome</h1>
     text text text
  </div>
</div>


home.php controller
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('html');
    }
    

    function index()
    {
        $data['title'] = "Site Title";
         $data['lines']= array();
        $data['lines']['first'] = "first el";
        $data['lines']['second'] = "second el";
        $this->load->view('home_view', $data);
    }
}


header_view.php
Code:
<div id="header">
    <div id="dlr">&lt;?=anchor('../','<img src="/dev/site3/images/topbox.gif" />') ?&gt;</div>
  
        <div id="contact">
          <ul>
          <li>Phone Toll Free:</li>
          &lt;?= $lines['first'] ?&gt;
          </ul>
        </div>
  </div>
#9

[eluser]depthcharge[/eluser]
Hi,

Your code as supplied above did pass the variable to the header view, however it is not showing the entire array, For example i have added a loop to loop through and print each item of the array shown below.

Code:
<div id="header">
<div id="dlr">&lt;?=anchor('../','<img src="/dev/site3/images/topbox.gif" />') ?&gt;</div>

<div id="contact">
<ul>
    <li>Phone Toll Free:</li>
    <ul>
    &lt;?php foreach ($lines as $line) :?&gt;
        <li>&lt;?php echo $line ?&gt;</li>
    &lt;?php endforeach;?&gt;
    </ul>
</ul>
</div>
</div>
#10

[eluser]deco10[/eluser]
I see that works but I have no reason to print the entire array. How can I access just a particular element of it?




Theme © iAndrew 2016 - Forum software by © MyBB