CI template - element is displayed in wrong place - php

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');

Related

How can i write a Template of HTML and create that HTML file with custom usernames as files_names when the API endpoints are hit in PHP?

So basically i need that when user registers in my Android App i will make a POST request with user Name and company Name that they wanted and i will create a html page with that data in the HTML Template. and webpage will be visible like this -> www.main-website.com/username [main-website.com is the domain that i have bought]
So what I did , is that I created a variable that stores the HTML Template with custom changes by adding php variables in-between.And then created a file.html. Check this code {and also If you know a better way to do this thing pls suggest [main idea -> userAuthenticate -> webpage created with username and company name as tags (this is protoype) ]
<!DOCTYPE HTML>
<?php
if(isset($_POST['userName']) && isset($_POST['companyName'])){
$user_name = $_POST['userName'];
$company_name = $_POST['companyName'];
}
$my_var = <<<EOD
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>template</title>
</head>
<body>
<h1>This is $user_name </h1>
<h2>My company name is $company_name </h2>
</body>
</html>
EOD;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.php","wb");
fwrite($fp,$my_var);
fclose($fp);
?>

How to add code to <head></head> in HTML with PHP and then display it?

I want to create a file (index.php) that will dynamically add the below code to the head of another file called (index2.php) and then display the page with the modified code.
<link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script>
I know the way to do this without modifying the code is
include 'index2.php';
However I do not know how to add the code to the head section of index2.php
How would I go about doing this?
Maybe it's just:
index2.php:
...
<head>
<?php require("./index.php"); ?>
</head>
...
?
Put index2.php file's head tag code in a diffrent file. and then include that php file in index.php file's tag
like
index.php
include_once('siffrent_file_name.php');
....
After doing some pondering I felt like I didn't want to parse the entire HTML document, so I added a "key" to index2.php and used this code:
$file = "index2.php";
$key = "<!-- KEY -->";
$appcode = 'link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script';
$index = fopen($file, "r") or die("Unable to open index symlink");
$code= fread($index,filesize("index2.php"));
fclose($index);
$index_app = (preg_replace ($key, $appcode, $code));
echo($index_app);

Return HTML string from php function and mail it using phpMailer

What i want to accomplish is:
call a function from a file
that function returns me a variable with the correct html template
pass the return of this function to another function and use it as body in phpMailer.
I have a page request.php where i call getEmailTemplate() from ManageEmailtemplates.php to get the correct html template and store it to variable: $body=getEmailtemplate();
function getEmailTemplate(){
$template='<html><body> html data </body> </html>';
return $template;
}
then i want to pass this variable to another file cls_mailHandler.php and use it as body to email.
function NewRequestMail($template){
.
.
.
.
.
$mail->Body = $template;
$mail->isHTML(true);
}
The problem is that when i try this way my emails sent i can see them on my mobile but on mail client on browser nothing show up(gmail, hotmail...)
If i change this $body=getEmailtemplate(); to this $body="'".getEmailtemplate()."'"; then in hotmail shows up html content with the ' at the beginning and at the end. But gmail nothing.
Is there any specific way that i have to pass html data inside variable and use them?
thank you
Try this format please, I checked it and it works
<!DOCTYPE html>
<head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <title>Jetbull Affiliates </title>
<meta name='viewport' content='initial-scale=1'>
<meta http-equiv='Content-Type' content='text/html;'>
</head>
<body>
<!-- Content here -->
</body>
</html>

Ajax save function to server doesn't work correctly

I'm working on a website that let's users simply save a note, to be accessed later. I'm however having quite some trouble with actually saving the note. I first had it set to automatically save on each keypress, but it's better if I have the user press on a button to save the file. What you'll also see in the code is that the note gets saved as the users IP address, when the user then visits the site again he'll see the same note (if he has the same IP again).
The error I get now when clicking the save button is:
PHP Warning: file_put_contents() [<a href='function.file-put-contents'>function.file-put-contents</a>]: Filename cannot be empty in /home/martmart/public_html/index.php on line 41
My index.php:
<?php
$note_name = 'note.txt';
$uniqueNotePerIP = true;
if($uniqueNotePerIP){
// Use the user's IP as the name of the note.
// This is useful when you have many people
// using the app simultaneously.
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$note_name = 'notes/'.$_SERVER['HTTP_X_FORWARDED_FOR'].'.txt';
}
else{
$note_name = 'notes/'.$_SERVER['REMOTE_ADDR'].'.txt';
}
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
// This is an AJAX request
if(isset($_POST['note'])){
// Write the file to disk
file_put_contents($note_name, $_POST['note']);
echo '{"saved":1}';
}
exit;
}
$note_content = 'Write something here :D';
if(file_exists($note_name) ){
$note_content = htmlspecialchars( file_get_contents($note_name) );
}
function saveNow() {
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
echo '{"saved":1}';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Marty Testweb</title>
<!-- Our stylesheet -->
<link rel="stylesheet" href="assets/css/styles.css" />
<!-- A custom google handwriting font -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Courgette" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="assets/audiojs/audio.min.js"></script>
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
</head>
<body>
<div id="pad">
<h2>Note</h2>
<textarea id="note"><?php echo $note_content ?></textarea>
</div>
<!-- Initialise scripts. -->
<script>
function saveNow()
{
alert("<?php saveNow(); ?>");
}
</script>
<button id="save" onclick="saveNow()">Save Note</button>
<!-- JavaScript includes. -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="assets/js/script.js"></script>
</body>
<div id="footer">
<footer>
<a href="">
<div id="footer_right">
Version 0.1.2
</div id="footer_right">
</a>
<audio src="assets/audiojs/music.mp3" preload="auto"></audio>
<div id="footer_left">
Save function not working yet
</div id="footer_left">
</footer>
</div id="footer">
</html>
The script.js:
$(function(){
var note = $('#note');
var saveTimer,
lineHeight = parseInt(note.css('line-height')),
minHeight = parseInt(note.css('min-height')),
lastHeight = minHeight,
newHeight = 0,
newLines = 0;
var countLinesRegex = new RegExp('\n','g');
// The input event is triggered on key press-es,
// cut/paste and even on undo/redo.
note.on('input',function(e){
// Count the number of new lines
newLines = note.val().match(countLinesRegex);
if(!newLines){
newLines = [];
}
// Increase the height of the note (if needed)
newHeight = Math.max((newLines.length + 1)*lineHeight, minHeight);
// This will increase/decrease the height only once per change
if(newHeight != lastHeight){
note.height(newHeight);
lastHeight = newHeight;
}
}).trigger('input'); // This line will resize the note on page load
function ajaxSaveNote(){
// Trigger an AJAX POST request to save the note
$.post('index.php', { 'note' : note.val() });
}
});
I don't really know how to solve this, so any help is greatly appreciated. I just want to have the file save with the same name as the user's IP address, when he click on the button. Please keep in mind I'm still a big newbie with these more advanced features so please point out anything I did wrong (but also please explain it easy :) ).
Thanks for reading,
Mart.
First of all I would suggest considering whether having the filename be the IP address is a good idea...many workplaces and other group settings share the same IP address, so any user at a workplace like that would see the note left by any other user at the same workplace.
As to your error, I think the problem may be that you didn't declare $note_name as a global variable within your function. Try changing it to this:
function saveNow() {
global $note_name;
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
echo '{"saved":1}';
}
In PHP if you want to use a global variable (one that wasn't declared inside a function or class) within a function, you always have to use the global keyword as shown above. Using global variables can be avoided entirely if you structure your code a bit differently, but that's another topic.
I wonder why you didn't get a notice about it not being defined though...while you're still developing you might want to put this at the top of your code:
error_reporting(E_ALL);
P.S. Although your code will work without doing this, for security reasons it would be good to specify the JSON MIME type before outputting JSON from PHP, e.g.:
function saveNow() {
global $note_name;
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
header('Content-type: application/json');
echo '{"saved":1}';
exit;
}

Layout and View, what's the difference? As an example of Zend_Layout and Zend_View

I cannot understand what is the difference between Zend_Layout and Zend_View.
Here is a picture from Zend_Layout Tutorial.
Everything seems easy to understand. We have elements in <head>, we have Header, Navigation segment, Content segment, Sidebar and Footer. And it's easy to understand how they are called. But I can't see the difference between View and Layout. Why Navigation segment is called as Layout's property and Footer and Header are called as View property?
I tested Zend_Layout and interchanged them. I called the Navigation segment not as the Layout's property but as the View's property:
echo $this->layout()->nav; // as in tutorial
echo $this->nav; // used to call like this
And everything works fine. Not only for $nav, but for any variable. So what's the difference?
I attach my experiment code here.
My experiment layout page consists of three main blocks:
header (html-code of header),
content (html-code of content block)
footer (html-код footer)
Here's a script of template:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div header>
<?php echo $this->header ?> // it works
<?php echo $this->layout()->header ?> // and this variant also works
</div>
<div content>
<?php echo $this->content ?> // same thing, it is a View's property
<?php echo $this->layout()->content ?> // And it is a Layout's property
</div>
<div footer>
<?php echo $this->footer ?> // same thing
<?php echo $this->layout->footer() ?> // both work (one or another I mean)
</div>
</body>
</html>
My code now:
$layout = Zend_Layout::startMvc(); // instantiate Layout
$layout->setLayoutPath('\TestPage\views'); // set the path where my layouts live
// And here's the most interesting
// Set Header layout first
$layout->setLayout('header'); // 'header.php' - is my file with html-code of the Header
// I pass only name 'header', and it makes 'header.php' from it.
// Predefined suffix is 'phtml' but I changed it to 'php'
$layout->getView()->button = "Button"; // assign some variable in the Header. Please pay attention, it is View's property
$layout->button_2 = "Button_2"; // and also I can assign this way. It's Layout's property now. And they both work
$headerBlock = $layout->render(); // render my Header and store it in variable
// the same procedures for the Content block
$layout->setLayout('content');
$layout->getView()->one = "One";
$layout->two = "Two";
$contentBlock = $layout->render(); // render and store in the variable
// and the same for the Footer
$layout->setLayout('footer');
$layout->getView()->foot = "Foot";
$layout->foot_2 = "Foot_2";
$footerBlock = $layout->render(); // render and store in the variable
// and finally last stage - render whole layout and echo it
$lout->setLayout('main_template');
$layout->getView()->header = $headerBlock; // again, I can do also $layout->header
$lout->content = $contentBlock;
$lout->getView()->footer = $footerBlock;
echo $lout->render(); // render and echo now.
And everything works fine, the page is displayed without errors. But I don't know whether I use Zend_Layout and Zend_View right way or wrong way. Is it the right way to construct the page using Zend_Layout as I do it? What's the difference between
echo $this->layout()->header // this
echo $this->header // and this
Which variant is right one?
Also it seems I have double rendering: at first I render each segment. And then I render them again when I render my final template. Is this the right way?
See the answer I posted to your other (similar) question for some background. Zend_View is for rendering templates. Zend_Layout is a special type of template that contains one or more other templates. You should only ever have one layout on a page. The idea is that the layout contains the portions of the HTML that don't really change between pages.
Your code can be simplified to:
The main layout file:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $this->render('_header.phtml')?>
<div content>
<?php echo $this->layout()->content ?>
</div>
<?php echo $this->render('_footer.phtml)?>
</body>
</html>
Usage:
$layout = new Zend_Layout();
$layout->setLayoutPath('\TestPage\views');
$layout->setLayout('layout'); // this should be whatever you named your layout file
$view = new Zend_View();
$layout->content = $view->render('main_template.php'); // this is the part that changes between pages
echo $layout->render();
Don't call startMvc() if you are not using the Zend Framework controller classes (which you don't appear to be).

Categories