Jump to content

Newbie In Php Programming


prinzzchyz

Recommended Posts

Hi

 

I am newbie in PHP Programming or in any programming languages

but Im willing to learn in PHP Programming

 

My first project for myself is to create a simple website with a MENU Navigation like Home, About US, Contacts US and News.

 

 

here is my CODE:

 

<html>

 

<head>

 

<title>My First Website</title>

 

<style type="text/css">

body {

font-family: Verdana;

font-size: 12px;

alignment-adjust: middle;

}

 

a {

color: #000;

margin-right: 10px;

}

 

#menu {

font-size: 10px;

border-bottom: 1px solid #000;

margin-left: auto;

margin-right: auto;

width: 650px;

padding: 5px;

}

 

</style>

 

<div id="header"><h1>My First Website</h1>

</div>

<div id="menu">

<a href="index.php">Home</a>

<a href="index.php?p=aboutus">About us</a>

<a href="index.php?p=contacus">Contact us</a>

<a href="index.php?p=news">News</a>

</div>

 

<div id="content">

 

<?php

$pages_dir = 'pages';

 

if (!empty($_GET['p'])) {

$pages = scandir($pages_dir, 0);

unset($pages[0], $pages[1]);

 

$p = $_GET['p'];

 

if (in_array($p.'.php', $pages)) {

include ($pages_dir.'/'.$p.'.php');

} else {

echo 'Sorry, Page has not found.';

 

}

} else {

include($pages_dir.'/index.php');

}

?>

 

 

</div>

</body>

</head>

</html>

 

I just want to insert images in every menu navigation or enchance it not just only a text.

 

 

 

Thanks in advance

Link to comment
Share on other sites

I believe what you are looking for is just html?

<a href="index.php"><img src="/path/to/image"></a>

 

Hi thanks for your reply

 

I tried this

 

<div id="header"><h1>Website</h1>

</div>

<div id="menu">

<a href="index.php"><img src="/phptest/div2_test/1.png>"Home</a>

<a href="index.php?p=aboutus">About us</a>

<a href="index.php?p=contacus">Contact us</a>

<a href="index.php?p=news">News</a>

</div>

 

but the output is i cant see image

 

see attached file

 

 

thanks

post-134064-0-17417200-1354076844_thumb.png

Link to comment
Share on other sites

Hi

 

this is now my new code I also insert height and width.

The problem that i noticed is that the proper path code so i used this ../ then it works

 

 

<div id="header"><h1>Website</h1>

</div>

<div id="menu">

<a href="index.php"><img src="../home.png" height="42" width="42"></a>

<a href="index.php?p=aboutus"><img src="../about.jpg" height="42" width="70"></a>

<a href="index.php?p=contacus"><img src="../contacus.jpg" height="42" width="70"></a>

<a href="index.php?p=news">News</a>

</div>

 

 

 

Thank you so much

post-134064-0-23242000-1354079775_thumb.png

Edited by prinzzchyz
Link to comment
Share on other sites

I'm going to give you two very good tips, which will save you a lot of time and headaches later on. ;)

 

First tip is to always put all of the PHP code processing the data at the very top of the file, and only send HTML to the browser once you're done with everything. That way you'll ensure that your scripts are a lot more flexible, and a lot easier to read (and thus maintain) later on.

 

Second tip is to always validate input, and escape output (if possible). This is to make sure that you do not allow someone to attack your site, and make it do stuff you certainly don't want it to do. Things such as XSS (and other HTML injection attacks), SQL injections, remote includes, are but a few of the dangers online.

Just because your site is small and unknown doesn't mean someone won't attack it: Lots of malicious people have automated scans running, so it doesn't matter how big or small your site is. At the best it might just give you a little more time before you're attacked, but you will be attacked.

 

If we gather these two tips, and apply them to your code, it'll look something like this:

<?php

$pages_dir = 'pages';

if (!empty ($_GET['p'])) {
// Strip away everything that's not a letter, number or underscore from the filename, and make it all lower-case.
$page = preg_replace ("/[^a-z0-9_]/", '', strtolower ($_GET['p']));

// Check if the file exists in the pages folder.
$page = $pages_dir.'/'.$page.'.php';
if (is_file ($page)) {
	// It does, then include it. Make sure it adds all HTML output to $output.
	include ($Page);
} else {
	// If the file wasn't found, send a 404 header and add error message to output
	header ("404 Not Found");
	$output = '<h1 class="error">Sorry, Page has not found.</h1>';
}
} else {
// No page specified, show index. Make sure it adds all HTML output to $output.
include ($pages_dir . '/index.php');
}

?>
<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<title>My First Website</title>

<style type="text/css">
body {
font-family: Verdana;
font-size: 12px;
alignment-adjust: middle;
}

a {
color: #000;
margin-right: 10px;
}

#menu {
font-size: 10px;
border-bottom: 1px solid #000;
margin-left: auto;
margin-right: auto;
width: 650px;
padding: 5px;
}
</style>

</head>
<body>

<header>
<h1>My First Website</h1>

<menu>
	<li><a href="index.php">Home</a></li>
	<li><a href="index.php?p=about_us">About us</a></li>
	<li><a href="index.php?p=contact_us">Contact us</a></li>
	<li><a href="index.php?p=news">News</a></li>
</menu>
</header>

<article id="content">
<?php echo $output; ?>
</article>

</body>
</html>

 

I've also made it into HTML5, as you were missing the doctype and opening body tag.

Also notice that I've replaced your scandir () + in_array () validation of the filename. While these two methods are functionally similar, in that they accomplish exactly the same from a usage standpoint, the above is far less resource intensive. Especially if you have a lot of files.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.