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>';
}
?>
Related
I do not know what I'm doing wrong. Perhaps someone could advise me. I'm trying to define seasons and then use the result in index.
...
File with function:
function getCurrentTheme($for_area) {
// Definition of seasons
$spring='spring theme title';
$spring_season=array('03-21',......);
$summer='summer theme title';
$summer_season=array(......);
$autumn='autumn theme title';
$autumn_season=array(......);
$winter='winter theme title';
$winter_season=array(......);
// Today
$current_date=date('m-d');
// So what season is now?
if ($for_area==='some_area') {
if (in_array($current_date,$spring_season) {
$theme=$spring;
}
else if (in_array($current_date,$summer_season) {
$theme=$summer;
}
else if (in_array($current_date,$autumn_season) {
$theme=$autumn;
}
else if (in_array($current_date,$winter_season) {
$theme=$winter;
}
else {}
}
else if ($for_area==='other_area') {
// ...
}
else {}
return $theme;
}
...
Index:
$area='some_area';
getCurrentTheme($area);
// And here is the fault. Hope sometimes I will stop being retarded.
echo $theme;
// What should be printed?
summer theme title
Thanks in advance and please try understand my innocence.
You need to store the value, so that you can then echo it:
$area='some_area';
$theme = getCurrentTheme($area);
echo $theme;
You're not storing the value returned from the getCurrentTheme function. Either store it in a variable and then echo it
$theme = getCurrentTheme($area);
echo $theme;
or simply echo the call without having to use a variable.
echo getCurrentTheme($area);
Your choice!
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.
I am using Twitter Bootstrap to style validation_errors for a registration page:
<?php
echo "<div class='alert alert-error span4'>";
echo validation_errors();
echo "</div>";
?>
The validations work and show up but part of the styling is always present (the div tag has a red background). Is there a way to have the styling show up ONLY when the validation_errors are present. I have tried a few things (embedding html in php tags and enclosing the php in div tags) but the result is the same.
The reason that the div structure still appears is because it's echoing without regard to whether there are errors or not.
You could set the value of a variable to the result of your validation_errors() function in your Controller, then only display the alert div in your View if you've actually got an error...
Your controller would assign the variable to hold the (potential) error:
$this->data['reported_error'] = validation_errors();
$this->load->view('view_name', $this->data);
Then your view would only display the alert div if there was an error:
if ( isset($reported_error) )
{
echo "<div class='alert alert-error span4'>".$reported_error."</div>";
}
This requires your validation_errors function to only return a value if there is an error.
You can try something like this it works for me.
<?php if(validation_errors()):?>
<div class='alert alert-error span4'><?php echo validation_errors(); ?></div>
<?php endif;?>
I use the following construct to put the errors with special classes next to the fields that they reference:
<div class="<?php echo my_error_class('fieldname') ?>">
<input type="whatever" name="fieldname" />
<span class="whatever"><?php echo my_error_msg('fieldname') ?></>
</div>
with the following functions in a CI helper:
<?php
if ( ! function_exists('my_error_class')) {
function my_error_class($field, $error_class = "error") {
if (FALSE === ($OBJ =& _get_validation_object())){
return '';
}
if(isset($OBJ->_field_data[$field]['error']) && !empty($OBJ->_field_data[$field]['error'])) {
return $error_class;
}
else {
return '';
}
}
}
if ( ! function_exists('my_error_msg')) {
function my_error_msg($field,$default = '') {
if (FALSE === ($OBJ =& _get_validation_object())){
return $default;
}
if(isset($OBJ->_field_data[$field]['error']) && !empty($OBJ->_field_data[$field]['error'])) {
return $OBJ->_field_data[$field]['error'];
}
else {
return $default;
}
}
}
I was having the same issue. I assigned validation_errors() to a variable in my controller and passed it to the view.
Going off James' solution, I had to change:
if ( isset($reported_error) )
to:
if (!empty($reported_error))
Once I did this, the alert block only displayed when there was an error.
I didn't find this in the guide but apparently validation_errors takes two optional args for a prefix and suffix... so you can do:
validation_errors('<p class="text-warning">', '</p>');
This is really basic PHP. Can someone tell me why this does not work and what I need to do to make it work.
<?php
$test_var=12;
proc_scrn($test_var);
proc_scrn($local_pid)
{
echo "tp12",$local_pid ;
}
?>
Well, you haven't actually created a function there. This would work:
<?php
$test_var=12;
proc_scrn($test_var);
function proc_scrn($local_pid='')
{
echo "tp12: ".$local_pid;
}
?>
function proc_scrn($local_pid)
{
// something
}
PHP- User-defined functions
Pretty simple
<?php
$var=1;
function proc_scrn($var1){
echo "tp12: ".$var1;
}
proc_scrn($var);
?>
i have created below function in a file info.php to debug variable & data during page load
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".__METHOD__;
echo "<br/>LINE:".__LINE__;
}
}
}
now i call this method from another page index.php, where i inculded info.php
in this file i want to debug POST array, so i write below code
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch($postarray,'POST ARRAY', TRUE);
}
everything is working fine but method and LINE appears below
METHOD:Info::watch();
LINE:17 // ( where this code is written in Info class)
but i wantbelow to display
METHOD: Testpost::gtPostdata()
LINE:5( where this function is called in Testpost class)
so how do i do that if i put$more=TRUE in watch() then method and line number should be diaply from the class where it is called.
can i use self:: or parent::in watch method?? or something else
please suggest me how to call magic constants from other classes or is there any other method to debug varaibale?? ( please dont suggest to use Xdebug or any other tools)
You have to use the debug_backtrace() php function.
You also have below the solution to your problem. Enjoy! :)
<?php
class Info {
static function watch($what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
$backtrace = debug_backtrace();
if (isset($backtrace[1]))
{
echo "<br/>METHOD:".$backtrace[1]['function'];
echo "<br/>LINE:".$backtrace[1]['line'];
}
}
}
}
You can not use those constants from that scope. Check out the function debug_backtrace() instead. If it gives you too much info, try to parse it.
debug_bactrace is the only way you could totally automate this, but it's a "heavy-duty" function.... very slow to execute, and needs parsing to extract the required information. It might seem cumbersome, but a far better solution is to pass the necessary information to your Info::watch method:
class Info {
static function watch($whereClass,$whereLine,$what,$msg='',$more=FALSE)
{
echo "<hr/>";
echo "<br/>".$msg.":";
if( is_array($what) || is_object($what) )
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
else{
echo $what;
}
if($more)
{
echo "<br/>METHOD:".$whereClass;
echo "<br/>LINE:".$whereLine;
}
}
}
now i call this method from another page index.php, where i inculded info.php
class Testpost {
__construct() { // some basic intializtion }
function getPostdata($postarray) {
$postarray=$_POST;
Info::watch(__METHOD__,__LINE__,$postarray,'POST ARRAY', TRUE);
}