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.
Related
I had two script, one in .php and one in .tpl
I need to pass the variable in php to the tpl.
I tried this one, but nothinng works (but somehow
it works for one or two days, and after that,
it showed blank,
if i create another php script just to echo the variable, it works.
PHP Code:
<?php
$usdidr2 = "12610.198648";
$usdidr2 = number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>
SMARTY Code:
<li>
<a href="example.php"><strong>
{php}
session_start();
$regValue = $_SESSION['regUSDIDR1'];
$regValue2 = number_format(45.99*$regValue,2,',','.');
echo "Rp. ".$regValue."";
print_r($regValue);
{/php}
</strong></a>
</li>
Here is the syntax to send data from php to tpl
$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);
Update:
$smarty->assign('rate',$usdidr2);// you just need to write rate without $
You can access it in smarty like {$rate} if it is string
You can access it in smarty like {$rate|print_r} if it is array
You can use this syntax:
$res = "Hello World!";
$this->context->smarty->assign('result', $res);
And passing to .tpl file like this:
{$result}
Hope this helping you.
Example of what i am trying to do:
<?php
$html_element = '<form><label>country</label><select>'.include_once("country_list.php").'</select></form>';
?>
I have tried the above and the following:
<?php
$html_element = '<form><label>country</label><select><?php include_once("country_list.php"); ?></select></form>';
?>
Of course the "$html_element" is then echoed in some div later. So how can i include the "country_list.php" in this php string variable so that it will pull correctly?
If you want to include the data, no matter which file type it is, do like this:
<?php
$html_element = '<form><label>country</label><select>'.file_get_contents("country_list.php").'</select></form>';
?>
I'm trying to load an php file through ajax using load(). Here's my code:
var loadUrl = "myfile.php";
$("#lst-results").load(loadUrl);
So, when I do this, it works:
<?php
$test = 'Hi :3';
?>
<h3>Hi</h3>
<h3><?php echo $test ?></h3>
But I need to include a file that contain a lot of includes and functions using a lot of different classes. I've tryed to include this file and use it as a function like this, but does not work and Nothing appears int the load() results. This function return an array.
<?php
include 'path/myfile.php';
$result = myFunction();
?>
I can't figure out how to solve it. :(
You need to "echo" the $result.
<?php
include 'path/myfile.php';
$result = myFunction();
echo $result;
header.php
<?php
$conn = mysql_connect('localhost', '-', '-');
#mysql_select_db('accmaker', $conn) or die("Unable to select database");
?>
<html>
<head>
<title>Mysite.com - <?php isset($pageTitle) ? $pageTitle : 'Home'; ?></title>
</head>
<body>
profile.php
require 'header.php';
$q = mysql_query("SELECT * FROM users WHERE username = '$username'");
$r = mysql_fetch_assoc($q);
$pageTitle = "Profile of $r[username]";
I think you understand what i want
I cant include header.php after the query, because i wont be connected to mysql
waht do you suggest other than having the connection snippet on every page
What do I suggest? A MVC (Model-View-Controller) Framework like Kohana. If you don't want to go that route, break your connection off into its own file:
<?php
# connect
require_once("connection.php");
# load page data array
require_once("page-data.php");
?>
...
<title><?php print $page["title"]; ?></title>
Note here how I have a $page array of data. This will be helpful when debugging later rather than having several independent variables. With an array of page data, I can quickly see all of the information laid out for any given page:
print "<pre>";
print_r($page);
print "</pre>";
Determining your title should be done within page-data.php, rather than on your page:
$config["site_name"] = "Bob's Shoe Mart";
$config["admin_email"] = "bob#shoemart.com";
/* query to get $row['title'] */
$page["title"] = (!empty($row["title"])) ? $row["title"] : $config["site_name"] ;
Not sure of a "best" solution, but we currently include multiple files. We have our "utilities.php" file that connects to the database and provides some nice functions. We then set our page titles and then we include "top.php" which is the layout portion. It doesn't have anything except HTML with a little bit of PHP for display purposes. Looks like this:
include "utilities.php";
$pageTitle = "Welcome";
include "top.php";
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);