• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

PHP Javascript & HTML frames

I have this special requirement.  Using an existing PHP script, a user select a variable number of files for editing, the best presentation depends a lot according to the number of files selected.

With the knowledge I have I could spend days in trial and error with this, I'm sure an efficient script could be rather short, so here is what I need to do, probably using a mixture of PHP/Javascript:

1) build a HTML frameset according to this number (from 2 to 6 frames, frames are not used for other numbers)
2) display in each frame the contents of one selected file (filenames are held in variables in script mentioned above)
3) present these contents in "textarea" for editing and then save if changed

I can probably adapt your solution script to fit the "frameset" but if you need to know, the division of the screen is as follows:
- one bottom line kept for navigation in all cases
- rest of screen divided in 2 columns for 2 files or 3 rows for 3 files or 2rows of 2 columns for 4 files or 2 rows of 3 columns for 5 and 6 files
(If of any use, I have these framesets in working HTML files as 2frams.htm, 3frams.htm, 4frams.htm and 6frams.htm)

Thank you for your help.
  • 2
  • 2
1 Solution
I created a scenario with an <iframe> element instead <framesets> but you can adapt that.

So, you will have two files: one for your main frame (file_edit_main.php) and your editing gallery frame (file_edit_iframe.php). Plus, a lot of files stored in a subdirectory named "myfiles".


  <title>Text Editing Gallery</title>

<form name="myForm" method="post" target="myImageGallery">

      <input name="selectedFiles" type="hidden" value=""/>


$editableFiles = array("file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt",
                  "file6.txt", "file7.txt", "file8.txt", "file9.txt");

for ($i=0; $i<count($editableFiles); $i++) {
      $filename = $editableFiles[$i];
      print '<input id="editableFiles" type="checkbox" value="'.$filename.'">File #'.($i+1).'<br>';


      <button type="button" onclick="editSelectedFiles();">Edit</button>

<script language="javascript">
var theForm = document.forms['myForm'];

function getSelectedFiles()
      var sel = "", obj = null;
      for (var i=0; i<theForm.editableFiles.length; i++) {
            obj = theForm.editableFiles[i];
            if (obj.checked==true) {
                  if (sel!="")
                        sel += ",";
                  sel += obj.value;
      return sel;

function editSelectedFiles()
      window.frames['myImageGallery'].location = "file_edit_iframe.php?selectedFiles="+getSelectedFiles();

<iframe id="myImageGallery" name="myImageGallery" src="file_edit_iframe.php" width="700" height="300"></iframe>


$selectedFiles = $_GET['selectedFiles'];
if ($selectedFiles!="")
    $selectedFiles = explode(",", $selectedFiles);

if (!is_array($selectedFiles))
    die ("No files were selected.");

$basedir = "myfiles/"; //directory where your files are stored
$fileCount = count($selectedFiles);
$maxCols = 3; //max number of columns you wish to display
$colLimit = min($maxCols, ceil(sqrt($fileCount))); //actual column limit
$colCount = 0;

print "<body><table border='0' cellspacing='0' width='90%'>";

for ($i=0; $i<$fileCount; $i++) {
    $filename = $selectedFiles[$i];
    $fileText = file_get_contents($basedir.$filename);

    // if cell goes in left-most column, open table row...
    if ($colCount == 0)
        $cellHtml = "<tr>";
    // ...otherwise, initialize cell's HTML variable
        $cellHtml = "";

    // add cell's own html
    $cellHtml .= "<td align='center'>".$filename."<br>"."<textarea>".$fileText."</textarea></td>\n";

    // if cell goes in right-most column, close table row
    if ($colCount == ($colLimit-1))
        $cellHtml .= "</tr>\n";

    // output cell's complete HTML to browser
    echo $cellHtml;

    // increment column index
    $colCount = ++$colCount % $colLimit;
// if last cell rendered was not in last column, then row is still open, so close it!
if ($colCount != 0)
    echo "</tr>";
// close table
echo "</table></body>";
Note that in file_edit_main.php I hard-coded file names into an array. I don't know where you're actually planning to retrieve them from (a database, a filesystem function that reads from a directory, etc).
rblampainAuthor Commented:
Thanks to lexlythius. I realize now that my definition of what I need is not good at all. I only have a basic understanding of PHP so please correct me if I'm wrong.

It looks like your script divides the screen into 2 windows but the users may want to edit an undefined number of files at the same time,  I failed to specify that.

What I wanted to do is divide the screen in 2 if they want to edit 2 files at the same time, divide the screen in 3 if they want to edit 3 files at the same time, divide the screen in 4.... or 6 which is the maximum that can fit for legibility. If the user wants to edit only one file then the whole file is displayed, if the user wants to edit more than 6 files at the same time then each file is presented in a fixed size "textarea" which will toghether occupy more than the screen space and scroll.

I already have  tested "HTML frameset" for the purpose and what I need is  a PHP script that creates these framesets and displays in the frames the contents of the files to be edited.

The files to be edited are already selected and the full paths are retrieved from an array from a previous PHP script, this script is taylored so that the selected files can have the (linux) permissions changed from read-only to read/write for saving after editing. So this array is to be passed between scripts with the "PHP session".

You deserve the points for your work because my description was confusing.
rblampainAuthor Commented:
I've re-introduced the question under "PHP, javascript & frames"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now