Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Using Frames with PHP

SHOULD I USE FRAMES? DO THEY WORK WITH PHP?

I'm not a great fan of frames - but they do have their uses. For example, if you wish to select a whole series of items from a hierarchy such as a directory structure, you might choose to use three or four frames:
  A scrollable frame to show the hierarchy
  A frame to show what's currently selected
  A frame to title the page and tell you what to do
  Perhaps a "footer" frame to display copyright information, etc.

Yes, it CAN be done and (with a clear head) it's not very hard. At the bottom of this article, you'll find the source code of an example that navigates through a directory tree. You are welcome to cut and paste and use this example for your own purposes, but remember that it's just a demo and it's up to YOU to ensure that it's suitable for your needs.

AN EXAMPLE OF THE USE OF FRAMES IN PHP

You may run this example via
http://www.wellho.net/demo/framed_dpd.php4
Some interesting things to note:
 - The use of sessions
 - The same script to generate all the frames and the frameset
 - The "name=here" to scroll the left frame correctly
 - The separation of the PHP and HTML as far as possible
 - recursion used in expand_dir()
 - global $tree to share a variable though all recursion levels
 - The "heavy compute" making up the tree done just once

<?php

$making = "whc_outline";
if ($_GET[frame] == "whc_top") $making = "whc_top";
if ($_GET[frame] == "whc_left") $making = "whc_left";
if ($_GET[frame] == "whc_right") $making = "whc_right";
if ($_GET[frame] == "whc_bottom") $making = "whc_bottom";
if ($_GET[done]) $making = "report";

# Demonstration of navigating through a directory hierarcy
# Uses sessions so that several visitors can be selecting at the same time

session_start();

function expand_dir($sd,$nlev) {
 global $tree;
 $contains = opendir($sd);
 while ($current = readdir($contains)) {
  if (ereg('^\.',$current)) continue;
  $full = "$sd/$current";
  if (is_dir($full)) {
   array_push($tree,"$nlev 1 0 $full");
   expand_dir($full,$nlev+1);
  } else {
   array_push($tree,"$nlev 0 0 $full");
  }
 }
 }

# If this is a new session, set up the directory tree with nothing selected

if (! $_SESSION[running]) {
 $source = "../../trainee";
 $tree = array("0 1 1 $source");
 expand_dir($source,1);
 $_SESSION[tree] = $tree;
 $_SESSION[running] = 1;
}

# Deal with any selection / deselection items

if ($making == "whc_outline" or $_GET[trigger]) {
$elat = -1;
if ($_GET[ac] != "") {
for ($k=0; $k<count($_SESSION[tree]); $k++) {
 list($level,$isdir,$shown,$value) = explode(" ",$_SESSION[tree][$k]);
 if ($value == $_GET[el]) {
  $shown = ($_GET[ac] == "ex") ? 1 : 0;
  $_SESSION[tree][$k] = implode(" ",array($level, $isdir, $shown, $value));
  $elat = $k;
  }
 }
}
$_SESSION[elat] = $elat;
}

# Parse data, making up offer tree and selected HTML strings

$showing = 0;
$position = 0; $hvname=0;
$chosen = array();
$offer = array();
foreach ($_SESSION[tree] as $element) {
 $position++;
 $bold = 0;
 list($level,$isdir,$shown,$value) = explode(" ",$element);
 if ($isdir) {
  if ($shown) {
  $showing = 1;
  $flag = "<a href=$PHP_SELF?ac=sh&el=$value&frame=whc_left&trigger=1#here>-</a>";
  } else {
  $flag = "<a href=$PHP_SELF?ac=ex&el=$value&frame=whc_left&trigger=1#here>+</a>";
  $showing = 0;
  }
  if (! $level) $flag = "&nbsp;";
 } else {
  if ($shown) {
  $flag = "<a href=$PHP_SELF?ac=sh&el=$value target=_top>DeSelect</a>";
  array_push($chosen,$value);
  $bold = 1;
  } else {
  $flag = "<a href=$PHP_SELF?ac=ex&el=$value target=_top>Select</a>";
  }
 }
 $prefix = str_repeat("&nbsp;",$level * 2+(2-$isdir));
 $mrs = "";
 if ($isdir or $shown or $showing) {
  # following line need revisiting - when do subdirectories show??
  # if (! ($isdir == 1 and $shown == 0 and $level > 0)) { # Test
  if ($hvname == 0 and $position > $_SESSION[elat] and $_SESSION[elat] >= 0) {
   $value = "<a name=here>$value</a>";
   $mrs = " &lt;------- most recent selection";
   $hvname = 1;
  }
  if ($bold == 0) {
   array_push($offer,"$prefix $flag $value $mrs");
  } else {
   array_push($offer,"$prefix $flag <b>$value</b> $mrs");
   }
  }
  # }
 }

$word = "Currenly";
if ($_GET[done]) {
 $freport = "You have selected ".count($chosen)." item(s)<br><br>";
 foreach ($chosen as $item) {
  $owbig = filesize($item);
  $freport .= "$item which is $owbig bytes in size<br>";
  }
 session_destroy();
 $word = "Finally";
}
##################### Response page ###################
if ($making == "whc_outline") {
?>
<html><head><title>File Selector System USING FRAMES</title></head>
<frameset rows="100,*,100">
 <frame src=<?=$PHP_SELF?>?frame=whc_top name=whc_top>
 <frameset cols="66%,*">
  <frame src=<?=$PHP_SELF?>?frame=whc_left#here name=whc_left>
  <frame src=<?=$PHP_SELF?>?frame=whc_right name=whc_right>
 </frameset>
 <frame src=<?=$PHP_SELF?>?frame=whc_bottom name=whc_bottom>
</frameset>
<?php
##################################################
} elseif ($making == "whc_top") { ?>
<html><head><title>File Selector System - top frame</title></head>
<body>This demonstration lets you navigate through a directory tree, expanding and contracting
directories and selecting files of your choosing. When you select the DONE button, you'll get
a report of all the items selected in more detail.</body>
<?php
##################################################
} elseif ($making == "whc_right") { ?>
<html><head><title>File Selector System - right frame</title></head><body>
<b><?= $word ?> Selected</b><br>
<?php if (count($chosen)) {
     print (implode("<br>",$chosen));
} else {
     print "No selections yet made";
} ?></body>
<?php
##################################################
} elseif ($making == "whc_left") { ?>
<html><head><title>File Selector System - right frame</title></head><body>
<b>Selection Menu</b><br>
<?= implode("<br>",$offer); ?></body>
<?php
##################################################
} elseif ($making == "whc_bottom") { ?>
<html><head><title>File Selector System - bottom frame</title></head><body>
Please select <a href=<?= $PHP_SELF ?>?done=1 target=_top>done</a> or make (further)
choices from the left selector box<br><br>
Example by Well House Consultants, Copyright <?= date("Y") ?>
<br>Want to know how to do this?
<a href=http://www.wellho.net/course/ph.html>Learn Here!</a>
</body>
<?php
##################################################
} else { # result report
?>
<html><head>
<title>Report on your selection</title>
</head><body>
<h2>Here are the examples that you chose</h2>
<?=$freport?><br>
<a href=<?=$PHP_SELF?>>Restart demo</a><br>
<a href=http://www.wellho.net>Our Home Page</a><br><br>
Example by Well House Consultants, Copyright <?= date("Y") ?>
<br>Want to know how to do this?
<a href=http://www.wellho.net/course/ph.html>Learn Here!</a>
</body>
<?php } ?>
</html>


See also PHP Course details

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

PHP - Further Web Page and Network Handling
  [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
  [3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
  [3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
  [3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1515] Keeping staff up to date on hotel room status - (2008-01-22)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1496] PHP / Web 2 logging - (2008-01-06)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1379] Simple page password protection - PHP - (2007-10-04)
  [1355] .php or .html extension? Morally Static Pages - (2007-09-17)
  [1210] PHP header() function - uses and new restrictions - (2007-05-30)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1183] Improving searches - from OR to AND? - (2007-05-11)
  [1114] PHP Image upload script - (2007-03-21)
  [1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [904] Of course I'll tell you by email - (2006-10-25)
  [847] Image maps for navigation - a straightforward example - (2006-08-28)
  [789] Hot answers in PHP - (2006-07-02)
  [767] Finding the language preference of a web site visitor - (2006-06-18)
  [675] Adding PHP tags to an old cgi program - (2006-04-08)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [565] Using PHP to output images, XML, Style sheets, etc - (2006-01-15)
  [542] Morning image, afternoon image - (2005-12-26)
  [537] Daily Image Santafied - (2005-12-22)
  [484] Setting the file name for a downloaded document - (2005-11-03)
  [451] Accessing a page via POST from within a PHP script - (2005-09-26)
  [443] Server side scripting of styles to suit the browser - (2005-09-12)
  [425] Caching an XML feed - (2005-08-26)
  [410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
  [376] What brings people to my web site? - (2005-07-13)
  [372] Time calculation in PHP - (2005-07-08)
  [356] Sudoku helper or sudoku cheat - (2005-06-23)
  [345] Spotting a denial of service attack - (2005-06-12)
  [314] What language is this written in? - (2005-05-17)
  [220] When to use Frames - (2005-02-19)

Web and Intranet - Frames
  [220] When to use Frames - (2005-02-19)

resource index - PHP
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/php-usin ... h-php.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard