Question

I have a problem I'm trying to solve with php, but I'm a total noob in terms of php programming The thing is, I'm modifying an html table with jquery, and what I want to do next is to swap this table with another exact table (exept for the classes of some cells) in another html file

Lets say the first file is named scheduleAdmin.html, and has the table I want to "transfer" to the other file (wich is in the same directory), named schedule.html. Both files have this table with an id='schedule'.

I'm confident that this is an easy task with php, but I'm really not making much progress.

Any help would be very appreciated

Was it helpful?

Solution

A part of the functionality. The purpose of this is for a teacher to update her schedule. She enters this page to make the changes and those changes reflect in the main page for her potential students to see

This is pretty simple... basically you just need to ouptut the data, and use slightly different markup. So the first thing you need to do split the container and the content and then rename files so you might have:

In scheduleAdmin.php:

<?php include(__DIR__ . '/functions.php'); // include path/to/this/files/folder/functions.php ?>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <!-- your html -->
          <?php // use the special include function to include the partial for the table ?>
          <?php include_partial('_scheduleTable.php', array('mode' => 'admin')); ?>
        <!-- the rest of your html -->
    </body>
</html>

And then in schedule.php:

<?php include(__DIR__ . '/functions.php'); // include /path/to/this/files/folder/functions.php ?>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <!-- your html -->
          <?php // use the special include function to include the partial for the table ?>
          <?php include_partial('_scheduleTable.php', array('mode' => 'admin')); ?>
        <!-- the rest of your html -->
    </body>
</html>

Now we need to create the include_partial function, which will take the name of a 'partial' (an html fragment) and an array of named variables to be available in that 'partial':

// functions.php

/**
 * Directly renders a partial to the screen
 * 
 * @param string $file the filesystem path to the partial
 * @param array $vars variables that should be available to the partial
 */
function include_partial($file, $vars = array()) {
   // let get_partial do all the work - this is just a shortcut to 
   // render it immediately
   echo get_partial($file, $vars);
}

/**
 * Get the contents of a partial as a string
 * 
 * @param string $file the filesystem path to the partial
 * @param array $vars variables that should be available to the partial
 * @return string
 */
function get_partial($file, $vars = array()) {
    // open a buffer
    ob_start();

    // import the array items to local variables
    // ie 'someKey => 'someValue' in the array can be accessed as $someKey
    extract($vars);

    // include the partial file
    include($file);

    // get the contents of the buffer and clean it out
    // then return that
    return ob_get_clean();
}

So now that we have that set we just need to create out partial file _scheduleTable.php:

<?php $classnname = isset($mode) && $mode == 'admin' ? 'the_css_class_for_admin' : 'the_other_css_class'; ?>
<table id="schedule">
  <tr class="<?php echo $classname ?>" >
    <!-- your td's and what not - jsut needed something to show you how to echo the classname -->
  </tr>
</table>

OTHER TIPS

Maybe this'll work.

In your first file:

<?php ob_start(); ?>

    <table id="schedule">
    ...

<?php ob_end_flush();?>

<?php 
    $contents = ob_get_contents();
    file_put_contents('schedule.html',$contents);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top