Need help to improve some code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Need help to improve some code

Post by mikeashfield »

Hi, I've decided to make my own website from the ground-up to showcase the projects I work on in the future as I'm now starting to think I'd like to get into web development as a career. Can anybody suggest any improvements to this code, it's very basic as is, and I'll build upon it heavily in the coming weeks:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}          
$URLpieces=explode('.', basename(curPageURL()));
$PageTitle=str_replace('_',' ',$URLpieces[0]);

//NavPages Array
$navArray=array('Home', 'About Me', 'Portfolio', 'Contact');
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Lang" content="en">
<title><?php echo "Mike Ashfield - ".$PageTitle; ?></title>
<link rel="stylesheet" type="text/css" href="glabal.css">
</head>
<body>
<h1>Home</h1><br>
<ul>
 <?php
    foreach($navArray as $navItem) {
        echo "<li><a href='htp://192.168.0.222/".str_replace(' ','_',$navItem).".php'>".$navItem."</a></li>";
    }
?>
</ul>
</body>
</html>
shaunak1234
Forum Newbie
Posts: 13
Joined: Thu Nov 03, 2011 12:24 am

Re: Need help to improve some code

Post by shaunak1234 »

What suggestion do you want everything looks fine!
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Need help to improve some code

Post by maxx99 »

I'd start with some template engine. Smarty maybe?

For your solution you could maybe:

Code: Select all

echo "<li><a href='http://192.168.0.222/".str_replace(' ','_',$navItem).".php'>".$navItem."</a></li>";
Move 192.168.... to config file.
Are you sure that it will be http? maybe another config entry.

Code: Select all

$navArray=array('Home', 'About Me', 'Portfolio', 'Contact');
That could also be a part of config. With additional parameter e.g. if it should be visible in nav menu?

URL rewrite would be also nice :)
http://192...222/home
http://192...222/about_me
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Need help to improve some code

Post by mikeashfield »

maxx99 wrote:I'd start with some template engine. Smarty maybe?

For your solution you could maybe:

Code: Select all

echo "<li><a href='http://192.168.0.222/".str_replace(' ','_',$navItem).".php'>".$navItem."</a></li>";
Move 192.168.... to config file.
Are you sure that it will be http? maybe another config entry.

Code: Select all

$navArray=array('Home', 'About Me', 'Portfolio', 'Contact');
That could also be a part of config. With additional parameter e.g. if it should be visible in nav menu?

URL rewrite would be also nice :)
http://192...222/home
http://192...222/about_me
Thanks for your suggestions. I'm not aware of using a config file yet, but I'm guessing you just mean including common code in all pages in the site in .php files and using include_once() to call them? Right?
As for using a template system, I'd rather not. I'm still getting to grips with learning PHp, and feel that I'd like to learn what it's got to offer me (to as full an extent as I can) for now and then move on to template engines and frameworks after that. :)
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Need help to improve some code

Post by maxx99 »

For config I prefer INI (more static) or XML (flexible edited from e.g. admin panel)
config.ini

Code: Select all

[db]
adapter = MYSQL
host = 127.0.0.1
username = root
password = 
dbname = test
charset = utf8
port = 3306
Nearly always im in Zend Framework environment so only thing i need is:

Code: Select all

 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV); 
Which i keep in registry for later :)

About frameworks & template engines
Zend Framework or Symfony can be really overwhelming for start... but check out Smarty. It's quite easy :)
http://www.smarty.net/sampleapp1
Post Reply