im making a php site where every page has a title, its keywords and description. The thing is that i would like to have a separate file, call it tags.php and have all the tags defined there so i can change them anytime without having to edit every single page. I have a vague idea on how to do it but im pretty much stuck.
Lets say i have for the moment 2 pages, index.php and schedule.php (or index.php?s=schedule) so i want to define the following tags for each one of them. The first being title, the second the keywords and the 3rd the description.
PHP Code:
$index= array("title"=>"Clinic Name", "keywords"=>"clinic, medical", "desc"=>"A good clinic for you");
the template is the same for all pages so the idea is to have dynamically replaced the tags with the ones corresponding to that page. $page is the name of the page which i automatically get with $page = $_GET['s'];
So this is the basic idea but i really dont know pretty much where to start. Any ideas please?
it works, but i just feel like it could be done in a much practical way. Otherwise i have to use the words "title", "keywords" and "desc" so many times in the code which is redundant. I would like to compact it into something like this:
PHP Code:
$index = array("Clinic Name", "clinic, medical", "A good clinic for you");
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks. But then, there is no way to abbreviate and avoid the repeated usage of the words "title", "keywords" and "description" like the example i gave in my second post? i believe we can get a much shorter code and that would be more likeable.
it works, but i just feel like it could be done in a much practical way. Otherwise i have to use the words "title", "keywords" and "desc" so many times in the code which is redundant. I would like to compact it into something like this:
PHP Code:
$index = array("Clinic Name", "clinic, medical", "A good clinic for you");
so i dont have to repeat the same words. Anybody got any ideas?
Thank you.
While it may not be practical for this project...this is a good candidate for storing the information in a database. Then you can remove all the array building code and put in a quick SQL statement for the page you are on.
I guess I didn't see that part about repeating words, but generally speaking I tend to err on the side of verbosity in my source code, as that makes it much easier to maintain. A little copy-and-pasting can make it easy to duplicate as needed.
If you don't want to go with a DB solution (a perfectly valid suggestion, mind you), you could look into setting up a parse_ini_file() solution, perhaps, where the ini file might be something like:
Code:
[index]
title = "index"
description = "This is the home page"
keywords[] = "keyword 1"
keywords[] = "keyword 2"
[schedule]
title = "Schedule Page"
description = "This is the scheduling page"
keywords[] = "foo"
keywords[] = "bar"
PS: Another popular option these days would be to set up a JSON file and use json_decode() to populate an array.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.
I cant help to think that something in this format wuld look great:
PHP Code:
$index = array("Clinic Name", "clinic, medical", "A good clinic for you");
but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.
Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.
I cant help to think that something in this format wuld look great:
PHP Code:
$index = array("Clinic Name", "clinic, medical", "A good clinic for you");
but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.
If you stored them in a database you'd have something like:
PHP Code:
$title = "";//get this however you like...
//connect to db
//select database
$sql = "SELECT title, keyword, description FROM table_name LEFT JOIN tag_table on table_name.foreign_key = tag_table.foreign_key WHERE title='$title'"; //don't forget to sanitize if you are using GET or POST
$result = mysql_query($sql);
if(!$result) die mysql_error();
while($row = mysql_fetch_array($result)){
//this is where you do whatever with your tags for the specified title
}
Thanks. I didnt know about that. Sounds interesting but seems like you have to repeat again the words "title", "keywords" and "description" for each array just like in the other codes, which is i would like to avoid just for the sake of having a much shorter code.
I cant help to think that something in this format wuld look great:
PHP Code:
$index = array("Clinic Name", "clinic, medical", "A good clinic for you");
but i fail to figure out how to have the code understand that the first value of the array is for title, the second for keywords and the 3rd for description without having to write those every time.
At the risk of repeating myself, doing something simply to make the code shorter is seldom much of a reason for doing so. But, if you feel it is important, then you would just have to know that, for example, $array[0] is one type of thing, $array[1] another, and so forth. That then means you have to be careful about staying 100% consistent with your ordering, never leaving out an element, and you probably should add a fairly verbose comment to your code explaining what you're doing -- which makes simply using the more verbose code in the first place seem, perhaps, less verbose?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks again. The usage of a database seems like a good a idea but the site doesnt have one so unfortunately i cant use that option.
One question for Nogdog, according to your example can we say that $index [0] equals Clinic Name while $schedule[0] = Consultation and so on? if so, then i may have figured out a way.
...One question for Nogdog, according to your example can we say that $index [0] equals Clinic Name while $schedule[0] = Consultation and so on?...
Yes, as long as you are totally consistent on how you define each such enumerated array.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
A website I designed with CMS, was all database driven. Pages could be customized by pulling page specific title, description, keywords from the database as well as the content.
Bookmarks