Google Answers Logo
View Question
 
Q: PHP, <?php include ("file.php"); ?> , file in cel ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: PHP, <?php include ("file.php"); ?> , file in cel
Category: Computers > Programming
Asked by: johnjri1-ga
List Price: $4.00
Posted: 04 Aug 2005 23:37 PDT
Expires: 03 Sep 2005 23:37 PDT
Question ID: 551958
I know a little HTML and very little about PHP.

I have two types of files:

I have one file, let's call it menu.php (I'm not sure whether to save
it as a .html file or a .php file).   It has say 30 groups of links in
it which are in groups something like this:

<html>
<head>
  <title></title>
</head>
<body>

<!-- group #1--> 
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>


<!-- group #2-->
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>


etc.
etc.

<!-- group #30-->
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>
<a href="http://www.somplace.com">wordsbasically describing a page or site</a><br>


</body>
</html

I have say 50 pages (again, I'm not sure whether to save it as a .html
file or a .php file) that have a cel on the left that will contain the
above menu.  I want to use php to do this (not frames), something like
this I was told:
<td>
   <?php
      include("menu.php");
   ?>
</td>

Like I said, lets say I have 50 pages.  All are going to include this
menu, but here is my question:
On say the 20th, 37th, 45th pages I want to include only the portion
of the "menu" in group 25.  In ten other pages I want to include only
the portion of the menu in group 3. etc etc.  It will always be just
one group.  How do I do this?  I'm guessing there are some tags I need
to add to the menu file, and one unique line of code for each
individual page.
Answer  
Subject: Re: PHP, <?php include ("file.php"); ?> , file in cel
Answered By: hammer-ga on 05 Aug 2005 06:20 PDT
Rated:5 out of 5 stars
 
johnjri1-ga,

You can do this using a variable. Say you have three pages, each of
which needs to show a different menu group. Before you do the include,
set a variable indicating which menu group should appear on this page.
In the samples below, the only thing that changes is the value of the
$menu_group variable.

page1.html looks like this. Note the $menu_group variable.
<HEAD>
</HEAD>
<BODY>

<?php
$menu_group = 1;
include("menu.php");
?>

</BODY>
</HTML>

page2.html looks like this:
<HEAD>
</HEAD>
<BODY>

<?php
$menu_group = 2;
include("menu.php");
?>

</BODY>
</HTML>

page3.html looks like this:
<HEAD>
</HEAD>
<BODY>

<?php
$menu_group = 3;
include("menu.php");
?>

</BODY>
</HTML>

Now that each page knows which menu it wants, menu.php needs to use
that value to decide what to include. By using print statements, you
can print only the appropriate menu group into the page. Note that the
emebedded quote marks are preceded by a backslash. This is necessary
to prevent parsing errors in the code. This tells PHP that you want a
literal quote mark, rather than that the print string is ending.

menu.php:
<?php
if($menu_group == 1)
{
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site</a><br>\n");
}
else if($menu_group == 2)
{
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n");
}
else if($menu_group == 3)
{
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
        print("<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n");
}
?>

To see this work, try the following links:
http://www.hammerdata.com/Google/GA551958/page1.html
http://www.hammerdata.com/Google/GA551958/page2.html
http://www.hammerdata.com/Google/GA551958/page3.html

Good luck with your web project!

- Hammer

Search strategy
----------------
None. Personal experience.

Additional resources
----------------------
PHP Online Manual
http://us3.php.net/manual/en/

Clarification of Answer by hammer-ga on 05 Aug 2005 06:22 PDT
Notes:
1. Lines in menu.php should not be wrapped.

2. If your server does not interpret php in html files, then you will
need to name your files with a .php extension.

- Hammer

Request for Answer Clarification by johnjri1-ga on 05 Aug 2005 07:35 PDT
Thanks Hammer, your solution works perfectly.  Thanks!  I asked a
second question about limiting the results.  The question is in the
same category with an almost identical heading.

Clarification of Answer by hammer-ga on 05 Aug 2005 08:12 PDT
johnjril-ga,

I'm glad the solution worked for you. If I get a chance, I'll look at
your other question. If I don't get to it, another Researcher will
probably be able to help you.

- Hammer
johnjri1-ga rated this answer:5 out of 5 stars
Thanks, It works :)

Comments  
Subject: Re: PHP, <?php include ("file.php"); ?> , file in cel
From: dooga-ga on 30 Nov 2005 20:05 PST
 
Awsome! A great answerer who made an example on a privately hosted
site. Excellent service.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy