2012-07-14 33 views
-1

我想从PHP中的html表格中生成无关的表格单元格。 请参阅本例的表格末尾:http://leobee.com/android/push/so/stdt3.php **注意:我还没有更新在IE中工作的代码,请使用壁虎浏览器。编程删除​​从PHP生成的HTML表格

我在jQuery中找到了这个例子:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tr_deletecell。然而,我需要在php中生成colspan单元格后删除多余的单元格。

问题:

  1. 是否有可能为PHP操作DOM删除表格单元格的合并单元格的细胞已被处理之后?如果可能的话,你可以规定确定这一点的过程,或者我应该研究哪些方法或库。

  2. 您是否知道另一个与我的表类似的例子,它可以按小时编程创建按日计划的日程安排,可以将事件扩展超过一个小时?

代码:

<?php 

// events array 
$events = array(
    array('Atari', 'Hall D' , '10:00 PM'), 
    array('Sonic the Hedgehog', 'Panel 4' , '11:00 AM'), 
    array('Bleach', 'Video 3' , '4:00 PM'), 
    array('Sailor Moon ', 'Panel 4' , '6:00 PM') 
); 

$events_flat = array(); 

foreach($events as $event) 
{ 
    $events_flat[$event[0]] = $event[1] . $event[2]; 
} 

// location array 
$locations = array(
    'Arena', 'Hall D', 'Video 1', 'Video 2', 'Video 3', 
    'Video 4', 'Video 5', 'Video 6', 'HD Theater', 'Panel 1', 
    'Panel 2', 'Panel 3', 'Panel 4', 'WorkShop 1', 'WorkShop 2', 
    'WorkShop 3', 'WorkShop 4', 'Autograph 1', 'Autograph 2' 
); 

// event start time array 
$times = array(
    '9:00 AM', '10:00 AM', '11:00 AM','12:00 PM', '1:00 PM', '2:00 PM', 
    '3:00 PM', '4:00 PM', '5:00 PM', '6:00 PM', '7:00 PM', 
    '8:00 PM', '9:00 PM', '10:00 PM', '11:00 PM', '12:00 AM', 
    '1:00 AM', '2:00 AM' 
); 

$html = '<table><tr><td bgcolor="green"><table name="schedule" id="schedule" border="1" bordercolor="black"><tr><td></td>'; 

foreach ($times as $time) 
{ 
    $html .= '<td width="100" height="25" bgcolor="yellow">'; 
    $html .= htmlspecialchars($time); 
    $html .= '</td>'; 
} 

foreach ($locations as $location) 
{ 
    $html .= '<tr><td width="100" height="25" bgcolor="pink">'; 
    $html .= htmlspecialchars($location); 
    $html .= '</td>'; 

    foreach ($times as $time) 
    { 


     $event = array_search($location . $time, $events_flat); 

     if ($event === FALSE) 
     { 
      $html .= '<td width="100" height="25" bgcolor="#70DBDB">'; 
      $html .= ' '; 


     } 
     else 
     { 
      //http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tr_deletecell 
      //http://www.php.net/manual/en/function.array-pop.php 
      //duration in hours 
      // Todo detect ie and use colSpan 
      $duration =3; 
      $html .= '<td colspan="'.$duration.'" width="100" height="25" bgcolor="orange">'; 
      $html .= htmlspecialchars($event); 
      //$event = array_pop($event-1); 


     } 

     $html .= '</td>'; 
     // $deletecell=document.getElementById("schedule").rows[this]; 
     // $deletecell.deleteCell(-1); 
    } 

    $html .= ' </tr>'; 
} 

$html .= '</table></td></tr></table>'; 

echo $html; 

?> 
+0

这是从用户的角度来看的目的是什么?你只是想在页面渲染后隐藏某些记录吗? – 2012-07-14 16:34:44

+4

为什么不修改您的PHP代码以生成正确的输出,而不是试图在之后将其删除? – 2012-07-14 16:37:38

+0

您还需要了解serverside(php)和clientside(dom,javascript)之间的区别。如果你不想让事情发送到浏览器,请确保它们不是在服务器上创建的 – freefaller 2012-07-14 16:37:57

回答

1

我想你应该考虑固定的输出,而不是故意制造错误的,然后试图事后纠正。

而不是使用foreach的,你可以只使用一个普通for循环与用来抓住每个$time在数组索引i递增变量i。那么如果你发现一个事件,你可以增加i额外的时间,从而减少<td>个细胞。

else 
{ 
    $duration = 3; 
    $html .= '<td colspan="'.$duration.'" width="100" height="25" bgcolor="orange">'; 
    $html .= htmlspecialchars($event); 
    $i += $duraton - 1 // subtract 1 since $i will be incremented after this iteration 
} 

基本上,是由3个时隙,而不是通常1发现一个事件时,由于每个事件占用3个时隙增加时间。请注意,这可以扩展到任何持续时间的事件。

作为一个方面说明,我不知道PHP,所以我的语法可能是错的。如果是这样,请纠正我。尽管如此,这应该足以给出总体思路。

0

我将尝试一种不同的方法来实现我的问题中使用“水平堆积条形图”链接中的图表。