I have made 2 views: 'list' and 'maps'
The 'list' view has an exposed filter input field (called title)
I try made a link in the head-section of the list-view (text-format: PHP-code) to the maps-view.
My question is, how can I do this with the GET method?
This code don't work:
maps
That's easy :) you missed the echo:
maps
# ^^^^
edit: there is also a <?= opening tag (instead of <? and <?php) which automatically calls echo (e.g. <?= $_GET['title'] ?>) but some people find them bad. Especially that modern frameworks such as Symfony2 recommend to disable short_open_tags.
Related
I would like to include a language menu on the top of my webpage where you can select between the languages English (en), Spanish (es) and German (de) (More languages might be added in the future). My website consists of the three pages nav.php, index.php and faq.php.
However, I am not sure about the best way to implement the languages on my website. So far I have come up with the following options:
(1) Create every page of the website in a different language and put it in the same folder:
website/pages/nav-en.php
/nav-es.php
/nav-de.php
/index-en.php
/index-es.php
...
(2) Create every page of the website in a different language and create an own folder for each language:
website/en/nav-en.php
/index-en.php
/faq-en.php
website/es/nav-es.php
/index-es.php
/faq-es.php
....
(3) With the options (1) and (2) described above I see the issue that code changes have to be done for each of the pages manually. Therefore, my solution would be to divide the functional code from the text content and loading the text file depending on the language that is selected in the menu:
website/pages/nav.php
/index.php
/faq.php
website/text-content/nav-text-en.php
/nav-text-es.php
/nav-text-de.php
/index-text-en.php
/index-text-es.php
....
Since I am a newbie in webdeveloping I wanted to ask you:
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
(b) Based on your experiences which option would you recommend?
(c) Is there anything else I need to consider?
Thanks for any advice.
You can use gettext for translation. This way you can have one file with structure and text in the same place for all languages, and translate your strings with an editor (like poedit), which could also be done by a translator. The translator would then only see the strings in one language and have to fill in the translated version, without having to deal with the HTML around it.
There is a shortcut to the gettext() function, _(), so you can write something like
<?=_("Linktext in different languages")?>
It's a bit more work to setup once, but after that, adding additional languages will be a lot easier than having different files with all the structure included, which will quickly become a maintenance nightmare.
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
I think using a $_GET param for both language and content is better way. For example,
index.php?page=faq&lang=en
(b) Based on your experiences which option would you recommend?
Using the power of php language is my advice. You may forget about static HTML website architecture.
(c) Is there anything else I need to consider?
If you're using apache, I suggest to use mod_rewrite. This way each page URL looks better:
index.php/en/faq
instead of
index.php?page=faq&lang=en
Well if you google at localization for PHP you will come up with multiple idea's. Personally I would use gettext. This way you can also format the numbers in the correct format.
My experience so far:
It's a nightmare to improve or even maintain a webpage where different views of the same content are based on different files, e.g. your start-page in english, german and spanish.
Therefore, it is a good approach to decouple the HTML-frame (+ CSS) from the language-dependent text. You can do that by implementing your own ideas ... or you are looking for a php framework which provides such functionality. I'm mainly a Java-guy so I don't know such framework. I guess Laravel and Zend Framework 2 could provide localization. Like others mentioned in the thread gettext also looks like a good way to realize localization in your project.
Hope that helps.
To build a multilangage website with easy maintenance, you can do this way :
1. You create ONE file by langage
en.php // english content
es.php // spanish content
...
2. Each file is an array with the same key, that content ALL the content you need to translate :
<?php
// en.php
$translate = array(
"menu_content" => array(
"home" => "Home page",
"nav" => "Nav page",
...
),
"nav_page_content" => array(
"title" => "Welcome to the nav page",
"content" => "In this page you will find...",
...
),
// same for ALL the content
);
3. Now, I see 2 ways you can add the content of your translation file in your website :
3.1 : You use $_SESSION['lang'] variable or some $_GET['lang'] variable if you have the current langage in your URL (eg. http://www.my-page?lang=en) and you load the php file you need at the begin of your PHP script. Then in your HTML you get the content according to your php array (the same key in ALL langage). For example using my example array for the menu :
<nav>
<ul>
<li id="home"> <?= $translate["menu_content"]["home"]; ?> </li>
<li id="nav"> <?= $translate["menu_content"]["nav"]; ?> </li>
...
</ul>
</nav>
And to change langage, you create a list of your langage and on click you change the $_SESSION['lang'] or $_GET['lang'] value and reload your page with the new php langage content.
3.2 : You can do almost the same without reload the page using $.ajax. The idea is almost the same : you have a list of all the langage of your website with a langage attribute for example :
<ul>
<li data-lg='es'>ES</li>
<li data-lg='en'>EN</li>
...
</ul>
On click, you you get this data-lg value and send it to your Ajax function, calling the php file of the choosen langage :
function changeLangue(lg) {
$.ajax({
type: "POST",
url: lg +".php", // eg. es.php, en.php, etc.
dataType: "json", // you will need to add "echo json_encode($translate);" at the end of your php file to send back JSON content
success: function(response){
// Here your change the content of your website without reloading the page, eg with my nav before:
$("#home").html(response.menu['home']);
$("#nav").html(response.menu['nav']);
...
},
error: function(x,e,t){
manageError(x,e);
}
});
With this method, you will need to change your current langage and rebuild your langage selection too but it's not that hard in JS/jQuery.
I don't know if it's the best way to do it to be honnest but it should work (I did it with the second method to test) and it's easy to add a new langage, you will only have to :
Create a new php file yournewlangage.php with the same array than before and translate the content
Add the yournewlangage to your langage list selection
And that's all if you did it right !
An other idea could be to use a Framework for you website. For example, I'm using Silex right now (micro-framework php), it's like Symfony but lighter and you can use the translation component to help you : https://silex.symfony.com/doc/2.0/providers/translation.html
Framework can help you to win some time if you can use them, take some time to use the best one for what you want to do !
Hope it's clear enought and it helps a little
I'm using PlatesPHP on a very basic level with static data rather than extracting it from db.
In my controller I have:
<?php
require 'vendor/autoload.pkp';
echo $templates->render('index', [
'project_1_checklist_point_1_help' => 'Google',
]);
and then in the index.php the following
<p><?=$this->e($project_1_checklist_point_1_help)?></p>
and in the template.php standard html skeleton.
It shows up as
Google
rather than a link, which I want.
Google
I've tried htmlentity() and htmlspecialchars(), but they are not what I was looking for at all.
Any ideas?
Cheers!
If you want the un-escaped string, just echo the variable without the method $this->e():
<p><?= $project_1_checklist_point_1_help ?></p>
The method $this->e() (short for $this->escape()) is equal to htmlspecialchars(), which html-encodes the string.
You can read more in depth about it the manual: http://platesphp.com/templates/escaping/
I've been given a website to maintain. The website is using PRADO framework and I had to do some minor changes. There were no problems with the HTML and CSS changes, but now I have to pass some data from a page to a view.
I've tried using:
$this->HomeCalendar2->DataSource = $result['data'];
$this->HomeCalendar2->dataBind();
but it says Component property 'Home.HomeCalendar2' is not defined.
Above my code there is the following code:
$this->HomeCalendar->DataSource = $result;
$this->HomeCalendar->dataBind();
and it works perfectly fine and I can't see where is the definition of HomeCalendar.
Any help will be appreciated.
P.S: I've never worked with PRADO before.
This question is quite old now, hope you already solved it.
Using DataSource means that there must be in your template (.page or .tpl) a TDataBoundControl component like the folowwing (using TRepeater for example)
<com:TRepeater ID="HomeCalendar">
<!--- [ properties like ItemTemplate here and it contents ] --->
</com:TRepeater>
Subclasses of TDataBoundControl are for component needing to loop results, (sorts of for or foreach somehow).
If you juste need to pass a signle result to the view you can use a component like TPanel/TActivePanel, TLiteral, etc. or simply use the expression tag.
Examples :
Expression tag :
Php :
<?php
$this->myvalue = 'Hello World!';
Template :
<h1><%= $this->myvalue %></h1>
Or another solution:
Template :
<com:TLiteral ID="MyLiteral" />
PHP :
<?php
$this->MyLiteral->getControls()->add('<h1>Hello world !</h1>');
I'm new to PHP and want to apply a specific class to the title of my page depending on what part of the site the viewer is browsing.
For instance, I want to apply the class "blog" to the if the viewer is at domain.com/blog OR domain.com/blog/post-1 so on and so forth BUT apply the class "pics" if they're viewing domain.com/pics or domain.com/pics/gallery-1 etc etc.
I found something that could be modified to serve my needs using javascript here
but I figured seeing as I'm using PHP already, it'd make more sense to keep this sort of thing server side.
As I say, I'm new to PHP. I've experimented with some regular expressions, but to no avail.
EDIT: Sorry not to be more specific in my first post, I am using wordpress as my CMS - this is my first stackoverflow post, I trust you can forgive me :)
<?php
if (substr($_SERVER['REQUEST_URI'], 0,5)=='/pics') {
$h1class='someclass';
}
The it depends on how you're putting the class in the tag, might be like this
?><h1 class="<?php echo $h1class; ?>">...
Rather than putting a class in based on the page, I would suggest having seperate CSS files for home.css, blog.css, whateverelse.css. Of course, these would be in addition to some sort of default.css or site.css or whatever that would contain the styles used across the site.
You can then build a function/method to create the CSS calls in the HTML header. I usually have a "Page" object that builds the actual HTML page, and a "get_css" method that spits out the CSS calls.
It's hard to get more specific without knowing how you currently build pages.
Here is the solution I ended up crafting. Credit to #m.buettner for pointing me towards explode().
<h1 id="title-text" class="
<?php #returns the category as a class
$url = array();
$url = explode('/', get_permalink());
echo $url[3];
?>
mono in-case-404">
SITE
</h1>
I am trying to play with php, I aint very good. But I figured its about time I started playing with it
What I have is say a page called ascr
Its filename is acsr.php
I have created a global php include, so I can play with different vars and include them dynamically.
So I have made a var called $shorttag = "acsr";
On my page I would like to use that shorttag to fire images and classes etc.
So for an image I want to display
img src="/img/<?php $shorttag; ?>.jpg"
and in other places perhaps,
div class="<?php $shorttag; ?>"
Is this the CORRECT way of doing stuff like this ?
//////////////////////////////////////
Example.
say I have folder with image in it called acsr.jpg
On my dynamic page I want to echo that image, but instead of using filename, I want to pull it in based on my php var , in this case its called $shorttag so essentially, $shorttag=acsr , and therefore I can spew out something like:
<?php $shorttag; ?>.jpg
( code above more than likely wrong )
But the result gives me ascr.jpg
You need to echo it so it is actually output.
div class="<?php echo $shorttag; ?>"
img src="/img/<?php echo $shorttag; ?>.jpg"
If PHP is configured to use short_open_tag (which is not usually recommended since it may be turned off on some servers)
img src="/img/<?= $shorttag ?>.jpg"
The rules change for PHP 5.4 and later, where you can use <?= ?> without having to turn on short_open_tag:
This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
Your examples is missing a way to echo out the value. You can either use echo like what Michael suggested, or use the syntax <?= $shorttag ?>.
Additionally, in your examples, you will be creating an image at /img/acsr.jpg and attaching a class called acsr to that div. If that is what you want, then yes, you're on the right track. Otherwise, you will need to explain more of what you wish to accomplish.
You're basically doing what I did with "pid" and "sid" (i.e., http://mysite.com/?pid=main_page&sid=sub_context) right?
You'll have to make sure your pages are protected from user tampering by ensuring your parameter values adhere to a standard (ex., regex replace all but a-zA-Z_- with ""). As for classes, you'd do best to avoid having them rely on a page id for styling. Make your styles global in nature.