I have an Elseif statement, which gets a template name and includes the template PHP file which contains a large array, it outputs the result on the page.
$template = str_replace("-","_","{$_GET['select']}");
if ($template == "cuatro"){
include("templates/cuatro.php");
echo $page_output;
} elseif ($template == "ohlittl"){
include("templates/ohlittl.php");
echo $page_output;
} else {
echo "Sorry, template not found.";
}
$page_output = "You've chosen $template_select[0].";
From there, I get a notice saying it couldn't find the $page_output variable.
Notice: Undefined variable: page_output in C:\ ... \template.php on line 10
It can find it if I put the variable in the included file though. But I'm trying to get this variable to remain on this page. How do I complete this?
You are defining $page_output after you are echoing it. At the time you call echo $page_output it doesn't exist yet.
Try:
$page_output = "You've chosen {$template_select[0]}.";
$template = str_replace("-","_","{$_GET['select']}");
if ($template == "cuatro"){
include("templates/cuatro.php");
echo $page_output;
} elseif ($template == "ohlittl"){
include(dirname(__FILE__) . "/templates/ohlittl.php");
echo $page_output;
} else {
echo "Sorry, template not found.";
}
Although I have no idea how you are setting $template_select and if you are aware it will always say the same template name?
An alternative approach that I believe achieves what you want:
$templates = array('cuatro', 'ohlittl');
$selectedTemplate = strtolower(str_replace("-","_",$_GET['select']));
foreach ($templates as $template)
{
if ($template === $selectedTemplate) {
include(dirname(__FILE__) . "/templates/" . $template . ".php");
echo "You've chosen {$template}.";
}
}
Either your template
Outputs the text directly (echo)
Stores the result in a global (e.g. $page_output) or a local variable (in the include happens inside a function, but that's transparent for the template).
Returns the output (yes, includes can return values).
You seem to want option 2, yet your templates are not defining any $page_output variable. You could also output the text directly in the templates, buffer the output, and assign it to $page_output:
ob_start();
include "file.php.inc";
$page_output = ob_get_contents();
ob_end_clean();
Related
I have searched almost everywhere read different documents but still I am unable to find answer of question which is Why we can't use variable outside if(isset($_POST['sign'])) block which posted from python?
Consider this below code.
$sign = '';
if(isset($_POST['sign']))
{
$sign = $_POST['sign'];
if(!is_dir($sign))
{
mkdir($sign);
echo "folder created";
}
else
{
echo "folder already exists";
}
}
echo $sign;
Here I am getting value coming from python in $sign it is working fine inside if block but when I echo it outside if block it is showing noting on browser.
I want to make a call of this php logic inside a html div but when passing it as a function the logic breaks since it does not send an error message in case of entering the pass wrong and its confirmation at the time of performing the password change.
<?php
require 'funcs/conexion.php';
require 'funcs/funcs.php';
$user_id = $mysqli->real_escape_string($_POST['user_id']);
$token = $mysqli->real_escape_string($_POST['token']);
$password = $mysqli->real_escape_string($_POST['password']);
$con_password = $mysqli->real_escape_string($_POST['con_password']);
if(validaPassword($password, $con_password))
{
$pass_hash = hashPassword($password);
if(cambiaPassword($pass_hash, $user_id, $token))
{
echo "Contraseña Modificada <br> <a href='index_alumnos.php' >Iniciar Sesion</a>";
} else {
echo "Error al modificar contraseña";
}
} else {
echo "Las contraseñas no coinciden <br> <a href='index_alumnos.php' >contacta a Academia</a>";
}
?>
If the echo happens before your actual div is drawn, the echo goes... right where it happens. Which isn't within your div.
One way of getting around this would be to put your error message into a variable and then deliver this variable into your div (whether it be through a return value, if it's a call, or some other means.)
Here's a simple example to illustrate this:
<?php
if(1 === 2) {
//great, 1 is 2
} else {
//oh no, an error
$someErrorLine = '1 is not 2';
} ?>
<h1>Hi</h1>
<div><?= $someErrorLine ?></div>
You could also check if the variable exists, something like if(isset($someErrorLine)) {} and echo the div with it, or put the div within your variable.
With my function what I have written I try thereby 2 things.
The links should be called like this http://localhost/?login=Bla, Now it is like this http://localhost/login,php?login "Bla
Next I would have asked, in my function a 1 is given after each call. I just can't figure out where this comes from, I've been sitting on this problem for a long time.
Output with the 1
This is the code with which I can call the pages
function Seite($pagename, $lay){
function Seite($pagename, $lay){
$path = "$lay/$pagename.php";
if (file_exists($path)) {
openSeite($path);
}
}
function openSeite($pageurl){
$fc = require($pageurl);
echo $fc;
}
function echopage($slug, $fade){
// $slug = ?SLUG=Seite
// $fade = Ordner des Layout
$page = isset($_GET["$slug"]) ? $_GET["$slug"] : "error";
$contente = seite($page, "$fade");
echo $contente;
}
I call the content on the index.php with
<? echopage("login", "admin/layout"); ?>
isset($_GET["$slug"]) returns a 1 because it is set (true), write a traditional conditional with the echo inside the if statement.
*Better Yet assign your output to a variable and concatenate the values accordingly.
$output = NULL;
if(isset($_GET["$slug"]){
$contente = seite($page, "$fade");
$output .= $contente;
}else{
//handle error
}
HTML:
<?=$output?><!--Output your displayed text held in the variable-->
ISSUE:
$page = isset($_GET["$slug"]) ? $_GET["$slug"] : "error";
You are essentially returning the set value, which is 1 also true.
From php manual for value: Returns TRUE if var exists and has any value other than NULL. FALSE otherwise.
You can test this by simply writing out a line of code echo isset($var); and checking the test php page. Then try defining a variable and doing the same thing. $var = "this is set"; then echo isset($var);, you will get a 1.
This issue came up to me in vBulletin system. i want to build a block in sidebar area to help me autmate some daily works.. i wrote php code like this :
<?php
$myDay = date('D');
$myHour = date('G');
// Saturday
if ($myDay == "Sat") {
if ($myHour >= 7) {
$output = include('textFilePath/saturday.txt');
} else {
$output = include('textFilePath/friday.txt');
}
}
// Sunday
if ($myDay == "Sun") {
if ($myHour >= 7) {
$output = include('textFilePath/sunday.txt');
} else {
$output = include('textFilePath/saturday.txt');
}
}
// and it goes on to friday...
// and in the end :
return $output;
?>
my problem is with include() function . when i return the $output value it returns a boolean(0 or 1) and include function writes out the txt files content in the beginning of document instead of "sidebar block"
i know i can echo the actual content instead of using include and txt files. but as i said i want to automate the daily tasks and give the users access to edit the txt files.
is there any other technique or function to assign the content of a local file to a variable ?
you may want to check
$output = file_get_contents('textFilePath/saturday.txt');
more information here : http://in3.php.net/file_get_contents
<h1><?php echo $GLOBALS['translate']['About'] ?></h1>
Notice: Undefined index: About in page.html on line 19
Is it possible to "catch" an undefined index so that I can create it (database lookup) & return it from my function and then perform echo ?
The easiest way to check if a value has been assigned is to use the isset method:
if(!isset($GLOBALS['translate']['About'])) {
$GLOBALS['translate']['About'] = "Assigned";
}
echo $GLOBALS['translate']['About'];
You can check if this particular index exists before you access it. See the manual on isset(). It's a bit clumsy as you have to write the variable name twice.
if( isset($GLOBALS['translate']['About']) )
echo $GLOBALS['translate']['About'];
You might also consider changing the error_reporting value for your production environment.
that wouldn't be the right thing to do. i would put effort into building the array properly. otherwise you can end up with 1000 db requests per page.
also, you should check the array before output, and maybe put a default value there:
<h1><?php echo isset($GLOBALS['translate']['About'])?$GLOBALS['translate']['About']:'default'; ?></h1>
try something like:
if ( !isset($GLOBALS['translate']['About']) )
{
$GLOBALS['translate']['About'] = get_the_data('About');
}
echo $GLOBALS['translate']['About'];
Yes. Use isset to determine if the index is defined and then, if it isn't, you can assign it a value.
if(!isset($GLOBALS['translate']['About'])) {
$GLOBALS['translate']['About'] = 'foo';
}
echo "<h1>" . $GLOBALS['translate']['About'] . "</h1>";
You need to define your custom error handler if you want to catch Undefined index
set_error_handler('exceptions_error_handler');
function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
try{
}catch(Exception $e){
echo "message error";
}