PHP placement problem - php

I am calling this function halfway down the page:
<div id="leftArea">
<?php
if (isset($id)){
single($id);
}
?>
</div>
The problem is i want use some of the output in the meta tags in the <head>, whats the best way to approach this?
EDIT:
The single function, takes the $id and echo's the data straight to the page, exactly where it is. In the DIV leftArea. But i want to take one of the rows from the DB and insert it into the META tags at the top

Copy the code into the <head> section.

Redesign your System
The best method is to create a class that manages your html page for you, example:
$Page = new HTMLPage("My Page",HTMLPage::Strict);
$Page->addScript("....");
$Page->addScript("....");
$Page->addScript("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->SetBody($MyTemplate);
$Page->send();
this way though out your functions you can do
function myfunc()
{
global $Page;
$Page->addScript("....");
}
the main point here is you should build your document up before sending it to the browser, this way you still have control over the content no matter where your code is executing from.
on the final send method you build your content up, and then push the content via echo, and then exit directly. (all processing should be done prior to output to manage errors)

Related

PHP includes header or footer location

I was hoping someone could help. I have just started to dabble with PHP includes for time saving in the future. For example I want to change the footer and header on a web page once (using include) instead of copying and pasting the code 30 or 40 times - oh no... a typo start again.
Which brings me to the question(s) where is it best to place this script?
<?php include("includes/headernav.html"); ?>
Can it be placed in a div, or should it be placed at the top of your code under the body?
If I want to make an image/banner include module. Can I
<?php include("includes/image.jpg"); ?>
Or is best to wrap the image in html and apply like this?
<?php include("includes/imagewrapped.html"); ?>
Do not include .jpeg files directly, use a wrapper. Only use include with other PHP files.
As for including the header, do it any way that feels natural as long as it produces valid html. There is no particular reason to declare another div element.
Hope this helps:
<?php include("includes/ui_header.php"); ?>
My page content between header and footer
<?php include("includes/ui_footer.php"); ?>
You can probably save this as a function and call that function wherever you want to display.
It doesn't matter whether you put include in any place. However, it's better to put include in the top or bottom of your code
While including headers/footers/menus on the site, please keep in mind following things:
1) Your header/footer includes(blocks) should be wrapped inside a div.
2) This way then can be differentiated and any new change to them can be done easily.
3) Its always a good practice to include a wrapper div around an element as CSS can use it for styling.
4) Your header/footer includes (blocks) should have a flexibility that even we place them in header,footer or any sidebar, they should not disturb the UI.
1) Because you are including the HTML file, you probably need to include it where you want to display it.
2) Create a function in php where you send only image URL (maybe some other parameters) and function returns the HTML code (String) which you only echo on page where you want to display it. This way you can ensure, that all images will have the same code and styling.
for example
function generateImage($url=null) {
if (isset($url)) return '<img src='.$url.' style="width: 100px; height:100px; border: 1px;" />';
else return false;
}
The better way is to include always a php file.

PHP: Echo function output in function-line, not line of function call

I have a problem with my self-coded template system. The content is inserted by Include(). Now I need to add a meta redirect to one page. I know, meta redirect is not the safest way, but I need it because of it's delay possibility.
Now i'm looking for a way to influence the wrapping page (template) by the wrapped page (content).
So I thought a function can do this job.
<?php function test($testvar){
echo $testvar;}
?>
<hr />
<?php
test("testtext");
?>
Of course the text echos in the line of the function call, not in the function line. Is there a way to make the function echo in the line of the function itself? In this case above the horizontal rule, not below?
Of course every other best pratice for this "template problem" ist welcome!
THX
EDIT:
<html>
<title>testpage</title>
<? a_function_that_echos_a_metatag_for_redericetion($param){
echo $param;
}
?>
<head>
<body>
<? include("test.php");
INCLUDED script---->
a_function_that_echos_a_metatag_for_redericetion("\"<meta http-equiv=\"refresh\"...")
Procedural programming cannot have the logic you request.
If you want to add the meta redirect element before the page content, you HAVE to know that you are GOING to make this redirect before the <head> is closed.
Is there a way to make the function echo in the line of the function
itself?
No. Functions are declarations and aren't executed until they are called.
If you wish to modify the request headers later down the page, then you'll need to employ output buffering. However, this approach will likely be overkill if only one page needs the special header consideration. It may be better to just detect the page via some parameter (such as REQUEST_URI) and insert the header conditionally from that.

str_replace (or another option) for replacing content located inside a php document

I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.

Get css class of div with php

I don't even know if this is possible but hopefully someone will be able to point me in the right direction.
Basically I want to know if there is a way of getting the css class of a div and then displaying content based on that class.
So for example if;
<body class="home">
Then a div would display as follows;
<div><p>This is the Home page</p></div>
Like I said, I don't even know if this is possible but any help would be greatly appreciated.
Thanks
What you're trying to do can be done with Javascript, but if you want to use php only, then try to use php before you provide the "class" parameter. For example, if $_GET['class']=="home" then <div class="<? echo $_GET['class']?>">some text</div>
Perhaps, you can use Javascript, with IDs for example:
<div id="home"></div>
<script>document.getElementById('home').InnerHTML = "this is text for home";</script>
Hope it helps!
See you point, but you goes the wrong way. If you want to put all content in one page differs by some query param, there is no need to do so. You just can hide unneeded blocks with css and show them with js. On the other hand, if this is some sort of server-side utilization, there is definitely no reason to do so too. On the server you can totally control the output, so make separate templates.
Is there a reason not to use PHP instead of reading the class of a div?
#index.html
<html>
<?php include /contentDefinitions.php; ?>
...
<?php $content = home; ?>
...
</html>
#contentDefinitions.php
<?php
if($content = home){
<p>This is the homepage. I am a happy paragraph.</p>
}
?>
**this would be a little more efficient with an array or something,
but at the end of the day the easiest thing would just be to include
home.php, page2.php, page3.php etc. as needed instead of going the
route of variables etc... though having an array would let you edit
all the content within one file.
I'm no master of code and have zero familiarity with Joomla, so this may be absolutely useless to you. :)

What's the best way to automatically print an HTML header and bottom in php?

Currently I'm using:
PrintHeader();
/* Page code... */
PrintBottom();
Is there a better way?
Any output shouldn't be done automatically. As not every script call may return a page of text. So, you have to call page renderer manually.
So, you can make it with single call CallTemplateRender();
Not a much difference though
Its best to separate your business logic and display code, I typically use a master php page with a variable that includes an inner php page. Something like this:
<html>
<head></head>
<body>
include($template_file);
</body>
</html
Then my logic page works like this:
//do processing
$set_variables;
$template_file = "inner_file.php";
include("master_template.php");
Of course its a little more involved than that but all of my logic and display is separate so it's easier to manage.

Categories