How can I put all includes on one page with classes? - php

For a school project I need to put all returning code into an include. I want it all on one page, but I can't get it to work. Creating an include is no problem, but I want them all in one PHP page. I have the following code, but it is wrong:
//footer
<?php
class footer
{
echo 'Account';
echo 'Subscription';
echo 'About us';
echo 'Terms of Use';
echo '<small>© 2017, (myname) & (name partner)</small>';
}
?>
How do I have to modify the code, so that it is correct?

Assign the html code to variables.
<?php $footer = "Account
Subscription
About us
Terms of Use
<small>© 2017, (myname) & (name partner)</small>"
?>
After that you only need to include the one PHP file and <?php echo $footer ?> for all parts of your page

You got it wrong. You will only declare methods(functions), properties(variables) and constants directly in a class body.
If you want to run echoes, you should declare it inside a method.
An example for your case:
class Footer
{
public static function printLinks()
{
echo 'Account';
echo 'Subscription';
echo 'About us';
echo 'Terms of Use';
echo '<small>© 2017, (myname) & (name partner)</small>';
}
}
Then call it in every place that you want it as:
Footer::printLinks(); //This will call all those echoes at once

Related

PHP Function echo included file

I'm currently developing a wordpress plugin. I created a shortcode which displays the content of another html/php file this looks like this:
function df_display_form()
{
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
}
My problem is that the shortcode will be showed at the top of the page. So I googled this issus and found a solution. As it is writte there the problem is that return should be used instead of echo.
So my question is how can I return the renderd content from the included file? (not echo).
Try using an output buffer.
https://www.php.net/manual/en/function.ob-get-clean.php
function df_display_form()
{
ob_start();
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
$out = ob_get_clean();
return $out;
}
You have to use php fuction to display the form using shortcode.
I think you have currently this type of code in show-dynamic-form.php file.
<?php
// Some codes
?>
<form>
Form elements
</form>
It will just echo your code to the top of your post or page. Correct way is:
<?php
// Some codes
$variable = '<form>';
$variable .= 'form elements';
$variable .= '</form>';
return $variable;
?>
Try to use this way in your dynamic-form.php file and it should work.

Page Title Based on Class/Function Variable PHP

New to classes, and I'm having an issue dynamically changing a page's title. I think I may know what the issue is, but I don't really know what to do in order to fix the problem. I have two pages, admin.php (where the class is housed) and display-admin.php (displays what the user sees). Within admin.php this is what I have:
class Admin {
public $title;
public function login() {
require("login-form.php");
return $this->title = "Login";
}
...
}
$o = new Admin();
This is what I have within display-admin.php:
<?php
include_once("admin.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
if(isset($o->title)) {
echo "<title>My Site | " . $o->title . "</title>";
} else {
echo "<title>My Site</title>";
}
?>
</head>
<body>
<?php
...
switch($action) {
case "login":
$o->login();
break;
default:
$o->displayPages();
}
...
?>
</body>
</html>
The title always stays as My Site. It never adds the title needed. Now, when I tested echoing $o->title at the end (before the <\body> tag), it displayed the string I passed. Is this because I am trying to echo the variable before calling the function? If so, how do I fix in order to display the title, and have my content displayed within the body?
Thanks in advance for the any/all help/suggestions.
In your case it isnt work because $o->title doesnt exist.
It is created in your "login()" method, and this is after your
if(isset($o->title)) {
So you can do this:
class Admin {
public $title;
public function __construct() {
$this->title = "Admin";
}
}
You can do it per example in the constructor of your Admin Class.
The constructur is a magic method. It will be automatically called directly if you created your class.
Hope this will help you.
If you create an object from class Admin the title will be change into 'Admin'
In your Admin Page you can do this:
$o = new Admin();
echo "<title>".$o->title."</title>";

php code within an html code stored inside a php variable, how can this work?

I am currently making a simple website, I have a file called 'functions.php', where I store some simple functions and this piece of code
<div id=\"topContactPan\">
<div id=\"topMenuPan\">
<div id=\"topMenuLeftPan\"></div>
<div id=\"topMenuMiddlePan\">
<a href= <?Php if(!admin_class::isadmin()){
echo "\"userprofile.php\" >User Profile</a></li>";}
else { echo "\"adminprofile.php\" >Admin</a>"; } ?>
I needed to escape all the " so it would interpret properly since this is within a php file, all of this is stored inside a variable $top, so whenever i create a new page of my website, i just echo $top and echo $bottom and theres the top header and bottom footer, only the content is missing.
but with the code above, I wanted one of the links on the navigation bar to switch where is is pointing to depending if the one who is logged in is an admin or a regular user. But it only seems that when I click the link, the header becomes http://localhost/YMMLS/<?Php.
Any suggestions? thanks so much
make 2 files.
header.php and footer.php
inside of each of those write your html as you normally would - then on your index or what ever page just say
include('header.php');
<p>YOUR HTML PAGE AND DOC<p>
include('footer.php');
No need to go through escaping everything.
I would do like this in your functions.php:
<?php
function template() {
?>
<div id="topContactPan">
<div id="topMenuPan">
<div id="topMenuLeftPan"></div>
<div id="topMenuMiddlePan">
<?php checkuser(); ?>
<?php
}
function checklink() {
if(!admin_class::isadmin()) {
echo "userprofile.php";
} else {
echo "adminprofile.php";
}
}
function checkuser() {
if(!admin_class::isadmin()) {
echo "User Profile";
} else {
echo "Admin";
}
}
?>
Then in your file you just include it and call its function :
<?php
include('functions.php');
template();
?>
It is not the best solution maybe. :D

How to pass $$variables to view from controller in CakePHP?

in AppController:
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
in Controller class:
function companyinfo() {
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
$welcome = 'welcome to $$company!';
$this->set(compact('logo','welcome'));
}
function beforeFilter() {
parent::beforeFilter();
}
in View class:
<html>
<body>
<?php echo $logo; ?>
<?php echo $welcome; ?>
</body>
</html>
it doesn't answer variable in view after passing the variable from AppController via controller..
1) When you use $this->set(compact('company'));, it is NOT setting a variable for use in any controller - it's passing $company to the view.
2) You're trying to write PHP code in a string, using a Helper (which are only available in Views)
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
3) It's unusual to want to pass data from AppController to Controller to View.
What you probably want to do is something like this:
//App Controller
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
//Controller
function companyinfo() {
$logo = 'logo';
$this->set(compact('logo'));
}
//Layout file (or view file, but I assume it's layout since you're getting data in the AppController)
<?php
echo '<div>' . $this->Html->image($logo) . '</div>';
echo "Welcome to " . $company;
I mean this in the most constructive way possible (we've all been there). It seems like you're struggling with some general PHP concepts. Before you get too heavy into CakePHP, I recommend trying out a few lengthy tutorials in generic PHP - then when you feel completely comfortable with it, dive into CakePHP.
To get company into the view you can set it in your appcontroller if you want it avaiable in ALL views throughout your site or inside a specific controller if you only want it available in the view of the function you set it inside of. Either way you'll need to make correct use of the set function. For example:
$this->set('company', 'Name of Company');
OR
$company = 'Name of Company';
$this->set('company', $company);
Afterwards you'll be able to access the $company variable in the view.
echo $company; outputs Name of Company
As for your question for Dave:
<?php $welcome = 'Welcome to $$company'; ?> <html><body><?php echo $welcome; ?></body></html>
would be written as:
<html><body><?php echo "Welcome to ". $company; ?></body></html>
However, you should really look into using layouts with cakephp so you don't need to the <html>, <head>, <body>, etc. tags in every view file

Quiting a PHP script within another PHP page

I'm trying to implement caching for a PHP script I'm writing, but I keep running into the following problem. I want the script to be included in other PHP pages, but when I try to pass the cached file and exit the embedded script it exits both the script and the parent page, but doesn't parse the rest of the code on the parent page. See the code below for an example.
index.php
<?php
echo "Hello World!<br />";
include("file2.php");
echo "This line will not be printed";
?>
file2.php
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
exit; // This stops both scripts from further execution
}
// Additional code here
?>
If the above index.php is executed you get the following output:
Hello World!
file2.php has been included
However, I'm trying to get it to look like this:
Hello World!
file2.php has been included
This line will not be printed
Use return; instead of exit; in the included file - this will only halt execution of that script.
Note that you an also use this to return a value to the parent script e.g.
file1.php
<?php
echo 'parent script';
$val = include('file2.php'); //$val will equal 'value'
echo 'This will be printed';
file2.php
<?php
echo 'child script';
return 'value';
Just wrap the "additional code here" in an else statement?
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
} else {
// Additional code here
}
?>
Otherwise I'm not sure what you're getting at. The exit command always terminates the current execution in whole - not just execution of the current file (for which, there is no command)
EDIT
Thanks to comments and posts by PHLAK, tomhaigh, MichaelM, and Mario, I myself learned something today - that you CAN indeed terminate the execution of a single included file w/the return command. Thanks, guys!
I personally try to avoid if-else conditions where possible and use (not sure if there's a coined term for it but) early-exit intercepting conditions.
index.php
<?php
echo 'header';
include 'content.php';
echo 'footer';
?>
content.php
<?php
if ($cached)
{
echo cached_version();
return; // return is not just for functions, in php...
}
//proceed with echoing whatever you want to echo if there's no cached version.
...
...
?>
Why not encapsulate the contents of file2.php into a function. That way you can return from the function when you need to, and the rest of the execution will not halt. eg:
file2.php
<?php
// this function contains the same code that was originally in file2.php
function exe()
{
$whatever = true;
if ($whatever)
{
echo "file2.php has been included <br />";
// instead of exit, we just return from the function
return;
}
}
// we call the function automatically when the file is included
exe();
?>
Leave index.php exactly as it is and you should see the output you are trying to achieve.

Categories