-2

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

mathetes
  • 11,766
  • 7
  • 25
  • 32
  • So are you looking to just script modifying the file as maintenance development task, or are you talking as part of the site functionality? – prodigitalson Feb 27 '13 at 02:42
  • 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 – mathetes Feb 27 '13 at 02:56
  • What have you tried? Please post your code. It is not our job to write your code. – Michael Irigoyen Feb 27 '13 at 03:21
  • I'm sorry, I didn't have any good code working. I was trying to work with the DOM but I figured out this was better, and turns out that prodigitalson solution was what I needed, not what I wrote in the question – mathetes Feb 27 '13 at 13:01

2 Answers2

0

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>
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • I have just one question though, what does the first line of _cheduleTable.php do? It gives me an error about a ":" not expected. I just deleted that line to see what happend and it worked... – mathetes Feb 27 '13 at 13:18
  • And one last thing, I swear! After some changes are made to the table via jQuery, I would like to save it in the _scheduleTable.php file. Can you give me any direction to do this? My idea is to start a script after a "Save changes" button is clicked – mathetes Feb 27 '13 at 13:28
  • @mathetes: You cant save directly... witht he code i gave you it just handles display. You would need to create an "edit" view that contains a form. That form would submit the data to the server which would then be validated and saved with PHP. You wouldnt save it directly in the php file though - you would use a data store like a data file (CSV or JSON for example) or a database. That should really be a new question though and i woudl urge you to read up on using a database (using PDO) or a datafile to store data before you post. – prodigitalson Feb 27 '13 at 13:48
  • @mathetes: The first line checks if the variable we are expecting exists and then determines the css class based on the value of that variable. And there is a typo in it... I have updated my answer to correct it. – prodigitalson Feb 27 '13 at 13:51
  • Wow, I thought this would be easier! Thanks a ton for your help. Now at least I know which direction I should follow – mathetes Feb 27 '13 at 14:15
0

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);
?>
apoq
  • 1,454
  • 13
  • 14