This is very bizarre and I have spent quite a while trying to figure out why the pages I was testing kept refreshing when they weren't supposed to. I finally narrowed it down to when I deleted the print_r($_SESSION['boosters']) code it quit refreshing. When the print_r is there it refreshes. You can test this yourself at http://prayerpond.com/posttest2.php (just keep manually refreshing the page and look at the counter).
Take a look at the counter displayed at the beginning of the page. If it skips a number then it is refreshing the page once it gets to the print_r.
Here's the code for the counter at the beginning of the page:
$_SESSION['counter']++;
echo $_SESSION['counter'];
Here's the rest of the code (I deleted everything else that was unnecessary to recreate the problem):
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/start.php");
// PRE-HEADER PROCESSING
unset($_SESSION['boosters']);
$_SESSION['counter']++;
echo $_SESSION['counter'];
$sql = "SELECT prayers_views_likes.*, prayers.postid, prayers.privacy, prayers.username
FROM prayers_views_likes
LEFT JOIN prayers ON prayers_views_likes.postid = prayers.postid
WHERE prayers_views_likes.type = 'answer'
and prayers.privacy != 'hidden'
and prayers.username != 'hoodleehoo'
and prayers_views_likes.adj_ratio > 0
ORDER BY prayers_views_likes.adj_ratio DESC, prayers_views_likes.views DESC
";
$_SESSION['boosters'] = send_query($sql);
print_r ($_SESSION['boosters']); //DELETE
// END PRE-HEADER PROCESSING
?>
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
</head>
<body>
</body>
</html>
This is driving me NUTS! Anyone know what on earth is causing this?
Try doing:
echo '<pre>' . htmlentities(print_r($_SESSION['boosters'], true)) . '</pre>';
Giving a true second argument to print_r() makes it return the formatted string instead of outputting it directly. Then htmlentities() will encode any HTML syntax in the result -- there's probably some HTML or Javascript that's causing the refresh. I also put it inside <pre> so that the formatting will be retained.
Related
this one may be easy, but seems a problem for my server (or me myself).
I have this piece of code in index.php:
<?php
header('Content-Type: text/html; charset="UTF-8"');
// Some code for generating data to be displayed
foreach ($ObjectArray as $SingleObject) {
print_r($SingleObject->getAllProperties());
}
And it does this:
But I don't want to use header('Content-Type: text/plain; charset="UTF-8"'); - I'd rather include HTML code from my header.htm:
<html>
<head>
<meta charset="UTF-8">
<title>Test Cards</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery-ui.js"></script>
</head>
with my index.php like that:
<?php
include 'view/header.htm';
echo '<body>';
// Some code for generating data to be displayed
foreach ($ObjectArray as $SingleObject) {
print_r($SingleObject->getAllProperties());
echo '</body>';
echo '</html>';
}
Unfortunately, this ain't too good. Charset still is recognized as UTF-8, but the result is far from my expectations:
Please tell me, what is happening and how to handle this kind of problem. Is it a case of combining HTML and PHP (clean PHP does use some fancy styling when HTML ain't present?) or maybe some mistake in my code?
Thanks in advance :)
The formatted look is preserved, because in the first case you have the content-type text/plain, while in the second case it is HTML (text/html).
You can wrap it in <pre></pre> tags to preserve formatting when returning HTML.
<?php
include 'view/header.php';
echo '<body>';
echo '<pre>';
// ...
// your foreach here
// ...
echo '</pre>';
echo '</body>';
I am having problems displaying views with a template.
My template looks like this:
<?php
$this->load->view('includes/header');
if($this->session->userdata('is_loggedin')!=1) //check if logged in
{
$this->load->view('includes/not_loggedin'); //includes this when not logged in
}
else //includes all this when is logged in
{
if(isset($content)) //check if content variable isnt empty
{
$this->load->view($content); //THIS IS MY CONTENT WHIC IS DISPLAYED IN WRONG POS
}
$this->load->view('includes/is_loggedin'); //
}
$this->load->view('includes/footer');
?>
By wrong position, I mean that my form is being displayed in the top lefthand corner outside of the HTML structure. Here is a copy from inspect element window. Notice where the div is located; the head tags are empty; and, the head information is in body.
<html lang="en">
<head></head> //head tags are empty
<body>
<div id="settings">...</div> //this is my loaded div
<meta charset="utf-8">
<title>Sludinājumu lapa</title> //head info outside tags
<link href="/assets/css/style.css" type="text/css" rel="stylesheet">
<div id="wrapper">....</div> //i need settings div inside wrapper div
</body>
</html>
Without loading that view, the HTML structure is fine. Also there is no problems with loading is_loggedin and not_logged into the views.
My header contains :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sludinājumu lapa</title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="wrapper">
And the footer contains:
<foter>
<p>developed by kriss</p>
</foter>
</div> //End of "wrapper" div
</body>
</html>
From the controller I am passing the data like this:
$data['content'] = $this->load->view('vchangeinfo');
$this->load->view('template', $data);
Any ideas why everything is so messed up?
Two ways of doing this:
Codeigniter views
Load it in advance (like you're doing) and pass to the other view
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['content'] = $this->load->view('vchangeinfo', NULL, TRUE);
$this->load->view ('template', $data);
// View
if(isset($content)) //check if content variable isnt empty
{
$this->load->view($content);
// OR don't know wich one works.
// echo $content;
}
Load a view "from within" a view:
<?php
// Controller
$this->load->view('template');
<?php
// Views : /application/views/template.php
$this->view('vchangeinfo');
Am trying to set the path for an image to be changed according to a php variable that gets determined by reading a cookie for window.innerWidth ( which is set in the head of the file)
The problem is that the cookie is not being read correctly on the first render by the browser, i have to refresh the browser x 2 to get the correct image.
Could anybody point me in the right direction please ? is there a method in php to get the cookie to always be read correctly the first time - at present it looks like it is being cached, or something like that.
Thank you
Example here : http://markaprice.co.uk/2012dev/r-img-set/image-test-ht.php
html below:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>
<link rel="stylesheet" type="text/css" href="css/img-styles.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<?php require_once('deliver-images.php'); ?>
<img class="ri" src="<?php echo $imgPath ?>the-landscape.jpg" alt="the landscape">
<img class="ri" src="<?php echo $imgPath ?>the-landscape-b.jpg" alt="the landscape">
php script(deliver-images.php) is below:
<?php
$device_width = 0;
$imgPath='';
// Read the device viewport dimensions
if (isset($_COOKIE['device_dimensions'])) {
$device_width = $_COOKIE['device_dimensions'];
echo($device_width);
}
if ($device_width > 0) {
// Low resolution image
if ($device_width <= 480) {
$imgPath = 'images/mobile/';
}
// Medium resolution image
else if ($device_width <= 1024 && $device_width > 480) {
$imgPath = 'images/tablet/';
}
else {
$imgPath = 'images/desktop/';
}
} else {
$imgPath = 'images/desktop/';
}
?>
This following line of yours sets the cookie.
<script>document.cookie = "device_dimensions=" + window.innerWidth;</script>
And this php file is where you check this cookie to make the decision about from which folder you are going to serve images.
<?php require_once('deliver-images.php'); ?>
Looking at your code it seems like you are thinking it that way.
First your script tag run and cookie got set
And then your PHP who use that cookie set in previous step to do some precessing
But that's wrong PHP is processed first on the server and then that page is sent to browser and your script runs.
Conclusion : First time when you open the page your cookies were not set because the script will run after the php code runs.
I'm trying to echo a PHP tag by doing this:
echo "<?php echo \"test\"; ?>";
The result should be just "test" without quotes, but my code isn't working. What is happening is that nothing is shown on the page, but the source code is "<?php echo "teste"; ?>"
Most of you will want to know why I want to do this. I'm trying to make my own template system; the simplest way is just using file_get_contents and replacing what I want with str_replace and then using echo.
The problem is, that in the template file, I have to have some PHP functions that doesn't work when I echo the page, is there another simple way to do this? Or if you just answer my question will help a lot!
Here is an example of what I am trying to accomplish:
template.tpl:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>[__TITULO__]</title>
</head>
<body >
<p>Nome: [__NOME__] <br />
Email: [__EMAIL__]<br />
<?php
if ($cidade != "") {?>
Cidade: [__CIDADE__]<br />
<?php
}
?>
Telefone: ([__DDD__]) [__TELEFONE__] <br />
Fax:
([__DDDFAX__]) [__FAX__] <br />
Interesse: [__INTERESSE__]<br />
Mensagem:
[__MENSAGEM__] </p>
</body>
</html>
index.php
<?php
$cidade = "Teste";
$file = file_get_contents('template.php');
$file = str_replace("[__TITULO__]","Esse Título é téste!", $file);
$file = str_replace("[__NOME__]","Cárlos", $file);
$file = str_replace("[__EMAIL__]","moura.kadu#gmail.com", $file);
if ($cidade != "") {
$file = str_replace("[__CIDADE__]",$cidade, $file);
}
echo $file;
?>
I can solve all this just not showing the div that has no content. like if i have a template, and in it i have 2 divs:
<div id="content1">[__content1__]</div>
<div id="content2">[__content2__]</div>
if the time that i set the content to replace the template I set the content1 and not set content 2 the div content2 will not show...
Use htmlspecialchars
That will convert the < > to < and >
You are dealing with two sets of source code here that should never be confused - the server code (PHP, which is whatever is in the <?php ?> tags) and the client (or browser) code which includes all HTML tags. The output of the server code is itself code that gets sent to the browser. Here you are in fact successfully echoing a PHP tag, but it is meaningless to the browser, which is why the browser ignores it and doesn't show anything unless you look at the client code that got sent to it.
To implement templates in this style, either they should not have any PHP code, or the resulting string (which you have stored in $file) should itself be executed as though it were PHP, rather than echoing it straight to the client. There are various ways to do this. One is to parse out the PHP tags in the string, echo everything that is not within the PHP tags and run eval() on everything that is.
I'm using Jaipho to display images to a mobile gallery from a custom Wordpress plugin. The wordpress theme that uses the Jaipho gallery is displayed using the WP-mobile-detector plugin.
The problem I am having is when I use php to gather the URLs to the photos to echo out a function to be parsed by javascript. I took the resulting static javascript code from the element inspector of Safari and pasted it into my code, commenting out the php, and it works everywhere. Safari for iOS doesn't seem to like the javascript code generated by the php.
HTML 5
<DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
PHP 5.2.6
Wordpress 3.2.1
When it works:
User Agent set to iPhone on Safari
Static code replaces php-generated code
$imageArray = $case->images_assc_array();
$i = 0;
foreach($imageArray['views'] as $view_name => $view_images) {
$before_img = $view_images['before'];
$after_img = $view_images['after'];
echo "dao.ReadImage($i,'".$before_img->medium_size()."','".$before_img->small_size()."','".ucfirst($view_name)." Before','".$case->description."');";
$i++;
echo "dao.ReadImage($i,'".$after_img->medium_size()."','".$after_img->small_size()."','".ucfirst($view_name)." After','".$case->description."');";
$i++;
}
Expected example generated output:
dao.ReadImage( 0,'/wp-content/uploads/rmgallery_images/medium/408/before-front.jpg','/wp-content/uploads/rmgallery_images/small/408/before-front.jpg','Front Before','38 year old who underwent a tummy tuck.');
dao.ReadImage( 1,'/wp-content/uploads/rmgallery_images/medium/410/after-front.jpg','/wp-content/uploads/rmgallery_images/small/410/after-front.jpg','Front After','38 year old who underwent a tummy tuck.');
dao.ReadImage( 2,'/wp-content/uploads/rmgallery_images/medium/409/before-side.jpg','/wp-content/uploads/rmgallery_images/small/409/before-side.jpg','Side Before','38 year old who underwent a tummy tuck.');
dao.ReadImage( 3,'/wp-content/uploads/rmgallery_images/medium/411/after-side.jpg','/wp-content/uploads/rmgallery_images/small/411/after-side.jpg','Side After','38 year old who underwent a tummy tuck.');
You have some mismatched quotes:
echo dao.ReadImage($i,'".$before...
echo "dao.ReadImage($i,'".$after...
and so on.
Try these:
echo 'dao.ReadImage('.$i.',"'.$before_img->medium_size().'","'.$before_img->small_size().'","'.ucfirst($view_name).' Before","'.$case->description.'");';
echo 'dao.ReadImage('.$i.',"'.$after_img->medium_size().'","'.$after_img->small_size().'","'.ucfirst($view_name).' After","'.$case->description.'");';
Credit to #Marc B and #linuxrules94 for a combined solution:
<?php
$imageArray = $case->images_assc_array();
$i = 0;
foreach($imageArray['views'] as $view_name => $view_images):
$before_img = $view_images['before'];
$after_img = $view_images['after'];
?>
dao.ReadImage(<?=json_encode($i);?>, <?=json_encode($before_img->medium_size());?>,<?=json_encode($before_img->small_size());?>,<?=json_encode(ucfirst($view_name));?> + " Before", <?=json_encode(stripslashes($case->description));?>);
<? $i++; ?>
dao.ReadImage(<?=json_encode($i);?>, <?=json_encode($after_img->medium_size());?>,<?=json_encode($after_img->small_size());?>,<?=json_encode(ucfirst($view_name));?> + " After", <?=json_encode(stripslashes($case->description));?>);
<? $i++;
endforeach; ?>
Thanks, everyone!
How about using heredocs: http://php.net/manual/en/language.types.string.php