Jump to content

Need Help / Possible Tutorial


brandinhall

Recommended Posts

Hey guys!I was wondering if someone would be kind enough to help me out. I am not really a programmer or savvy in that type of nature. I know HTML / CSS but really my experience is in the design field. I am using software called osDate.osDate allows you to give your site a title and meta tags from the admin interface, BUT... osDate doesn't support changing your title/meta tags for specific pages, this is a huge problem for me and the SEO of my site! Here's what I need and I'm hoping someone can help me out with. I want to know if someone could write me a quick tutorial or possibly show me how the code would work to do the following: IF we are on page 'page.php' then use 'THIS' as my title site. As an added bonus, I wonder would hard it would be to fetch 'THIS' from.. let's say a text file? or php file where i can define all the titles etc that get pulled into my title/meta tags? (the same theory would be applied in the meta tags/description) if anyone could gimme a hard, I'd greatly appreciate it! Thanks!

Link to comment
Share on other sites

i would make a one file that you can include into all the pages of your site. from there you can determine what page you are on, i.e. home.php, about.php, etc. you can make an associative array to map your pages to tags and whatnot, and then output that on the page so it will show up in the HTML. first thing is look up the SERVER array and find out how to get the page namehttp://www.php.net/m...bles.server.php then learn how to make arrays, so you can hardcode the information for each page, i.e.

$metaInfo = array(  'home'  =>  array(	 'descr'  => 'home desc text',	 'author'  => 'home author text'   ),  'about' => array(	'descr' => 'about desc text',	'author' => 'about author text'  ));

so then you use the current page name as a key to get the value for each meta tag (param as in my example) you want to display, and then you could use those value to plug into some HTML and then save that to a variable so you can output it where you want.

$pageName = //figure out how to get page name and save it here$homeMeta = $metaInfo[$pageName]; $output = '<meta name="description" content="' . $homeMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $homeMeta['author'] . '"/>'; 

Link to comment
Share on other sites

thanks for not being an ###### towards my lack of knowledge! lolI was just reading further to try and understand more of what you said. I came across this http://www.cre8asiteforums.com/forums/index.php?showtopic=4558&st=0 This guy mentions a way that seemed a bit more simple (to me, the noob) haha.

<?php  $tpTitle="Helping you to improve your web site"; $pgHeading="Site-Report.com - Helping you to improve your web site"; $pgDesc="Helping you to improve your web site"; $pgKeywords="site-report"; ?>

So i get that we define "params?" and then he says we can add this to our header.

<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><title><?php echo $tpTitle ?></title><meta name="description" content="<?php echo $pgDesc ?>"></meta><meta name="keywords" content="<?php echo $pgKeywords ?>"></meta></head>

and then we "echo" or print them out in the head. That made alot of sense, I went ahead and made a page called testing.php, when i uploaded it to my server and viewed the page my title/meta tags hadn't changed. Here's my code (that didnt work)

<html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><title><?php echo $tpTitle ?></title><meta name="description" content="<?php echo $pgDesc ?>"></meta><meta name="keywords" content="<?php echo $pgKeywords ?>"></meta></head><body>  <?php$tpTitle="Helping you to improve your web site";$pgHeading="Site-Report.com - Helping you to improve your web site";$pgDesc="Helping you to improve your web site";$pgKeywords="site-report";?> </body></html>

This works perfectly for me if i can get it to work. osDate includes a header file and footer file. The index.php file includes templates like "homepage.tpl" but if i can just define what the page should echo on every page, it'd work perfectly. I used this concept only because it seemed simple to me and yours was difficult, I'm gonna read up some more on what you suggested and try to understand it better.. Do you know why my code didn't "echo" when i viewed it in my browser on my server?

Link to comment
Share on other sites

You have to set the values before printing them

<?php$tpTitle="Helping you to improve your web site";$pgHeading="Site-Report.com - Helping you to improve your web site";$pgDesc="Helping you to improve your web site";$pgKeywords="site-report";?><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><title><?php echo $tpTitle ?></title><meta name="description" content="<?php echo $pgDesc ?>"></meta><meta name="keywords" content="<?php echo $pgKeywords ?>"></meta></head><body></body></html>

Link to comment
Share on other sites

thanks for not being an ###### towards my lack of knowledge! lolI was just reading further to try and understand more of what you said. I came across this http://www.cre8asiteforums.com/forums/index.php?showtopic=4558&st=0 This guy mentions a way that seemed a bit more simple (to me, the noob) haha.
<?php  $tpTitle="Helping you to improve your web site"; $pgHeading="Site-Report.com - Helping you to improve your web site"; $pgDesc="Helping you to improve your web site"; $pgKeywords="site-report"; ?>

This works perfectly for me if i can get it to work. osDate includes a header file and footer file. The index.php file includes templates like "homepage.tpl" but if i can just define what the page should echo on every page, it'd work perfectly. I used this concept only because it seemed simple to me and yours was difficult, I'm gonna read up some more on what you suggested and try to understand it better.. Do you know why my code didn't "echo" when i viewed it in my browser on my server?

that code will work, but a couple of things to keep in mind. you have more than one page, so you will want to store unique meta values based on the page. hence an array of arrays (one array for each page) will keep that information organized and clear for you, and easy to maintain. By getting the page name, you have a dynamic way of accessing each key (page) in the array, and thus pulling out the specific values you will want to use on that page. Putting it all into a single file and including it on the all the pages means you only have to maintain this in one spot, and whenever you make a change, you don't have to update multiple pages. The reason you may not be seeing it is because PHP needs to be run on a webserver. This means you have to have one installed locally, like with MAMP or WAMP depending on your OS, you can test locally, or you can get a free host that supports PHP and upload there to test it out. While the way the other guy showed you isn't technically wrong or bad per se, you didn't mention in that post that this functinality needed to be scalable for multiple pages, which may have changed his answer, which is why I gave you the recommendation that I did.
So i get that we define "params?" and then he says we can add this to our header.
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><title><?php echo $tpTitle ?></title><meta name="description" content="<?php echo $pgDesc ?>"></meta><meta name="keywords" content="<?php echo $pgKeywords ?>"></meta></head>

and then we "echo" or print them out in the head. That made alot of sense, I went ahead and made a page called testing.php, when i uploaded it to my server and viewed the page my title/meta tags hadn't changed. Here's my code (that didnt work)

<html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><title><?php echo $tpTitle ?></title><meta name="description" content="<?php echo $pgDesc ?>"></meta><meta name="keywords" content="<?php echo $pgKeywords ?>"></meta></head><body>   <?php$tpTitle="Helping you to improve your web site";$pgHeading="Site-Report.com - Helping you to improve your web site";$pgDesc="Helping you to improve your web site";$pgKeywords="site-report";?> </body></html>

as Ingolme said, the assignment statements need to come before you actually use the variables.
Link to comment
Share on other sites

Hey, lol! that worked!! How would you go about making this

<?php$tpTitle="Helping you to improve your web site";$pgHeading="Site-Report.com - Helping you to improve your web site";$pgDesc="Helping you to improve your web site";$pgKeywords="site-report";?>

Pull the keywords, desc and titles from a file base on what page we are on?

Link to comment
Share on other sites

Okay based on your information this was my attempt.Heres the code I did:

<?php$pageName = $_SERVER["SCRIPT_FILENAME"];$homeMeta = $metaInfo[$pageName];$metaInfo = array(  'home'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'   ),  'about' => array(	    'descr' => 'about desc text',	    'author' => 'about author text'  ));$output = '<meta name="description" content="' . $homeMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $homeMeta['author'] . '"/>';?><!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><title><?php echo $pageName ?></title><?php echo $metaInfo ?></head><body>test</body></html>

When I test it on my web server I get the output: " Array test " and the title of my page becomes: <title>/home/content/a/d/u/testsite/html/testing2.php</title>here is the HTML Source when viewing the page:

<!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><title>/home/content/a/d/u/adulthustlers/html/testing2.php</title>Array</head><body>test</body></html>

Sorry im so daft "thescientist" lol

Link to comment
Share on other sites

Okay based on your information this was my attempt.Heres the code I did:
<?php$pageName = $_SERVER["SCRIPT_FILENAME"];$homeMeta = $metaInfo[$pageName]; $metaInfo = array(  'home'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'   ),  'about' => array(		'descr' => 'about desc text',		'author' => 'about author text'  ));$output = '<meta name="description" content="' . $homeMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $homeMeta['author'] . '"/>';?> <!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><title><?php echo $pageName ?></title><?php echo $metaInfo ?></head><body>test</body></html>

When I test it on my web server I get the output: " Array test " and the title of my page becomes: <title>/home/content/a/d/u/testsite/html/testing2.php</title>here is the HTML Source when viewing the page:

<!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><title>/home/content/a/d/u/adulthustlers/html/testing2.php</title>Array</head><body>test</body></html>

Sorry im so daft "thescientist" lol

first thing is you need to understand what $output is for. notice i am making a string of the meta tags in HTML, using the dynamic values from the $metaInfo array. You are just outputting the array, not the $output that is being specifically generated for the sake of the page. And that's why you see array. And you're not daft, you're picking it up pretty well considering you said you had limited knowledge of PHP.
Link to comment
Share on other sites

Awesome help guys! This is what I have.

<?php$pageName = basename($_SERVER['PHP_SELF']);$homeMeta = $metaInfo[$pageName]; $metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'   ),  'about' => array(		'descr' => 'about desc text',		'author' => 'about author text'  ));$output = '<meta name="description" content="' . $homeMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $homeMeta['author'] . '"/>';?> <!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><title><?php echo $pageName ?></title><?php echo $output; ?> </head><body>test</body></html>

When I view my HTML source from the webserver I am not correctly printing the meta (BUT) take a look..

<!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><title>/testing2.php</title><meta name="description" content=""/><meta name="author" content=""/></head><body>test</body></html>

The Meta Info is Printed but without the details I'm trying to create for it.Now when I try to use my brain.... This is how I'm understanding the process When I use this:

$pageName = basename($_SERVER['PHP_SELF']);

It tries to pull information up on Which page we are on. When when we use this:

$metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'

We are saying that if we are on page "testing2" (testing2.php is the name of my file) then we will print the values:

		 'descr'  => 'home desc text',		 'author'  => 'home author text'

to the out put

$output = '<meta name="description" content="' . $homeMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $homeMeta['author'] . '"/>';

Now if I understand this correctly... I am going wrong somewhere along the lines of where the Page title is triggering what info to output?Edit: Or Where my page name doesn't sync correctly with the value ive given it to output?

Link to comment
Share on other sites

Actually, I've just realized you are going to have to strip the .php from the filename using substr(basename($_SERVER['PHP_SELF']),0,-4) and the selecting of meta text stored in array is determine by the filename, so it will have to be called home.php or about.php, as theres is no index ref for testing2 from testing2.php. Also as the home page (home.php) is usually the index.php, and you will have to account for this, and use if($pageName == "index"){$pageName = "home";}

Link to comment
Share on other sites

you have to assign the value of the array to $metaInfo before you get the subarray.

 $metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'   ),  'about' => array(		'descr' => 'about desc text',		'author' => 'about author text'  ));$pageMeta = $metaInfo[$pageName];  //I changed the name to $pageMeta because home is too specific,//and the variable name should distinguish it as being dynamic.  make sure to change it in all places in your code.

Pay attention to the order of your variable assignments and when you use them. and now you realize that there are a couple of extra steps to get the name of the page, i.e. without the php. Just keeping you on your toes. I can't give it all away for free ;)

Link to comment
Share on other sites

I thought by doing:

$metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'this is a discription for testing2',		 'author'  => 'I am the author of testing2'   ),

It would tell the output, if my page is called testing2then to output

$output = '<meta name="description" content="' . $pageMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $pageMeta['author'] . '"/>';

Are you saying I need to define a special array somehow?I am reading on w3schools about arrays, perhaps I am missing or my question is to tailored to something specific, please elaborate if you don't mind how the logic and working of this :)

Link to comment
Share on other sites

no you have it right. if your page is called testing2.php then testing2 will work. look at the <title> though, you have a forward slash before testing. that won't match what you have in the array. personally, to get the pagename, I would do this

$pageName = basename($_SERVER['SCRIPT_NAME'], '.php');

http://php.net/manual/en/reserved.variables.server.phphttp://php.net/manual/en/function.basename.php anyway, play around with it, and make sure the <title> in your HTML matches a member in your $metaInfo array, and then you will know you have $pageName right.

Link to comment
Share on other sites

I thought by doing:$metaInfo = array('testing2' => array('descr' => 'this is a discription for testing2','author' => 'I am the author of testing2'),It would tell the output, if my page is called testing2
Yes, it does now!, but I was replying to post #8, before you provided the changed code in post #11.
Link to comment
Share on other sites

Heres what I have:

<?php$pageName = basename($_SERVER['SCRIPT_NAME'], '.php');$pageMeta = $metaInfo[$pageName];$metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'this is a discription for testing2',		 'author'  => 'I am the author of testing2'   ),  'about' => array(	    'descr' => 'about desc text',	    'author' => 'about author text'  ));/*if($pageName == "testing2"){$pageName = "PHP is Fun!";} */$output = '<meta name="description" content="' . $pageMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $pageMeta['author'] . '"/>';?><!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><title><?php echo $metaInfo['testing2']; ?> </title><?php echo $output; ?></head><body>test</body></html>

Any hints?

Link to comment
Share on other sites

 <?php$pageName = basename($_SERVER['SCRIPT_NAME'], '.php');  $metaInfo = array('testing2' => array('descr' => 'this is a discription for testing2','author' => 'I am the author of testing2'),'about' => array('descr' => 'about desc text','author' => 'about author text')); $pageMeta = $metaInfo[$pageName]; $output = '<meta name="description" content="' . $pageMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $pageMeta['author'] . '"/>';?><!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><title><?php echo ucwords($pageName); ?> </title><?php echo $output; ?> </head><body>test</body></html>

Link to comment
Share on other sites

Heres what I have:
<?php$pageName = basename($_SERVER['SCRIPT_NAME'], '.php');$pageMeta = $metaInfo[$pageName]; $metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'this is a discription for testing2',		 'author'  => 'I am the author of testing2'   ),  'about' => array(		'descr' => 'about desc text',		'author' => 'about author text'  ));/*if($pageName == "testing2"){$pageName = "PHP is Fun!";} */$output = '<meta name="description" content="' . $pageMeta['descr'] . '"/>';$output .= '<meta name="author" content="' . $pageMeta['author'] . '"/>';?> <!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><title><?php echo $metaInfo['testing2']; ?> </title><?php echo $output; ?> </head><body>test</body></html>

Any hints?

I already explained it.
you have to assign the value of the array to $metaInfo before you get the subarray.
 $metaInfo = array(  'testing2'  =>  array(		 'descr'  => 'home desc text',		 'author'  => 'home author text'   ),  'about' => array(		'descr' => 'about desc text',		'author' => 'about author text'  ));$pageMeta = $metaInfo[$pageName];  //I changed the name to $pageMeta because home is too specific,//and the variable name should distinguish it as being dynamic.  make sure to change it in all places in your code.

Pay attention to the order of your variable assignments and when you use them.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...