用两个html文件之间的PHP交换元素

时间:2013-02-27 02:39:39

标签: php html copy-and-swap

我有一个问题,我正在尝试用PHP解决,但我在php编程方面是一个总的菜鸟 问题是,我正在使用jquery修改一个html表,接下来我要做的是将这个表与另一个html文件中另一个精确的表(某些单元格的类的exept)交换

让我们说第一个文件名为scheduleAdmin.html,并且我要将表“转移”到另一个文件(位于同一目录中),名为schedule.html。这两个文件都有一个id ='schedule'的表。

我确信这对于php来说是一项简单的任务,但我的确没有取得多大进展。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

  

功能的一部分。这样做的目的是让老师更新她的日程安排。她进入此页面进行更改,这些更改反映在主页中,供潜在学生查看

这很简单......基本上你只需要输出数据,然后使用稍微不同的标记。因此,您需要做的第一件事是拆分容器和内容,然后重命名文件,以便您可以:

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>

然后在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>

现在我们需要创建include_partial函数,该函数将使用'partial'(一个html片段)的名称和一个命名变量数组在'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();
}

现在我们有了这个集合,我们只需要创建部分文件_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>

答案 1 :(得分:0)

也许这会起作用。

在你的第一个档案中:

<?php ob_start(); ?>

    <table id="schedule">
    ...

<?php ob_end_flush();?>

<?php 
    $contents = ob_get_contents();
    file_put_contents('schedule.html',$contents);
?>