Google Answers Logo
View Question
 
Q: <?php include ("file.php"); ?> , file in cel , another question ( Answered,   0 Comments )
Question  
Subject: <?php include ("file.php"); ?> , file in cel , another question
Category: Computers > Programming
Asked by: johnjri1-ga
List Price: $4.00
Posted: 05 Aug 2005 07:32 PDT
Expires: 04 Sep 2005 07:32 PDT
Question ID: 552059
PHP, <?php include ("file.php"); ?> , file in cel 

I need to limit the results in <?php include ("file.php"); ?> ..........

I just got a google answer which I am very satisfied with regarding a
php file.  I had a second thought after I asked it.  Let me first show
the link and copy/paste the answer to set up the new question:

---------------------------------------------------------------------------------------------------------------
http://answers.google.com/answers/threadview?id=551958#a

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

--------------------------------------------------------------------------------------------------------------

Some of the menu groups will have as much as 30 + items.  I want to be
able to insert a variable in the code to limit the number of results
that I can change when needed.  For example, if a menu group has 30
items I want to be able to choose say 10 (or whatever) items to
include starting from the top. (items 11-30 will not be included). 
How do I do this?

Clarification of Question by johnjri1-ga on 05 Aug 2005 15:57 PDT
I'm a little disapointed the question was locked for so long and no
answer.  If it makes it easier I have the ability to rapidly insert a
unique number/ code in front of each menu item, that is not a problem.
 So maybe instead of $menu_group = 3; type of answer,,, it could
somehow tell it to print group 3, items 1-10.  Each group would have
to sart over at one (1) however.  Maybe this makes it easier.  I hope
this makes sense.
Answer  
Subject: Re: <?php include ("file.php"); ?> , file in cel , another question
Answered By: hammer-ga on 11 Aug 2005 05:14 PDT
 
Johnjr1,

You can do this with an array. The revised page1.html below has a new
variable: $limit. This limits the number of menu choices. To limit by
page, put the $limit variable in each page. To create a limit that
affect all your pages, put the variable in menu.php. The revised
menu.php below puts the requested menu choices into an array, then
loops through the array, emitting only the number of choices you allow
with $limit.

page1.html
<HTML>
<HEAD>
</HEAD>
<BODY>

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

</BODY>
</HTML>

menu.php
<?php
$links = array();
$cnt = 0;

if($menu_group == 1)
{
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site1</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site2</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site3</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site4</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site5</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 1
wordsbasically describing a page or site6</a><br>\n";
}
else if($menu_group == 2)
{
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 2
wordsbasically describing a page or site</a><br>\n";
}
else if($menu_group == 3)
{
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
        $links[] = "<a href=\"http://www.somplace.com\">Menu 3
wordsbasically describing a page or site</a><br>\n";
}
if(isset($limit))
{
        while($cnt < $limit)
        {
                print($links[$cnt]);
                $cnt++;
        }
}
else
{
        foreach($links as $link)
        {
                print($link);
        }
}
?>

To see a working example:
http://www.hammerdata.com/Google/GA551958/page1.html

- Hammer

Clarification of Answer by hammer-ga on 11 Aug 2005 05:15 PDT
BTW, if $limit is not set, then the PHP code emits all the menu choices.

- Hammer
Comments  
There are no comments at this time.

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