I have a piece of code I can't figure out how it actually works.
Its flow is like this, looks very easy
Get information to display for example $info
Display information using $this->set('columnname', $info);
Is that set function built-in in cakephp? columnname can also be page's content/type. this set will display info in the page. Where is that page source I need to see? For example perhaps it is stored somewhere in some view that has something like <span id=x></span> it then may only need to get the id and replace something inside the span.
Yes set is built-in in cakephp.
You set variables from controller to pass it to view, like in your controller
$this->set("your_info", $info);
Then in view you can access it as:
echo "This is the info set from controller:".$your_info;
//or if its an array, do
print_r($your_info);
Did you mean something like that. Hope it helps
Related
I create a view file called "Mainbar" and i want to display some other view files in that, like: slideshow products and ... .
if i add $this->load->view('slideshow'); in Mainbar page the variabales I used in slideshow function wont work. what is the best way to display view files into other view and pass the variables of them too
You can return view contents as data in the form of string.Using
$data= $this->load->view('slideshow', '', TRUE);
Then in another view just echo $data; will display view slideshow.
There is a third optional parameter lets you change the behavior of the method 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:
For more visit Codeigniter Views
I would declare an array that would hold the variables I want to display on the page.
So your code would look something like
$data['page_title'] = 'Why so serious';
$data['page_to_load'] = 'slideshow';
$this->load->view('Mainbar', $data);
Then in the Mainbar.php file you would load $page_to_load and the rest of that variables in your data array.
<?php $this->load->view($page_to_load); ?>
I have cookie date stored in a serialized array which I would like to access via the Blade template.
If a cookie value is set matching the current field name, then I want to show it. I am currently using the following code, but I'm not sure how to access the array value.
{{{ Cookie::has('myBookingDetails') ? Cookie::get('myBookingDetails') : old('name') }}}
The value of the myBookingDetails cookie looks like this:
a:4:{s:4:"name";s:13:"Joe Bloggs";s:5:"email";s:29:"joe#domain.co.uk";s:5:"phone";s:11:"0777777777";s:3:"reg";s:6:"123456";}
How can I access the "name" value via Blade?
The data is serialized. You need to use unserialize() function to get the data.
$data = unserialize(Cookie::get('myBookingDetails'));
$name = $data['name']
Check for existence before use.
Don't know if previous answer solved your enigma, but I could give some help.
I fought with a similar case. Seems that in template - in laravel 5.1 - I couldn't access directly to cookies:
Cookie::get('cookiename') simply returns null
cookie('cookiename') returns a Symfony\Component\HttpFoundation\Cookie, but $cookie->getValue() returns (again) null.
The good old $_COOKIE['cookiename'] returns the right cookie, so you could simply go with unserialize( $_COOKIE['myBookingDetails'] )['name']; !
PS: It should be better to handle with cookies in controller and pass a normal variable to the view!
I am editing a template to try and add some conditional logic to my page.
The page template shows topics related to a user.
I want to add a piece of code which will grab the user name from the page we are viewing and then use that in a string for my conditional statements.
The code I have put together is as follows, but it breaks my page so I am doing something wrong.
<?php global
// I query the ID and try and set that to the $userID - I think I am doing this wrong, but when I echo the ID it gets the correct info.
$userID = get_queried_object()->ID;
// This is the string I create using the userID which should be from the query above
$memberstatus = get_user_meta($userID,'member_status',true);
?>
later on I use IF statements to use thsi result (which i know work) so i won't post them. My problem is trying to get the above to work.
Any help?
damm, looks like when I remove 'global' from the php it works! I thought global had to be in this...ah well
This might be a simple question but now i started working on GET method so I'm kinda new to it.
I have some images which are given some unique ids:
<imagetag>;
$b is the variable that stores the ID of the image.
Now I want to pass the $b variable value to imageid.php.
The link successfully opened imageid.php in my browser but the URL looks like imageid?id=. It does not display the id in the URL.
I also tried doing this:
echo $_GET['id']; // in my imageid.php
... but it's not printing the id that I just passed to imageid.php using the GET method? Why?
There isn't really enough information in your question to give a full answer, but have you checked that $b is really holding the image ID? Also, are you side "echo $var = $_GET['id'];" wouldn't just echo whether or not $var was assigned to? (i.e. false or true).
Please provide the full code so I can reply with a better answer.
I'm doing a website. There's a pagination, you click on links and they take you to the page you need, the links pass $_GET variable ( a href="?pn=2" ) and that works fine.
However when i add the category links (also contain $_GET variable
(a href="?sort=english") on the same page, which kind of sort the content on the page, and click it, the system simply overrides the url and deletes all the previous $_GET's.
For example, I'm on page 2 (http://website.com/index.php?pn=2)
and then I click this sorting link and what I'm expecting to get is this (http://website.com/index.php?pn=2&sort=english), but what I get is this:
(http://website.com/index.php?sort=english). It simply overrides the previous $_GET, instead of adding to it!
A relative URI consisting of just a query string will replace the entire existing query string. There is no way to write a URL that will add to an existing query. You have to write the complete query string that you want.
You can maintain the existing string by adding it explicitly:
href="?foo=<?php echo htmlspecialchars($_GET['foo']); ?>&bar=123"
Try using this:
$_SERVER['REQUEST_URI'];
On this link you can see examples. And on this link I have uploaded test document where you can try it yourself, it just prints out this line from above.
EDIT: Although this can help you get the current parameters in URL, I think it's not solution for you. Like Quentin said, you will have to write full link manually and maintain each parameter.
You could create a function that will iterate through your $_GET array and create a query string. Then all you would have to do is change your $_GET array and generate this query string.
Pseudocode (slash I don't really know PHP but here's a good example you should be able to follow):
function create_query_string($array) {
$kvps = array();
for ($key in $array) {
array_push($kvps, "$key=$array[$key]");
}
return "?" . implode("&", $kvps);
}
Usage:
$_GET["sort"] = "english";
$query_string = create_query_string($_GET);
You need to maintain the query parameters when you create the new links. The links on the page should be something like this:
Sort by English
The HTTP protocol is stateless -- it doesn't remember the past. You have to remind it of what the previous HTTP parameters were via PHP or other methods (cookies, etc). In your case, you need to remind it what the current page number is, as in the example above.