I'm writing PHP code, where I have function, in this function I have include, it works fine but I get error if I'm write new code in this include file
Error:
Notice: Undefined variable: text
Function:
function inc($templateinc){
if(file_exists(Fsys.Fview.$templateinc)){
include Fsys.Fview.$templateinc;
}
else {
include Fsys.Fview.'errors/404.php';
}
}
Where I'm printing function:
$text = "text";
inc("main/index.php");
main/index.php file:
echo $text;
How can I fix this problem?
Thank you
instead of
inc("main/index.php");
try
include_once("main/index.php");
Don't know what are you trying to achieve.
Just put $text = "text"; inside main/index.php
and change the code to inc("main/index.php");
echo $text;
Basically, $text is not defined inside that index.php
function inc($templateinc,$data){
if(file_exists(Fsys.Fview.$templateinc)){
include Fsys.Fview.$templateinc;
}
else {
include Fsys.Fview.'errors/404.php';
}
}
$data=array();
$data['title']='Some title...';
$data['text']='This is page content...';
$data['array']=array(1,2,3,4);
inc('test.php',$data);
test.php:
echo $data['title'];
echo $data['text'];
foreach ($data['array'] as $var) {
echo $var;
}
So, $data ($text) should be passed as argument in your function.
Related
I am currently trying to create a small template engine for a project that I am working on, and I am using a system where I am replacing {$tag} with a preset tag. So say I put {username} in my template file, it will return a string which is the username. Now I want to go beyond just a simple string replacing a string. So using the same code I put
$tpl->replace('getID', '<?php echo "test"; ?>);
And it didn't work, so when I went to inspect element, I saw that it returned <!--? echo "test"; ?-->...
So now I am just trying to figure out why it returned commented code.
Here is my class file:
class template {
private $tags = [];
private $template;
public function getFile($file) {
if (file_exists($file)) {
$file = file_get_contents($file);
return $file;
} else {
return false;
}
}
public function __construct($templateFile) {
$this->template = $this->getFile($templateFile);
if (!$this->template) {
return "Error! Can't load the template file $templateFile";
}
}
public function set($tag, $value) {
$this->tags[$tag] = $value;
}
private function replaceTags() {
foreach ($this->tags as $tag => $value) {
$this->template = str_replace('{'.$tag.'}', $value, $this->template);
}
return true;
}
public function render() {
$this->replaceTags();
print($this->template);
}
}
And My index file is:
require_once 'system/class.template.php';
$tpl = new template('templates/default/main.php');
$tpl->set('username', 'Alexander');
$tpl->set('location', 'Toronto');
$tpl->set('day', 'Today');
$tpl->set('getID', '<?php echo "test"; ?>');
$tpl->render();
And my template file is:
<!DOCTYPE html>
<html>
<head></head>
<body>
{getID}
<div>
<span>User Name: {username}</span>
<span>Location: {location}</span>
<span>Day: {day}</span>
</div>
</body>
</html>
You're redeclaring PHP in a php file when there is no need to. i.e. you're trying to print <?php which is why it's messing up.
So, you can replace this:
$tpl->set('getID', '<?php echo "test"; ?>');
with this
$tpl->set('getID', 'test');
But, you obviously already know that, you're just trying to go further, the way to do this is by using php inside the set. So, as an idea, you could try this:
$tpl->set('getID', testfunction());
(You're calling testfunction here to define the 'getID' here btw)
So, now you want to write a little function to do something fancy, for the sake of this example:
function testfunction(){
$a = 'hello';
$b = 'world';
$c = $a . ' ' . $b;
return $c;
}
The above should then return hello world in place of {getID}
In reference to your comments - if you want to go one step further and start being more advanced with the return results, you can do the following:
function testfunction(){
$content = "";
foreach ($a as $b){
ob_start();
?>
<span><?php echo $b->something; ?></span>
Some link
<div>Some other html</div>
<?php
$content += ob_get_clean();
}
return $content
}
Hello Id like to know how to call the function I just written from URL?
Bellow are my php code.
<?php
require 'db.php';
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
?>
I tried http://127.0.0.1/study/sf.php?function=l1 but it wont echo the written code.
Please point me to the right direction.
Yes you could supply that parameter into calling your user defined function:
$func = $_GET['function'];
$func();
Might as well filter the ones you have defined thru get_defined_functions
function l1(){
echo "Hello there!";
}
function l2(){
echo "I have no Idea what Im doing!";
}
function l3(){
echo "I'm just a year 1 college student dont torture me sir!";
}
$functions = $arr = get_defined_functions()['user']; // defined functions
if(!empty($_GET['function']) && in_array($_GET['function'], $functions)) {
$func = $_GET['function'];
$func();
}
Sample Output
Sidenote: function_exists can be also applied as well:
if(!empty($_GET['function']) && function_exists($_GET['function'])) {
// invoke function
}
One option you can do if use if/elseifs like so:
if($_GET['function'] == 'l1')
{
l1();
}
else if($_GET['function'] == 'l2')
{
l2();
}
Or you could use a riskier approach and call the function name directly from the input.
$functionName = $_GET['function'];
$functionName();
Edit:
Or you could use a switch statement:
switch($_GET['function'])
{
case 'l1':
l1();
break;
case 'l2':
l2();
break;
case 'l3':
l3();
break;
}
this time i have a hard problem. I have:
[folder] (file)
Structure directory
[class]
- (class.page.php)
- (class.main.php)
[core]
- (core.test.php)
Now class.data.php
<?php
class DataTools {
public function clean($string) {
if (!empty($string)) {
$string = addslashes($string);
$string = mysql_real_escape_string($string);
$string = (string)$string;
$string = stripslashes($string);
$string = str_replace(" ", "", $string);
$string = str_replace("(", "", $string);
$string = str_replace("=", "", $string);
return $string;
} else {
echo "Error";
die();
}
}
Now class.page.php
<?php
class Page {
public function __construct {
include "class.data.php";
$data = New DataTools();
}
?>
Now core.test.php
<?php
require_once "../class/class.page.php";
$page = new Page;
$nome = $data->clean("exemple"); // line 13
?>
When i open class.test.php it display this:
Fatal error: Call to a member function clean() on a non-object in /membri/khchapterzero/core/core.test.php on line 13( this is not important becouse i reduced the page for the topic, but the line in the original page was that i posted, the other line was comments)
This seems ok, if all files are in one folder it works fine, i try and there was no error. check your structures and names.
I check on:
Test->
class.data.php
class.page.php
core.test.php
in include only filename.
So checka again your paths
$data is defined in the Page object, it is not available as a variable in the global scope. And because you are not storing it as a class member of the Page obejct, it is also lost when PageĀ“s constructor resolves.
To fix this:
First make $data a class member of the Page class, so that it is not discarded after the constructor is done
<?php
class Page {
public function __construct {
require_once "../include/class.data.php";
$this->data = New DataTools();
}
?>
Then, access this data variable inside the Page instead of trying to call $data directly:
$nome = $page->data->clean("exemple");
I have next to no experience with php as a language, and am running it a little problem in producing a Drupal theme. What I need is to execute a function once, that will return a Boolean, then use that Boolean throughout the template.
Here is what I have so far:
html.tpl.php->
<?php
function testMobile(){
return false;
}
define('isMobile', testMobile());
?>
...
<?php
if(!isMobile){
echo '<h1>NOT MOBILE</h1>';
}else{
echo '<h1>IS MOBILE</h1>';
}
?>
page.tpl.php->
<?php
if(!isMobile){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>
In the drupal output I get this ->
NOT MOBILE
NOT DESKTOP
along with this error message:
Notice: Use of undefined constant isMobile - assumed 'isMobile' in include() (line 77 of /Users/#/#/#/sites/all/themes/#/templates/page.tpl.php).
what am I doing wrong here? How can I most easily achieve my goal?
It seems that the defined variable is falling out of the scope of the template file. You can simply solve this by using a session variable.
Below is a code sample ...
session_start(); // not necessary with drupal
$_SESSION['isMobile'] = testMobile();
function testMobile(){
return false;
}
In your template you can add following...
<?php
if(!$_SESSION['isMobile']){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>
Try to define variable in template.php in hook_theme_preprocess_page(&$vars, $hook).
So template.php can looks follow way:
function testMobile(){
return false;
}
function YOURTHEME_theme_preprocess_page(&$vars, $hook) {
$vars['isMobile'] = testMobile();
}
And page.tpl.php
<?php
if(!$isMobile){
echo '<h1>IS DESKTOP</h1>';
}else{
echo '<h1>NOT DESKTOP</h1>';
}
?>
so im making homepage with three languages.
I am using switch method, here is code -
public function languages()
{
if (isset($_GET['lang']) && $_GET['lang'] != '')
{
$_SESSION['lang'] = $_GET['lang'];
}
else
{
$_SESSION['lang'] = 'en_EN';
}
switch($_SESSION['lang'])
{
case 'en_EN': require_once('language/lang.eng.php');break;
case 'lv_LV': require_once('language/lang.lv.php');break;
case 'ru_RU': require_once('language/lang.ru.php');break;
default: require_once('language/lang.eng.php');
}
}
public function translate($txt)
{
global $text;
return $text[$txt];
}
and it should display in index.php file like this -
<?php $index->translate('search'); ?>
but the problem is that it shows no errors, no notices, no warnings and no translated or default text.
I included function languages() , maybe you can help me with this problem?
EDIT:
im calling $language at start of index.php file - <?php require_once('class.index.php'); $index = new index; $index->languages(); ?> and $text is defined in lang.eng.php; lang.lv.php and lang.ru.php file.
Since you're using a class, I think it's better to use properties instead of globals, it will be easier in future mantainance. Create a class variable holding $text and use that
class Index {
var $text;
public function languages()
{
require(".....");
$this->text = $text;
}
public function translate($txt)
{
if(isset($this->text[$txt]))
{
return $this->text[$txt];
}
else
{
return "no translation";
}
}
}
$index = new Index;
$index->languages();
echo $index->translate('search');
type
<?php echo $index->translate('search'); ?>
Check first whether the session is initialized or not and also place the languages() function call before the translate so that it loads the language prior to translation and also put error_reporting(E_ALL) at top so that any error suppresion will be be removed and also put echo the returned result of translate statement