/    Sign up×
Community /Pin to ProfileBookmark

Questions regarding two PHP website structures

I’m in the process of learning PHP, and have two questions regarding website structures.

Most of my websites use the following containers: banner, footer, navigator and content (some are grouped within another general container). When making such websites, I have first made the following webpages: banner.php, navigator.php and footer.php. These three are inserted into index.php using the require() PHP code. If the website includes a main webpage, a picture webpage and a link webpage, I import the banner, navigator and footer webpages into each of them, and create the unique content in the content containers. [B]Is this good practise?[/B]

Second, I have seen some webpages that use an entirely different structure, which may seem more efficient. Instead of navigating from index.php, to pictures.php, to links.php etc, you navigate from index.php?categoryid=1, to index.php?categoryid=2, to index.php?categoryid=3, etc. [B]How is this accomplished?[/B] Can someone direct me to a tutorial on this? I haven’t been able to decipher how its done by looking at the source code for the websites in question. This is an example of what I’m describing: [URL=”http://www.episteme.no/index.php?categoryid=1″]http://www.episteme.no/index.php?categoryid=1[/URL]

Any advice is greatly appreciated.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Kyleva2204Jun 22.2008 — Answer 1: it depends on who you talk to, SSI was made just for that reason, so It really doesn't matter. It may affect load times, but not by enough to be noticed by human.

Answer 2: I asked this same question when I was just learning PHP, took me forever to figure it out.. anyways inside, we called everything else after the " ? " as GET. You continue on the url using & after the ? if you want to add onto it.

For example: mysite.php?page=home&news_item=1

For PHP to respond to this, you can call the array $_GET. to grab the page in the URL just call on $_GET['page'] and it will return "home". same for news_item, just use $_GET['news_item'] and it will return 1

I hope this helps you!
Copy linkTweet thisAlerts:
@YourChildJun 24.2008 — the implementation you're using - with the banner.php, navigator.php, content.php, and footer.php is alright but it can be a bit hectic to visualize the whole page's html from <head></head><body>content</body></html> since everything is disconnected and pieced together page by page...

I haven't seen every type of implementation but when I started off php I learned it as you did..

The implementation I'm using right now is a little nice and easier to follow and it goes somewhat like this:

I have 1 page which I call template.php and it comprises of everything -

<i>
</i>inside template.php:

&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo content(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

--------------------
inside www.mysite.com/photoalbum.php:


include 'template.php'

function content(){
$html = '&lt;table&gt;&lt;tr&gt;&lt;td&gt;...code that generates the photo gallery...&lt;/table&gt;';

<i> </i>return $html;
}




Thats a basic demonstration....this way, if something goes wrong with your html, you can pinpoint it easily because you can see from tops to bottom...

Like I said I dont know how others are implementing their site...I'm sure there are other more neat ways of doing it...

as for the url with the $_GET variables....

say you have a set of links with variables appended to the end, you can grab these variables and do something with each one...For example:


inside "www.mysite.com/store.php"


function content(){

if(empty($_GET['category'])){ //this is when the url is "www.mysite.com", without the appended $_GET variables
echo '
&lt;a href="www.mysite.com/store.php?category=1&gt; category 1&lt;/a&gt;&lt;br/&gt;
&lt;a href="www.mysite.com/store.php?category=2&gt; category 2&lt;/a&gt;&lt;br/&gt;
&lt;a href="www.mysite.com/store.php?category=3&gt; category 3&lt;/a&gt;&lt;br/&gt;';
}else{
$category = $_GET['category'];

<i> </i> if($category == 1){
<i> </i> displayShoes();
<i> </i> }
<i> </i> if($category == 2){
<i> </i> displayDresses();
<i> </i> }
<i> </i> if($category == 3){
<i> </i> displayShirts();
<i> </i> }
}
}

function displayShoes(){
echo //shoes....
}
function displayDresses(){
echo //dresses...
}
function displayShirts(){
echo //shirts...
}

Copy linkTweet thisAlerts:
@IanMartinsauthorJun 28.2008 — Thank you very much for your insight, Kyleva2204 and YourChild!

I've been getting a good grasp on your answers to my second question, but am having trouble implementing it into the page structure that I'm using. Something is missing. When I load the page, the content div is empty.

Here's the scripts that I'm using -- I've made them very short and simple to look over:

index.php:

http://pastebin.com/m58891dfe

navscript.php:

http://pastebin.com/m647e051b

navigator.php:

http://pastebin.com/m738bec3d

Any further advice is greatly appreciated. :-)
Copy linkTweet thisAlerts:
@Kyleva2204Jun 28.2008 — FOr that purpose, instead of using "require()" try

[code=php]echo file_get_contents('$path_to_file')[/code]

Require parses PHP and passes it off to the current document, for what you want to work, you would need to have something like this instead:
[code=php]
<?php
echo <<<END
<ul>
<li>Main menu
<ul>
<li><a href="?menyId=frontpage" id="frontpage">Home</a></li>
<li><a href="?menyId=guestbook" id="guestbook">Guestbook</a></li>
<li><a href="?menyId=contact" id="contact">Contact us</a></li>
</ul>
</li>
<li>Photo gallery
<ul>
<li><a href="?menuId=familyphotos" id="familyphotos">Family</a></li>
<li><a href="?menyId=skatoyphotos" id="skatoyphotos">Sk&#229;t&#248;y</a></li>
</ul>
</li>
</ul>
END;
?>
[/code]


Note how I'm echoing it out, and not just doing HTML. You could also try this, if that doesnt work:

[code=php]
<?php
$my_text = <<<END
<ul>
<li>Main menu
<ul>
<li><a href="?menyId=frontpage" id="frontpage">Home</a></li>
<li><a href="?menyId=guestbook" id="guestbook">Guestbook</a></li>
<li><a href="?menyId=contact" id="contact">Contact us</a></li>
</ul>
</li>
<li>Photo gallery
<ul>
<li><a href="?menuId=familyphotos" id="familyphotos">Family</a></li>
<li><a href="?menyId=skatoyphotos" id="skatoyphotos">Sk&#229;t&#248;y</a></li>
</ul>
</li>
</ul>

END;
?>
[/code]


and to include the file, use this:

[code=php]
<? include('my_file.php'); echo $my_text; ?>
[/code]



I really hope this helps,

Kyle
Copy linkTweet thisAlerts:
@NogDogJun 28.2008 — To save a little typing (and [i]possibly[/i] a couple microseconds), you could replace [b]echo file_get_contents()[/b] with just a [b]readfile()[/b].
×

Success!

Help @IanMartins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...