用PHP动态创建HTML表
我试图在使用PHP查询数据库时创建一个动态html表。我很难理解如何向它附加两个不同的循环,以确保正确生成我要创建的表用PHP动态创建HTML表,php,jquery,html,codeigniter,Php,Jquery,Html,Codeigniter,我试图在使用PHP查询数据库时创建一个动态html表。我很难理解如何向它附加两个不同的循环,以确保正确生成我要创建的表 <table><tr> <td>$ID and $ITEM_NAME</td> <td>$ID and $ITEM_NAME</td> </tr> <tr> <td>$ID and $ITEM_NAME</td> <td>$
<table><tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr>
<tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr>
<tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>
<table><tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr>
<tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr>
<tr>
<td>$ID and $ITEM_NAME</td>
<td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
$ID和$ITEM\u名称
如您所见,我必须在一个HTML表行中添加两行信息,在第三行中,我需要添加一个并在继续查询的同时创建一个新的HTML表 目前我的PHP代码看起来像这样
foreach($item_ID as $x => $x_value) {
$sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
$query = $this -> db -> query($sql);
foreach($query->result() as $row){
$item_name = $row->item_name;
}
$html_output .= '<tr><td>';
$html_output .= $x_value. " ".$item_name;
$html_output .= '</td>';
//Not sure how to proceed with closing the rows and opening new one on 3rd query item?
}
foreach($x=>x$U值的项目ID){
$sql=“从item_表中选择item_名称,其中id=”$x_值“;
$query=$this->db->query($sql);
foreach($query->result()作为$row){
$item\u name=$row->item\u name;
}
$html_输出。='';
$html_输出。=$x_值。“”$item_名称;
$html_输出。='';
//不确定如何在第三个查询项上关闭行并打开新行?
}
请注意,我正在使用Codeigniter,不过我只是在寻找一些逻辑方面的帮助
如果我能帮你收拾东西,请告诉我
感谢您花时间阅读本文。任何帮助都将不胜感激。首先,只需查询一个问题:
$sql = "SELECT id, item_name FROM item_table WHERE id IN ('" . implode("', '", $item_ID) ."')";
$query = $this -> db -> query($sql);
设置计数器
$i = 1;
然后启动表代码
$html_output .= '<table>';
换言之,听起来您想用每个奇数行创建一个新表。此外,您希望使用HR标记分隔表
$i = 0; //initialize as 0 rows
foreach($item_ID as $x => $x_value) {
$sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
$query = $this -> db -> query($sql);
foreach($query->result() as $row){
$item_name = $row->item_name;
$row_output = '<tr><td>';
$row_output .= $x_value. " ".$item_name;
$row_output .= '</td>';
//presumably you would close the row here too, unless you have more table cells you aren't showing us
$i++;//we have a row, so increment
if ( !($i % 2) ) { // if this has a remainder, then the current row counter is odd
if ( $i != 1 ) {
//beginning of a table that isn't the first table, add the hr
$html_output .= '<hr>';
}
$html_output .= '<table>'; // open table
$html_output .= $row_output;
} else { //this is an even numbered row, the last row of the table
$html_output .= $row_output;
$html_output .= '</table>'; // close table
}
}
}
$i=0//初始化为0行
foreach($x=>x$U值的项目ID){
$sql=“从item_表中选择item_名称,其中id=”$x_值“;
$query=$this->db->query($sql);
foreach($query->result()作为$row){
$item\u name=$row->item\u name;
$row_输出=“”;
$row_输出。=$x_值。“”$item_名称;
$row_输出='';
//假设您也会在这里关闭该行,除非您有更多的表单元格,您不会显示给我们
$i++;//我们有一行,所以递增
如果(!($i%2)){//如果有余数,则当前行计数器为奇数
如果($i!=1){
//在不是第一个表的表的开头,添加hr
$html_输出=“
”;
}
$html\u输出。='';//打开表格
$html\u输出。=$row\u输出;
}else{//这是一个偶数行,表的最后一行
$html\u输出。=$row\u输出;
$html_输出。='';//关闭表格
}
}
}
使用行计数器和模,这样您就有了类似于$html\u输出的东西。=$iRowCounter%2==0?"" : "";代码>。。。作为一个非常粗略的估计嘿,保罗,你的问题有问题吗?语法似乎不正确。我只是注意到了一些其他的东西。我的要求的一部分是,它还显示每行两个项目。上述代码每行输出一项。再次感谢你。我喜欢一个查询的想法!想知道你是否也能帮我满足第二个要求?当然,我误解了你的问题。我编辑了我的答案以反映这一要求。这完美地呈现了。谢谢你的解释!非常感谢。嗨,faerysteel,上面的代码遗漏了我要求的一部分,我必须每行输出2个项目。检查上面的输出html示例。再次感谢您的帮助!请澄清:每两个结果为一行。那么每两行就有一个表。对的为什么要将行拆分为多个表?为什么要在每个单元格中放置多个数据块($ID和$ITEM_NAME),为什么每行放置两个?这似乎是对桌子的滥用。你到底想完成什么?也许有更好的办法。
if ($i % 2 == 1) {
$html_output .= '<td></td></tr>';
}
$html_output .= '</table>';
$i = 0; //initialize as 0 rows
foreach($item_ID as $x => $x_value) {
$sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
$query = $this -> db -> query($sql);
foreach($query->result() as $row){
$item_name = $row->item_name;
$row_output = '<tr><td>';
$row_output .= $x_value. " ".$item_name;
$row_output .= '</td>';
//presumably you would close the row here too, unless you have more table cells you aren't showing us
$i++;//we have a row, so increment
if ( !($i % 2) ) { // if this has a remainder, then the current row counter is odd
if ( $i != 1 ) {
//beginning of a table that isn't the first table, add the hr
$html_output .= '<hr>';
}
$html_output .= '<table>'; // open table
$html_output .= $row_output;
} else { //this is an even numbered row, the last row of the table
$html_output .= $row_output;
$html_output .= '</table>'; // close table
}
}
}