...

View Full Version : a newbie coder. Need some help



newbie_coder
12-03-2008, 06:34 PM
Hi coders,
I'm a newbie coder and need some help regarding PHP.

I've visited some sites developed with PHP and I saw this code earlier.


Eg:
www.somesite.com/products/productcategory.php?cid=19

www.somesite.com/pg=publication

www.somesite.com/pg=reports

It seems that they're trying to hide the actual page link and show by the means of "pg=page1, page 2, and so on".

What does this code return and what are the advantages of using this way:


....productcategory.php?cid=19

How would I make my site using the code above?

Please help me and I'm getting a hang of PHP :p

Thanks much in advance.

Cheers!

hinch
12-03-2008, 06:53 PM
productcategory.php?cid=19

tells the page to pull a category out of a database with the ID of 19

www.somesite.com/pg=publication

is the equivilent of index.php?pg=publication this is most likely telling a wrapper page(index.php) to load a specific include.

so for example i use home.php?p=home in one of my sites. home.php has a select statement like this that tells it which include page to include in the main site


if (!isset($_GET['p'])) {
include "pages/corphome.php";
} else {
switch ($_GET['p']) {
case "home":
include "pages/corphome.php";
break;
case "mycompany":
include "pages/mycompany.php";
break;
case "myoffers":
include "pages/myoffers.php";
break;
default:
include "pages/corphome.php";
};
}

newbie_coder
12-03-2008, 07:05 PM
Hi hinch,
Thanks a lot for the reply and helping me.

How would I embed this code in my HTML page.

Say I've three links:

About Me
Blog
Portfolio


How would I incorporate the PHP code in my page.

eg: www.mysite.com/pg=about_me

Moreover, do I need to use this code in all my pages?

if (!isset($_GET['p'])) {
include "pages/corphome.php";
} else {
switch ($_GET['p']) {
case "home":
include "pages/corphome.php";
break;
case "mycompany":
include "pages/mycompany.php";
break;
case "myoffers":
include "pages/myoffers.php";
break;
default:
include "pages/corphome.php";
};
}

Thanks again.

hinch
12-03-2008, 09:48 PM
right your 3 links would be something like this

pg=aboutme
pg=blog
pg=portfolio

you would have an index.php file that contained this



if (!isset($_GET['pg'])) {
include "pages/index.php";
} else {
switch ($_GET['p']) {
case "home":
include "pages/index.php";
break;
case "aboutme":
include "pages/aboutme.php";
break;
case "portfolio":
include "pages/portfolio.php";
break;
case "blog":
include "pages/blog.php";
break;
default:
include "pages/index.php";
};
}


You would then create a pages folder and in there put your 4 pages
index.php, blog.php, aboutme.php, portfolio.php

those files would contain your page code.

however the advantage of using a system like this is that you don't need to put anything between <html> and <body> tags in every page

so your main index.php would be something like


<html>
<head>
<title>blah</title>
</head>
<body>
<?php
if (!isset($_GET['pg'])) {
include "pages/index.php";
} else {
switch ($_GET['p']) {
case "home":
include "pages/index.php";
break;
case "aboutme":
include "pages/aboutme.php";
break;
case "portfolio":
include "pages/portfolio.php";
break;
case "blog":
include "pages/blog.php";
break;
default:
include "pages/index.php";
};
}
?>
</body>
</html>

and the pages within your pages folder contain just the content like. so for example your pages/index.php would be something like this.



<h2>this is an index page</h2>
<p>body content goes here</p>

No need for any of the rest of the rubbish as thats already included on your index.php file.

This method is handy for doing things like location aware menu's, breadcrumb systems, etc and allows you to make one change to the primary index that then propogates across the entire site.

newbie_coder
12-04-2008, 06:16 PM
Hi hinch,
Sorry to bother you again. I worked on procedure and the codes like you had said but it's not working. I'm getting an error 404 page not found.

Here's what I did.

In the root folder I created a file "index.php" which contains this code:


<html>
<head>
<title>blah</title>
</head>
<body>
<?php
if (!isset($_GET['pg'])) {
include "pages/index.php";
} else {
switch ($_GET['p']) {
case "home":
include "pages/index.php";
break;
case "aboutme":
include "pages/aboutme.php";
break;
case "portfolio":
include "pages/portfolio.php";
break;
case "blog":
include "pages/blog.php";
break;
default:
include "pages/index.php";
};
}
?>
</body>
</html>

And I created a folder named "pages" and inside this folder I created "index.php, blog.php, aboutme.php, portfolio.php".

And the "index.php" file inside the folder "pages" contains this code:


<h2>this is an index page</h2>
<p>body content goes here</p>
<a href="pg=aboutme">About Me</a>

Please check this link:
<a href="pg=aboutme">About Me</a>

Is this correct? Cos, when I click on the About Me link I get an error saying "The requested URL was not found on this server"

Sorry to bother you again hinch. Please assist me. I would be grateful to you.

Thank again.

hinch
12-04-2008, 06:22 PM
when you click the link what does it say in the url bar of the browser.

try <a href="?pg=aboutme">About Me</a>

failing that you may have to actually use the full page

<a href="index.php?pg=aboutme">About Me</a>

depends if apache is setup with mod_rewrite and such on the servers you were looking at before could be your web server setup is just a standard one so no rewrite or rules inplace for defaulting the page to index.php

newbie_coder
12-04-2008, 06:57 PM
Thanks a lot hinch. It's working now when I changed this line of code


switch ($_GET['p']) {


to


switch ($_GET['pg']) {


Is it correct or am I not getting something?

Both the methods,


1.
<a href="?pg=aboutme">About Me</a>
2.
<a href="index.php?pg=aboutme">About Me</a>

is working fine.

What's the advantage of using this method?

Moreover, like I said earlier the method


www.somesite.com/products/productcategory.php?cid=19


how would I go about to setup a site using this method? Let's say for example, I'm going to setup a news website and there are many categories, do I need to give manually the "cid" code for the links.

Eg:


<a href="/newscategory.php?cid=1">Local news</a>
<a href="/newscategory.php?cid=2">Weather</a>
<a href="/newscategory.php?cid=6">Media</a>
<a href="/newscategory.php?cid=9">International News</a>


and so on.

Thanks a lot hinch.

Cheers!

hinch
12-04-2008, 07:03 PM
you were right to change to p to pg i must have missed that one :)

for doing something like this you'd have basically a link like

index.php?pg=newscat&cid=

you'd then set your cid to be whatever typicaly this is done with a database lookup table to dynamically generate the link list.

newbie_coder
12-04-2008, 07:11 PM
Hi once again hinch,


you'd then set your cid to be whatever typicaly this is done with a database lookup table to dynamically generate the link list.

Could you please give me a link where I can learn something like database lookup?

So it means I've to fetch the "cid" manually and place them one by one in the link by myself, right?

I hope I'm not driving you nuts :(

Thanks a lot.

hinch
12-04-2008, 07:17 PM
arg now your getting complex.

basically you'd have a table in your mysql database something like

tblNewsCats

In that table you'd have 2 columns.

ID & CatName

Then on your page that generates the links to the categories you'd select the contents of that table, loop through them echoing out the list of categories and your address ould be something like

<a href="index.php?pg=newscat&cid=<?php echo $results[0];?>">?php echo $results[1];?></a>

Then you'd have a page called newscat.php in your pages folder which would then retrieve the cid from the link and do another lookup against teh database table that stores all your news (a 2nd table) and links against thecategory id in that table to pull out the correct information.

I'm really really bad at explaining things though your best bet is to hit google for "php + mysql tutorials" or something along those lines.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum