Dynamic content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

Dynamic content

Post by chriso »

Is it possible to display an html or php page through another php/html document? For example, lets say I have a php page called index.php. In this page I have a table:

Code: Select all

<table>
<tr>
<td>echo $content;
</tr>
</table>
Now, if a person clicks on link A, then $content = page1.html and the page1.html document is displayed in the table.

However, if a person clicks on link B, then $content = page2.php?key=value and that page and it's processed results are displayed in the table.

If I can do this, could someone write me the code for a simple example of how I would do this?

Thank you.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Page One...

Code: Select all

<body>
<table>
<tr>
<td><? require("page2.php?anyVar=anyValue") ?></td>
</tr>
</table>
</body>
Page Two (page2.php)...

Code: Select all

anything you want to appear in the td.<br>
<? echo("php also get run before it's include in the td") ?><br>
The value of anyVar is <? echo($_GET["anyVar"] ?><br><br>
Hope that helps!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You can define GET vars in the link, and then the script that defines $content can create different content according to the GET vars it receives.

For example:

<a href="index.php?page=articles&aid=12"></a>

..defines two elements in the $_GET superglobal array: $_GET['page'] and $_GET['aid'].

This would tell a script that the user has selected an article with article id = 12.
loco_ola
Forum Newbie
Posts: 15
Joined: Wed Oct 15, 2003 7:10 am

otherwise

Post by loco_ola »

if you dont want to pass variables and have it all in one page you cuold do it like this:

Code: Select all

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="Content-Style-Type" content="text/css">
<script>

function WriteIt(id,nestref,text) {
    if(document.all) {
         document.all('thisdiv').innerHTML = text;
    }
    else if (document.getElementById) {
         document.getElementById('thisdiv').innerHTML=text;
    }
    else if (document.layers) {
        document.layers('thisdiv').innerHTML = Text;       
    }
}
  </script>
<TITLE></TITLE>
</head>
<body bgcolor="#ffffff">
 <TABLE BORDER=1>
    <TR>
      <TD width="128">
       <!--write what you want in the bit "<strong>change your stuff here.</strong>" in the link. -->
		<a href="javascript:WriteIt('thisdiv',null,'<strong>change your stuff here.</strong>')">First Button </a><br>
        <a href="javascript:WriteIt('thisdiv',null,'whatever else.')">Second Button </a></TD>
      <TD width="196">
 <div id="thisdiv" style="position:relative;">
        <table>
          <tr>
            <td>
            Whatever.....</td>
          </tr>
        </table>          
 </div>
      </TD>      
   </TR>
 </TABLE>
<BR>
</body>
</html>
but this is not really worth doing if you can do it how the others suggested as its pretty clunky but does save you changing pages if you were trying to avoid that. you could make it dynamic too but dont forget you are loading unnecessary data.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Example

Post by markl999 »

My 2 cents ;)

Code: Select all

&lt;a href="/index.php?id=1"&gt;Link A&lt;/a&gt;
&lt;a href="/index.php?id=2"&gt;Link B&lt;/a&gt;
&lt;a href="/index.php?id=3"&gt;Link C&lt;/a&gt;
etc..and in index.php do ...

Code: Select all

<?php
if(!isset($_GET['id'])){
    $_GET['id'] = 1; //sets a default page to display
}
if(!file_exists('page'.$_GET['id'].'.html')){
    $_GET['id'] = 1;  //makes sure they don't ask for page456 etc..
}
?>
<table>
    <tr>
        <td><?php require_once 'page'.$_GET['id'].'.html' ?></td>
    </tr>
</table>
If your pages arn't all named page1.html page2.html etc.. then you'de just add an extra GET var as McGruff illustrated. Course i've skipped error checking and other essentials but it should provide an idea and you should be able to modify it to handle php files, different files in different directories etc.. 8O
Post Reply