I use codeigniter. I have footer_view.php, header_view.php and other view php files for pages like aboutus, contactus, etc... for example for about us page, I create model for db actions, than I create a controller to get db variables and send variable to about us view as:
$this->load->view('aboutus/',$data);
Everything is fine until this point, but when I need to get data from db at footer, how will I use in the CodeIgniter Way?
If I create a footer_model, I cannot make view('footer/') because it is actually a part if page, not a page itself :/
You can use $this->load->vars($data);
This will make all data available to all views, footer_view.php, header_view.php and any other views.
$data['your_info'] = $this->user_model->get_info();
$data['your_latest_info'] = $this->user_model->get_latest_info();
$data['your_settings_info'] = $this->user_model->get_settings_info();
$this->load->vars($data);
$this->load->view('your_view');
You view can and will access the data like so:
Your "primary" view
<?php
// your_view.php
// this can access data
$this->load->view('header_view');
<?php foreach ($your_info as $r):?>
<?php echo $r->first_name;?>
<?php echo $r->last_name;?>
<?php endforeach;?>
$this->load->view('footer_view');
?>
Your header_view
<?php
// header_view.php
<?php echo $your_latest_info->site_name;?>
?>
Your footer_view
<?php
// footer_view.php
<?php foreach ($your_setting_info as $setting):?>
<?php echo $setting->pages;?>
<?php endforeach;?>
?>
No need for any template library...
I wrote a blog post about this http://joshhighland.com/blog/2008/11/09/how-i-do-layouts-and-views-in-codeigniter/
basically, you want to load all the data that all your views are going to need in the controller. I use an array of data elements, then pass that to the layout view. My blog post explains more about this, and shows more detail. Here is a sample of my controller.
$this->load->model('search');
$lastest_albums = $this->search->last_n_albumsAdded(10);
$popular_songs = $this->search->popular_n_songs(10);
$featuredAlbums = $this->search->getFeaturedAlbums();
$body_data['featured'] = $featuredAlbums;
$body_data['newest'] = $lastest_albums;
$body_data['popular'] = $popular_songs;
$layout_data['content_body'] = $this->load->view(’home/homePage’, $body_data, true);
$this->load->view('layouts/main', $layout_data);
Related
I am using PHP Codeigniter While I will try to convert pdf for multiple pages, the next page data coming on the first pages. I want to convert pages data page wise. How can I do it?
I have followed this code:
Controller:
<?php
$createPDFFile="myData.pdf";
$data['user_data'] = $returnData;
$htmlContent = $this->load->view('admin/download_user_data', $data, TRUE);
$this->createPDF(FCPATH.'assets/generate_pdf/'.$createPDFFile, $htmlContent);
?>
in View
<?php
if(!empty($user_data)) {
foreach($user_data as $pdata) {
?>
<!-- Here My html Code -->
<?php
}
}
?>
You have to put a break line after page data. I have got the success to added this line below of the last html tag.
<br pagebreak="true"/>
Hope it will be helpful for you.
I have a section in my .html page where I want to run some PHP code, which reads data from a database and 'echoes' the table filled up with that information. I kinda did it object-oriented so I'm trying to keep that along the project.
I have this exact function or method in the Class (Class Servicio):
public static function listaServicios(){
$servicios = array();
$query = "";
$db = Database::getInstance();
$query = "SELECT descripcion, precio FROM servicio";
$resultado = $db->conn()->query($query);
foreach($resultado as $item){
$servicio = new Servicio();
$servicio->setDescripcion($item['descripcion']);
$servicio->setPrecio($item['precio']);
array_push($servicios,$servicio);
}
return $servicios;
}
As you can see, the function just returns an array filled up with as many objects as rows there is in the table. The PHP page would call this method, and then print the table with the selected data inside (there is no problem with this).
The thing is how can I print it in the specific section (div with id='datos') previously mentioned, which simply is:
<section class="principal">
<div id="datos">
</div>
</section>
I kinda of have an idea but I really don't know to implement it. Maybe using a document.ready function in jQuery calling the PHP code? I would really like to use this language even if it is a very tiny function in order to learn.
You can use jquery's load function to load content via a remote file into the div. But you will need to update your listaServicios to return html instead of the array.
jquery:
$(document).ready(function(){
$('#datos').load('phpfile.php');
});
phpfile.php
<?php
$serv = new Servicio();
echo $serv->listaServicios();
?>
Correct answer to your question depends on the interface of your future application you want to reach.
If you want some kind of SPA, then you should make AJAX-requests using JavaScript (jQuery or something else) and create a separate controller to response with data.
But I think in your particular case you can do everything on server side.
First of all you should add new method to your class, name f.e. render:
public static function render(){
$services = static::listaServicios();
include 'path/to/your/template.php';
}
Then inside your template.php write next:
<section class="principal">
<div id="datos">
<?php
foreach($services as $service) {
// echo your $service here as you wish
}
?>
</div>
</section>
So after calling render() PHP will render that part of template.
To access the properties of Servicio is better you construct your table in php:
<?php $servicios = Servicio::listaServicios(); ?>
<section class="principal">
<div id="datos">
<table style="width:100%">
<tr>
<th>Descripcion</th>
<th>Precio</th>
</tr>
<?php foreach ($servicios as $servicio): ?>
<tr>
<td><?= $servicio->getDescripcion() ?></td>
<td><?= $servicio->getPrecio() ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</section>
Hello im using two files 1. layout.html.php(for all the HTML code) 2.layout.php(for all the PHP code)
Im trying to send a variable from the layout.php to layout.html.php file
The variable is equal to a list of all jobs from a database table
See code below
I want to use this list of jobs in a unordered list as links in layout.html.php file
I would appreciate any help i can get with this
CODE FOR LAYOUT.HTML.PHP
<li>Jobs
<ul>
<?php
foreach ($jobs as $job){?>
<li><?= $job['name']?> </li>
<?php }?>
</ul>
CODE FOR LAYOUT.PHP
<?php
require '../loadTemplate.php';
$stmt = $pdo->prepare('SELECT * FROM job');
$stmt->execute();
$jobs= $stmt->fetchAll();
$templateVars = ['stmt' => $jobs];
$output = loadTemplate('../templates/layout.html.php',['stmt' => $jobs]);
require '../templates/layout.html.php';
?>
If in a file A you got the variable X:
<?php
$frase = "abcdef";
?>
Then if you want to use the same variable in another file you can use:
<?php
include('A.php');
echo $frase;
?>
There is other ways to do that. You can also use the Session, cookies, or just pass in the url.
In my case, I like to do in that way:
I have a page home.php. In that page I represents a consult. So, in home.php I got:
<?php
include 'php/db.php'
$result = BuscaDados();
while($row = $result->fetch_assoc()){
// here you get all lines
}
?>
In db.php I got a implementation of my functions that return the consult. Try it out.
i had save data in mysql data base and i had create template html tags in one column. now i want to display template data in php, but its not working.
MYSQL DATABASE IMAGE CLICK HEAR TO SHOW DATABASE
Controller
class Project extends CI_Controller{
function template(){
$query = $this-db->query("select * from templete where id='11' ");
$result = $query->result_array();
$data['result'] = $result[0];
$this->load->view('template_view',$data);
}
}
View
<html>
<head></head>
<body>
<?php
extract($result);
echo $template_data;
?>
</body>
</html>
Output :
{$ticker}
{$address1}
{$address2}
{$city}
{$phone}
{$website}
I want output
HSPG.OL
Sparebankgården
Bjørkelangen
http://www.hsbank.no
template data dyanamic
its gone be
<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p><p>{$phone}</p><p>{$website}</p>
or
<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p>
any one help, i am post this question 2nd time with full coding.
Although Codeigniter as a framework doesn't stop you or restrict you to stay in MVC boundaries but it is really appreciated and a good practice that one should do so.
BEST PRACTICE
Create a model and put your query in a function in that model. Call
that function in your controller, get your data in an array/object and
then send it to the view that's how MVC is supposed to be.
In your case, you are using php extract() function to get data out of the $result array which it has done perfectly but what you are printing is not the correct index.
Read this for php extract info
so instead of printing $template_data use the following ;
<html>
<head></head>
<body>
<?php
extract($result);
echo $ticker.'<br>';
echo $address1.'<br>';
echo $city.'<br>'; // and so on
?>
</body>
</html>
Lot of things wrong with this code. You shouldn't be getting template data from the database, that should be in the view. You should probably delete the template_data column from your table.
class Project extends CI_Controller{
function template(){
$query = $this-db->query("select * from templete where id='11' ");
$data['result'] = $query->result_array();
$this->load->view('template_view',$data);
}
}
View:
<html>
<head></head>
<body>
<?php echo $result['ticker']; ?>
<?php echo $result['address1'] . ' ' . $result['address2']; ?>
<?php echo $result['city']; ?>
<?php echo $result['phone']; ?>
<?php echo $result['website']; ?>
?>
</body>
</html>
I'm new to zend and i'm struggling with some items.
I tried using Helpers, but then realized it doesn't do what I wanted it to do...
either i'm using $this->placeHolder... wrong or it doesn't do what I want it to do
I want to do the following:
I want to create a custom class that basically has 2 methods addScriptContent, and getScriptContent...
addScriptContent adds what ever is passed into it to a string that is global for the page.
getScriptContent will just output whatever data that has been added using addScriptContent
i.e.
this->addScriptContent('var x="foo";')
....some html
this->addScriptContent('var y="feee";')
....some html
echo this->getScriptContent() //writes out var x="foo"; var y="fee";
The placeholder helper does exactly what you are after:
<?php $this->placeholder('scriptContent')->set('var x="foo";') ?>
....some html
<?php $this->placeholder('scriptContent')->append('var y="feee";') ?>
....some html
<?php echo $this->placeholder('scriptContent')?>
the key methods on the helper are set, append and prepend; which overwrite, add to, and add to the start of the content respectively.
If you really want to write your own helper for this, this wouldn't be too difficult. Take a look at the headTitle or headScript helpers for an example, but would be something along the lines of:
class My_View_Helper_ScriptContent extends Zend_View_Helper_Placeholder_Container_Standalone
{
public function scriptContent($script)
{
$this->append($script);
}
}
usage would then be:
<?php $this->scriptContent('var x="foo";') ?
....some html
<?php $this->scriptContent('var y="feeee";') ?>
....some html
<?php echo $this->scriptContent() ?>