php template for different pages with different css and mysql - php

Hi I'm trying to figure out how to do a PHP template system with a class, i dont like header.php footer.php or smarty or whatever blabla, just want the template.html, the class.php and page1.php, page2.php etc... with my own php code, and I've found a lot of website of people teaching how to do this but i still have lots of questions.
1) i want to add EXTRA css to some pages
2) SOME pages has php code like mysql queries and stuff like that
3) the CONTENT which would be a variable where ever i want in the template is not only words, instead is a large amount of divs and stuff, also in some pages the CONTENT variable has queries inside, like fillin a (e.g)dropdown menu.
Hope somebody can guide me in this, i actually have my tempalte for explame, (the tags are just random)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>CompanyName | ##TITLE##</title>
<link href="layout.css" rel="stylesheet" type="text/css" />
<link href="styles.css" rel="stylesheet" type="text/css" />
##EXTRA_CSS##
<link rel="shortcut icon" href="/favicon.ico" />
<!--[if IE 6]>
<script src="DD_belatedPNG_0.0.8a-min.js"></script>
<script>
DD_belatedPNG.fix('img, div');
</script>
<![endif]-->
##EXTA_JS##
</head>
<body>
<div id="main">
<div id="container_black">
<div id="container_white">
<div id="container_header">
<div id="logo_top"></div>
<div id="lineas_verticales_top">
<div class="volver_portada">Volver a portada</div>
<div class="english_spanish"><u>Español</u> | English</div>
</div>
<div id="nav_bar_black"><div id="nav_bar_red"><div id="nav_bar_yel">
<ul class="menuholder">
<li class="menu_principal">Principal</li>
<li class="menu_empresa">Empresa</li>
<li class="menu_productos">Productos</li>
<li class="menu_clientes">Clientes</li>
<li class="menu_recetas">Recetas</li>
<li class="menu_contacto">Contacto</li>
</ul>
</div></div></div>
<div id="topbg_degr"></div>
</div>
<div id="container_left">
<div id="conmargen_left_top"></div>
<div id="container_conmargen_left_middle">
##CONTENT##
</div>
<div id="conmargen_left_bottom"></div>
<!--[IF INDEX]
<div id="fono"></div>
<div id="dir"></div>
-->
</div>
<!--[IF INDEX]
<div id="nav"></div>
-->
<div id="container_right">
<div id="conmargen_right_top"></div>
<div id="container_conmargen_right_middle">
<!--[IF PAGE OR PAGE OR PAGE]
[ELSE IF]
[ELSE IF]
-->
</div>
<div id="conmargen_right_bottom"></div>
</div>
<!--[IF INDEX]
<div id="frame_facebook">
<span>CompanyName</span> en Facebook
<div class="breakL"></div>
<fb:like href="#" layout="button_count" show_faces="true" width="100" font="tahoma"></fb:like>
</div>
-->
<br/>
</div> <!-- cierre del container white -->
</div> <!-- cierre del container black -->
<div id="footer">
<div class="footer_comment">
CompanyName Todos los derechos reservados 2011
</div>
</div>
</div> <!-- cierre del main -->
<br/>
</body>
</html>
for instance contact.php, the content would be a form, and on the top of the page i have this huge php code, where i validate and all.
I'd really appreciate if somebody would put me in the right path to do this. thank you in advance.

There are probably a hundred different ways you could approach this, each with their own advantages and disadvantages - using databases and/or ajax calls and/or headers and footers as you already said. You really have to work out which way works best for your particular project or style of coding, or both.
However - if you really just want 'template.html', but have some pages with PHP, javascript, MySQL or whatever else in them - then I would suggest.
Create template.html with placeholders such as {PAGE_TITLE}, {MAIN_CONTENT} or whatever you need.
Create page1.php/page2.php etc and do any server side work, generate variables to match your placeholders. It might be handy to store them as an array, ie:
PAGE_VARS array(
['TITLE'] => My Page
['CONTENT'] => This is my page content
)
At the end of your 'page' script, load the entire contents of template.html into a string
$template = file_get_contents('template.html')
Then, build up your template with the replaced variables using either a basic loop:
foreach ($PAGE_VARS as $KEY=>$VALUE) {
$template = str_replace("{".$KEY."}",$VALUE,$template)
}
(Feel free to get clever and probably more efficient with some regular expressions above, this is just a quick example.)
Output your template.
echo $template;

You can create a quick class (class.php) like this that will be your 'template engine' :
class Template {
var $contents;
function load($file) {
if ($fp = fopen($file, "r")) {
$this->contents = fread($fp, filesize($file));
fclose($fp);
}
}
function replace($str,$var) {
$this->contents = str_replace("<".$str.">",$var,$this->contents);
}
function show() {
$search = array(
'/\t/', //Remove Tabs
'/<!--[^\[-]+?-->/', //Remove Comments
'/\n\n/' //Remove empty lines
);
$replace = array(
'',
'',
''
);
$this->contents = preg_replace($search, $replace, $this->contents);
echo $this->contents;
}
}
In your template.html file, add special tags where you want to put your content, like this :
<html><head></head>
<body>
<div id="id1"><page_title></div>
</body>
</html>
...
Then create a function to write inside the tags (one per zone) :
function writetitle($s) {
$GLOBALS['writes']++;
$GLOBALS['page_title'] .= $s;
return;
}
finally, in your page.php, call the class, write your content, and generate the page :
require_once('class.php');
//Load Class
$template = new Template;
$template->load("template.html");
//Some query :
$query = mysql_query('SELECT...');
$res = mysql_num_rows($query);
//write your content :
writetitle('my title, '.$res.'');
//Generate the page :
$template->show();
Doing this you can create as many zones as you want. writetitle act like echo, so you can make queries and everything you want.
I hope it helps.

Insetad of having PHP parsing your template files I would suggest writing the templates using PHP and not your own pseudo code.
It's perfectly valid to write
...
<body>
<?php if($fooBar == "hahahaha"):?>
The cool foobar link
<?php endif;?>
</body>
...
I would then create a layout file with all common html etc. Then using small template snippets to be inserted into the layout where the main content goes.
By having actual PHP code in your templates the PHP enginge can handle them and you don't have to write your own logic to handle loops, if statements etc.
Take a look at Zend Framework and you'll get a good idea of how you can write your template engine.

Related

How to set title with data from included PHP file

I have a website with a template page that is something like this
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<div id = "header"> ... </div>
<?php include 'content.php'; ?>
<div id = "footer"> ... </div>
</body>
</html>
And then content.php would look something like this
<?php
$title = "xx";
#other php code here
?>
<p> more content </p>
My question is whether there is some way to set this up so that I am able to set the title from the file included in the middle of the page (without using javascript). I know that most people suggest including it at the top but if I were to do that the html would be at the top instead of between the header and the footer. I've wracked my brains for a while and I haven't really figured out a good way to do this (and there are a variety of possible files to be included; content.php is just an example, so I really do need some way to do this dynamically). I want to avoid putting too much code outside of the template. Any ideas?
Use output buffering:
<?php
ob_start();
include 'content.php';
$content = ob_get_clean();
?>
<html>
<head>
<title><?=htmlspecialchars($title)?></title>
</head>
<body>
<div id = "header"> ... </div>
<?=$content?>
<div id = "footer"> ... </div>
</body>
</html>
I'm not sure if you're using a templating engine or not (something like Laravel's Blade templates allow for this, I believe), but assuming you are using straight PHP, I have found the most efficient way to do this is to approach from the opposite direction and include the template file in each of my content files.
For example, I may have template.php, which has multiple functions, like this:
<?php
function createHeader($title, $keywords) {
//echo or <<<EOD your header with the set variables
}
function createFooter(...) { ... } //etc
And then, in my 'child' files, I would do:
<?php include('template.php'); ?>
<?php createHeader("My Website Front Page!", "fun, good times, joy"); ?>
<h1>My page content</h1>
<p>Content goes here</p>
<?php createFooter(...); ?>
This is a different structure from what you were attempting, though, and may not retain your intended structure.

I cannot get a COOKIE to update it's value (which is an integer)

I have been working on this for so long now, I feel this is my last option, I have a slideshow on a html web page and they can essentially flick through all the way through to the end by scrolling. Essentially I want users to be able to go back to their dashboard and when they click to go back into the slideshow, to re-load the page they were on...
At the top of the page I have this:
<?php
if (!isset($_COOKIE['PageNo'])){
setcookie("PageNo", 1, time() + (86400 * 30), "/"); // 86400 = 1 day, so set the cookie for a month long period
}
?>
I am essentially saying set the cookie at 1 to begin with (the first page in the slideshow = 1, then above the next section I have the below:
<?php
if($_COOKIE['PageNo'] >= 2)
{
?>
<script>
window.location.replace("<?php echo "istudy_university.php#slide=".$_COOKIE['PageNo']; ?> ");
</script>
<?php
}
else
{
?>
<script>
window.location.replace("istudy_university.php#slide=1");
</script>
<?php
}
?>
Above each slide I have the below and just changing the slide=number:
<?php $_COOKIE['PageNo'] = 3; ?>
So I'm saying, if the cookie is more than or equal to 2, then go to the page no'x' else go to page 1. However all it keeps doing is bringing me back constantly to Page 1. Please help!! Am I setting the cookie wrong?
UPDATE: After sliding through some of the slides, the cookie should have changed to 5, however it's still 1?
UPDATED code showing the HTML for page:
<?php
session_start();
require "includes/dbh.inc.php";
?>
<?php
echo $_COOKIE['PageNo'];
//$_COOKIE['PageNo'] = 5; //Commented out, for testing
if (!isset($_COOKIE['PageNo'])){
setcookie("PageNo", 1, time() + (86400 * 30), "/"); // 86400 = 1 day, so set the cookie for a month long period
}
?>
<!doctype html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>iStudy University | Stress & Anxiety</title>
<link rel="stylesheet" type='text/css' media='all' href="webslides.css" rel="stylesheet">
<body>
<script src="static/js/webslides.js"></script>
<!-- BEGINNING OF SLIDES -->
<?php
if($_COOKIE['PageNo'] >= 2)
{
?>
<script>
window.location.replace("<?php echo "istudy_university.php#slide=".$_COOKIE['PageNo']; ?> ");
</script>
<?php
}
else
{
?>
<script>
window.location.replace("istudy_university.php#slide=1");
</script>
<?php
}
?>
<main role="main">
<article id="webslides">
<!-- SLIDE 1 -->
<section class="bg-apple aligncenter">
<span class="background dark" style="background-image: url('istudy_slides_images/abstract.jpg')"/></span>
<div class="wrap" id="slide=1">
<h1 class="text-landing">Stress & Anxiety</h1>
<br>
<br>
<br>
<hr id="hor-rule">
<h1 class="slides-logo">iStudy University <i class="fas fa-graduation-cap"></i></h1>
<h2 class="slogan">Designed by Students <br><span class="iv">IV</span> <br>Students</h2><br><br>
</div>
</section>
<!-- SLIDE 2 -->
<?php $_COOKIE['PageNo'] = 2; ?>
<section class="aligncenter">
<span class="background light" style="background-image: url('istudy_slides_images/mountain.jpg')"/></span>
<div class="wrap" id="slide=2">
<blockquote class="quote">
<p class>"No one can create negativity or stress within you. Only you can do that by virtue of how you process your world"</p>
<p><cite>Wayne Dyer</cite></p>
</blockquote>
</div>
</section>
<!-- SLIDE 3 -->
<?php $_COOKIE['PageNo'] = 3; ?>
<section class="bg-slide3">
<div class="wrap size-80" id="slide=3">
<h3 class="title stessAnx"><strong> Stress & Anxiety</strong></h3><br>
<p>Stress and anxiety are common experiences of students in higher education.<br>This module will introduce you to evidence based techniques for managing stress and anxiety based upon cognitive behavioural therapy (CBT).</p>
</section>
</div>
Include jQuery library in both dashboard and slides page.
Include Scrollify library in the slides page
In the dashboard page, add an id to the slide page navigation link, like:
<a id="home" href="#">Slides</a>
Modify the sections in your slides page as follows:
Add a unique ID and a common class name for all the sections.
Example:
<section class="slides aligncenter" id="b">
<span class="background light" style="background-image: url('istudy_slides_images/mountain.jpg')" /></span>
<div class="wrap" id="slide=2">
<blockquote class="quote">
<p class>"No one can create negativity or stress within you. Only you can do that by virtue of how you process your world"</p>
<p><cite>Wayne Dyer</cite></p>
</blockquote>
</div>
</section>
<!-- SLIDE 3 -->
<section class="slides bg-slide3" id="c">
<div class="wrap size-80" id="slide=3">
<h3 class="title stessAnx"><strong> Stress & Anxiety</strong></h3><br>
<p>Stress and anxiety are common experiences of students in higher education.<br>This module will introduce you to evidence based techniques for managing stress and anxiety based upon cognitive behavioural therapy (CBT).</p>
</section>
** The id for each section is given as 'b', 'c' etc.
** both section contains a common class name - 'slides'.
In the slides page, add the following JavaScript code in the footer.
$.scrollify({
section: ".slides", //Rename the class name with the common class name that you gave for the sections
after: function() {
localStorage.setItem('currentPage', $.scrollify.current()[0].id)
}
});
In the dashboard page, add the below JavaScript code to the footer at the end:
<script>
if(localStorage.getItem('currentPage') != ''){
var newUrl = 'scroll.html#'+localStorage.getItem('currentPage');
$("#home").attr("href", newUrl);
}
</script>

Shorten text to add read more link

I have the following HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TestPost</title>
<script src="jquery-2.1.4.min.js"></script>
<script src="more.js"></script>
</head>
<body>
<div class="post1">
This is post one
</div>
<div class="post1">
Another post number one
</div>
<div class="post2">
And this is post two
</div>
<div class="post3">
Last but not least, post three
</div>
</body>
</html>
What i'm looking for is the folowing:
When the text in a div is longer that, lets say, 5 chars, it should cut it off and add ...read more(including a link to a page).
I tried some PHP and some JQuery, but to be honest, I'm not sure anymore what to use.
If I could get the answer, that would be fantastic, but a push in the right direction would be very appreciated as well :)
Edit: The second post1 was added for testing purposes for anyone who's wondering.
Use attribute class instead of id. replace id='post' with class='post'
Use this code into your more.js
var mess_content = $('.post');
mess_content.each(function(){
if ($(this).text().length > 120) {
var obrtext = $(this).text().substr(0,120) ;
$(this).html(obrtext+"<a href='#'>read more</a>") ;
}
});
To do this with PHP, when you ouput your text, run it through a shortening function like this:
function shorten($output, $limit = 5) {
$output = htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
if (strlen($output) > $limit) {
$output = substr($output, 0, $limit) . ' ... read more';
}
echo $output;
}
You can use it then like this:
<div id="post1">
<?php shorten('This is post one'); ?>
</div>

How do I iterate through form-fields on a page and send them to a database?

Here is the code for Checkout.php:
<?php
//Include SimpleSanitize.
include 'simplesanitize.php';
// First attempt at PHP Sessions. Wish me luck.
// Start the session.
session_start();
$product = $_REQUEST["product"];
$qty = $_REQUEST["qty"];
$price = $_REQUEST["price"];
//var_export($product);
//var_export($qty);
if( !isset($_SESSION['last_access']) || (time() - $_SESSION['last_access']) > 71200 )
$_SESSION['last_access'] = time();
// If post is not null, then add selected data to corresponding sessionid in database.
if($_SESSION['last_access'] != null && $qty != null)
{
// Sanitize information.
$info = new SimpleSanitize('request', 'both');
//$product = $info->get('product');
//$quantity = $info->get('qty');
//$price = $info->get('price');
$connection =
mysql_connect("..com","","");
if($connection)
{
mysql_select_db("__dbase", $connection);
foreach($arr as $key => $value){ echo "$key: $value ";}
print_r($val);
$query = "UPDATE sessions SET qty='".$val."' WHERE sessionid='".session_id()."' AND product='".$item."'";
mysql_query($query)
or die(mysql_error());
// Assume items added successfully.
$ItemAddedMessage = "Item added to cart.";
// Close connection to database.
mysql_close($connection);
}
else
$ItemAddedMessage = "Quantities updates";
}
// Open the DB connection and select the DB - creates the function getCreativePagerLyte()
include('configurations.php');
// Gets the data
$id=isset($_POST['id']) ? $_POST['id'] : '';
$search=isset($_POST['search']) ? $_POST['search'] : '';
$multiple_search=isset($_POST['multiple_search']) ? $_POST['multiple_search'] : array();
$items_per_page=isset($_POST['items_per_page']) ? $_POST['items_per_page'] : '';
$sort=isset($_POST['sort']) ? $_POST['sort'] : '';
$page=isset($_POST['page']) ? $_POST['page'] : 1;
$extra_cols=isset($_POST['extra_cols']) ? $_POST['extra_cols'] : array();
// Uses the creativeTable to build the table
include('creativeTable.php');
$ct=new CreativeTable();
// Data Gathering
$params['sql_query'] = 'SELECT product, qty, price FROM sessions WHERE sessionid="'.session_id().'"'; // IMPORTANT: you must specify the fields and not use *
$params['search'] = $search;
$params['multiple_search'] = $multiple_search;
$params['items_per_page'] = $items_per_page;
$params['sort'] = $sort;
$params['page'] = $page;
// Layout Configurations (Most used - the commented lines are the default values)
$params['header'] = 'Product, Quantity, User Action, Price'; // If you need to use the comma use , instead of ,
$params['width'] = ',,,';
//$params['search_init'] = true;
//$params['search_html'] = '<span id="#ID#_search_value">Search...</span><a id="#ID#_advanced_search" href="javascript: ctShowAdvancedSearch(\'#ID#\');" title="Advanced Search"><img src="images/advanced_search.png" /></a><div id="#ID#_loader"></div>';
//$params['multiple_search_init'] = 'hide';
$params['items_per_page_init'] = '10,20,50,100'; // default: '10*$i';
//$params['items_per_page_all'] = '#TOTAL_ITEMS#';
//$params['sort_init'] = true;
//$params['sort_order'] = 'adt';
//$params['ajax_url'] = $_SERVER['PHP_SELF'];
$ct->table($params);
foreach($ct->data as $key => $value){
$ct->data[$key][0]='<input type="text" value="'.$ct->data[$key][0].'" name="product" />';
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="qty" />';
$ct->data[$key][2]='<input type="submit" value="Update Item">';
$ct->data[$key][3]='<p name="price">'.$ct->data[$key][3].'</p>';
}
// If its an ajax call
if($_POST['ajax_option']!=''){
if(strpos($_POST['ajax_option'],'items_per_page')!==false)
$out_ajax['items_per_page']=utf8_encode($ct->draw_items_per_page());
if(strpos($_POST['ajax_option'],'body')!==false)
$out_ajax['body']=utf8_encode($ct->draw_body());
if(strpos($_POST['ajax_option'],'pager')!==false)
$out_ajax['pager']=utf8_encode(getCreativePagerLite($page,$ct->total_items,$ct->items_per_page));
echo json_encode($out_ajax);
exit;
}else{
// Insert a Pager into the table (I used this CreativePager Lite version because its very easy to use, but you may use any pager system that you like)
$ct->pager = getCreativePagerLite($page,$ct->total_items,$ct->items_per_page);
}
?>
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/creative.css">
<title>Mild Steel Products</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js" ></script>
<script type="text/javascript" src="js/jquery.tools.min.js"></script>
<script type="text/javascript" src="js/cufon.js"></script>
<script src="js/Kozuka_Gothic.js" type="text/javascript"></script>
<!-- Fix for Internet Explorer 9 Beta -->
<!--[if gte IE 9]>
<script type="text/javascript">
Cufon.set('engine', 'canvas');
</script>
<![endif]-->
<script type="text/javascript" charset="utf-8">
// <![CDATA[
$(document).ready(function(){
Cufon.replace('h1,h2,h3', {
});
$(function() {
$("h3.message").delay(3000).fadeOut();
});
});
// ]]>
</script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/creative_table.min.js"></script>
<script type="text/javascript" src="js/creative_table_ajax.min.js"></script>
</head>
<body>
<div class="main">
<div class="header">
<div class="logo"><img src="images/logo.png" border="0" alt="logo" /></div>
<div class="menu">
<ul>
<li>Home</li>
<li>Order Online</li>
<li>Products</li>
<li>About us</li>
<li>Contact us</li>
</ul>
</div>
<div class="clr"></div>
</div>
<div class="header_text2">
<h2> Checkout</h2>
<p>Adjust your quantities, then click purchase. </p>
<div class="clr"></div>
</div>
<div class="clr"></div>
<div class="top_sup_resize">
<div class="menu2">
<ul>
<!--<li>Allthread</li>
<li>Hex Nuts</li>
<li>Washers</li>
<li>Hex Bolt & Nut Kits, Setscrews</li>
<li>Screws</li>
<li>Cup Head Bolts & Nuts</li>-->
</ul>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
<div class="body">
<div class="body_left">
<h2><?php echo $ItemAddedMessage; ?></h2>
<h3 class="message"></h3>
<div id="container">
<?php echo $out=$ct->display(); ?>
</div>
</div>
<p> </p></div>
<div class="clr"></div>
</div>
</div>
<div class="footer">
<div class="footer_resize">
<ul>
<li>home</li>
<li>order online</li>
<li>products</li>
<li>about</li>
<li>contact</li>
</ul>
<div class="clr"></div>
</div>
</div>
</body>
</html>
I am trying to submit a form on my page. It's proving to be a very difficult and frustrating task.
The problem is that there is no way of telling how many 'fields' will need to be submitted. The user might add 50 products to the cart, and make 30 changes to the quantities, or they might add 600 products to the cart and make 599 changes to the quantities (you never know), and with this website, the client is expecting high volume of sales so I need to be prepared for cases such as that.
I have a Table (a script that I bought) which has its own form tag for the data inside the table, however it uses GET not POST. And I am using:
foreach($_GET['items'] as $p=>$q)
{
// Update in database. This sql query has been tested, so I know it works, which means that the problem lies within the foreach loop, since it only stopped working when I added the foreach loop to update all form fields.
}
I have searched all ovver php.net, google and am out of ideas, and would greatly appreciate anyone's advice or help.
How can I update ALL form fields to the database?
Thanks
Edit:
The result of print_r($_GET); is:
Array ( [ct_items_per_page] => 10 [ct_sort] => t_t_t_t [ct_page] => 1 [ct_search] => [ct_items_per_page_change] => all [ct_multiple_search] => Array ( [0] => [1] => [2] => [3] => ) [product] => M5 x 1 MTR Allthread (Grade 304 Unit Price) [items] => Array ( [1] => 45 ) )
Edit:
What else would work in my situation? Does anyone have any links/tutorials/articles? I cannot find anything anywhere. Are there other ways to submit all fields to a database?
Lucifer,
You say you bought a script, why not initially contact who you bought it from for troubleshooting your issue?
What concerns me is that you list very little code, and so we are unable to help you because you simply say "it doesn't work! Please help me solve it!", well help us help you. Show us the FORM code, and the processing code.
Are you sure it is $_GET that the form is submitting as? Are you sure the variables are called $items[] in the form?
If I was solving this, or at least asking for help, I would list all the above code, and add plenty of debugging adding things like:
adding /* comment */ tags to comment out SQL inser sections (as you don't want to clean all that up each time you test).
adding var_dump tags to ouput as many variables as possible.
using Firefox Firebug for debugging if there is any javascript handlers that process the initial form.
UPDATE
11/23/2010
/checkout.php
I've done a quick review of your setup, you are using an AJAX table to manage searching / displaying your products. There are a couple methods you could try.
use jQuery serialize() to serialize (need to research this if you want to use this route) your data and submit it as a POST request to your backend (whatever that is? same checkout.php?).
You need to modify the checkout.php page to have an actual submit button, currently it is all handled via AJAX (javascript submit) which is setup for GET, not POST. You would have to update your FORM to be action="POST" as well. Add an actual submit button to your checkout form, you only have Update buttons which update the quantity.
Short of doing some consulting for you, there is not way to 'quickly' tell you what to change and where, this would take some time to modify, however I have outlined 2 options above you can tackle if you wish.

Dynamic title in php

I am Trying to get the page title (<title>bla..bla..bla..</title>) to be changable in php with a multi-file layout like so:
Functions.php is included into index.php, then get_header() is called from functions.php to include the page header.php the title tag is inside the header file. I would like to be able to set the title from index.php how can i do this?
For examle this is what i have tried:
Index.php:
<? require_once('includes/functions.php'); global $t; $t = '/home/s0urc3'; get_header();?>
<div id="main">
<h2>NEEDED</h2>
<p class="postmeta">Permalink | <span class="date">Revision Date</span></p>
<p>CONTENT AND CRAP</p>
<!-- main ends -->
</div>
<?php /*test*/echo($title);/*test*/ get_footer();?>
Header.php:
<?php //include('functions.php')?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<? title('$t')?>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<?php get_theme_css()?>
</head>
<body>
<!-- wrap starts here -->
<div id="wrap">
<!--header -->
<div id="header">
<h1 id="logo-text"><img src="<?php get_url('images/Logo.png')?>" alt="S0URC3"><!-- S0URC3 --></h1></div>
<p id="intro">
Just another poorly coded website!
</p>
</div>
<div id="nav">
<!-- <div id="navi"><div id="menu" class="fixed"> -->
<ul class="">
<li class="">Home</li>
<li class="">Blog</li>
<li class="">Forums</li>
<li class=""> Comments</li>
<!--<li class="">Clans</li>-->
<li class="">-astro-</li>
<!--<li class="">Inspiration</li>
<li class="">Resources</li>
<li class="">Tutorials</li>
<li class="">WordPress</li>-->
</ul><!-- </div></div> -->
</div>
<!--header ends-->
</div>
<!-- content-wrap starts -->
<div id="content-wrap">
Functions.php:
<?php
require_once('constants.php');
//===============//
//Start Functions//
//===============//
//Gets a file from the domain http://files01.s0urc3.ismywebsite.com/
function get_url($file)
{
echo (FILE_ROOT . $file);
}
//gets the url of the theme
function get_theme_css() {echo('<link rel="stylesheet" href="' . FILE_ROOT . 'colourise/style.css" type="text/css" />');}
function get_header() {require_once('includes/header.php');}
function get_footer() {require_once('includes/footer.php');}
//Gets the URL of the current page
function page_url($p)
{
$s = empty($_SERVER["HTTPS"]) ? ''
: ($_SERVER["HTTPS"] == "on") ? "s"
: "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
: (":".$_SERVER["SERVER_PORT"]);
if ($p == 'yes')
echo ($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
else
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2)
{
return substr($s1, 0, strpos($s1, $s2));
}
//gets the year
function cur_year() {echo (YEAR);}
function ads($code) {echo('<script type="text/javascript" src="http://links.ismywebsite.com?i='. $code .'"></script>');}
function title($title)
{echo('<title>Index Of: '.$title.'</title>');}
//=============//
//End Functions//
//=============//
?>
P.S. I only am including functions once all functions are available in the header and footer when functions.php is called in index.php
First, the file header.php is being included from within a function, so it does not have access to the global variable $t. You will need to do one of the following:
In functions.php:
function get_header() {
global $t;
require_once('includes/header.php');
}
Or, in header.php:
<?php //include('functions.php')
global $t;
?>
<!DOCTYPE html>
<!-- etc -->
That way, the variable you have declared will be available to the function's local variable scope.
Second, you need to call the function with either double quotes or no quotes at all. Single quotes will not parse a variable.
Right
title($t);
title("$t");
title("{$t}");
Wrong
title('$t');
Here is the PHP Manual Page on the String datatype -- be sure to check out single quoted strings versus double quoted strings.
Nitpicky Things:
You should always use the full opening tags (<?php), and not the short open tags (<?)
Your functions, like title(), should always return a value, and you can echo that, rather than echoing directly from inside the function.
The strleft() function you implemented, though clever, already exists. See: strstr() Actually, this function does the opposite of what you want; I mis-remembered what it did. Carry On. EDIT AGAIN: No, apparently I was right, you pass the optional third parameter as true. You should generally use functions that already exist because, with certain exceptions, they will be faster.
A Notable exception to this rule is array_rand()
Try to change
<? title('$t')?>
to
<? title($t)?>
in your code.

Categories