[英]How to collect data from dynamic HTML table
I have a page with a HTML table with newsfacts (NIEUWSBERICHT), photos (IMAGE) and ads (RECLAME). 我有一个带有HTML表的页面,其中包含新闻事实(NIEUWSBERICHT),照片(IMAGE)和广告(RECLAME)。 On this page the user can organize these items by adding a pagenumber to them. 用户可以在此页面上通过添加页码来组织这些项目。 After saving the pagenumbers to the db, the items with a pagenumber will be broadcast in an edition of the cablenewspaper (or whatever you call it in English), which is the final goal. 将页码保存到数据库后,具有页码的项目将在CableNewspaper的一个版本(或任何您用英语称呼的版本)中广播,这是最终目标。 The HTML table also shows the foreign keys I need for my queries. HTML表还显示了查询所需要的外键。
There's an image of the HTML table at: HTML表的图像位于:
My problem is: how can I collect the data from the above HTML table after submitting the form? 我的问题是:提交表单后,如何从上述HTML表中收集数据?
I want to get 2 types of queries like (for example; keys correspond with the above picture): 我想获得2种类型的查询(例如;键与上图相对应):
UPDATE NIEUWSBERICHT SET pagenumber = 2 WHERE nieuwsbericht_id = 53
UPDATE NIEUWSBERICHT SET pagenumber = 3 WHERE nieuwsbericht_id = 56
UPDATE NIEUWSBERICHT SET pagenumber = 6 WHERE nieuwsbericht_id = 62
and 和
UPDATE IMAGE SET pagenumber = 7 WHERE image_id = 19
What makes it difficult for me is that the HTML table is build up dynamically (in PHP) and the number of rows is variable. 使我感到困难的是,HTML表是动态构建的(在PHP中),并且行数是可变的。 See listing below. 请参阅下面的清单。 Somehow I think it would be better if I let go of the dynamically building of the table and start using arrays to store all variables in instead of "$cell". 我以某种方式认为,如果我放弃动态创建表并开始使用数组存储所有变量而不是“ $ cell”,那会更好。 Or maybe I can somehow get the PageNo-item in the screen get multiple values, like in the picture the PageNo on the first row gets the values 20 and 8, which I can use in the query. 或者,也许我可以以某种方式使屏幕上的PageNo项获得多个值,例如图片中第一行的PageNo获得值20和8,这些值可以在查询中使用。 Aahhhrrg, I just don't know how to continue anymore. Aahhhrrg,我就是不知道该如何继续。 Anyone please? 有人吗
// Get column names //
$fields_num = mysql_num_fields($result);
echo "<h1>Create edition {$table}</h1>";
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "<td>PageNo</td>";
echo "</tr>\n";
// Show query result //
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "<td> <select name='paginanummer'>
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select></td>";
echo "</tr>\n";
}
Wow, it's become quite a story. 哇,这已经变成了一个故事。 Hope I did it all right. 希望我没事。
give the select
elements name with the id of the row that it represents for example: 提供select
元素名称及其代表的行的ID,例如:
<select name="position_12">
for the row with id 12 on the db 对于数据库上ID为12的行
than when the form is submitted, loop over the data to search for the parameter that start with "position_" 而不是提交表单时,请遍历数据以搜索以“ position_”开头的参数
foreach($_POST as $key=>$value){
if(preg_match("^position\_(\d+)$",$key,$matches) && is_numeric($value)){
$row_id = $matches[1];
echo "insert {$value} into row number {$row_id}";
}
}
where I put the echo
line you have the $row_id
and $value
vars, that you can use to build your query 在我放置echo
行的位置,您具有$row_id
和$value
变量,可用于构建查询
Bingo, it works! 宾果游戏!
Here's the code for populating the HTML-table column "PageNo". 这是用于填充HTML表格列“ PageNo”的代码。 As you can see I gave the select unique names (thanks to Yaron;-)) and I put the keys I need in the select's value, like a string. 如您所见,我给了选择的唯一名称(感谢Yaron ;-)),然后将所需的键放在了字符串的选择值中。
echo "<td> <select name='position_".$rowcounter."'>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 0'></option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 1'>1</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 2'>2</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 3'>3</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 4'>4</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 5'>5</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 6'>6</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 7'>7</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 8'>8</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 9'>9</option>
<option value='$type, $nieuwsbericht_id, $reclame_id, $image_id, 10'>10</option>
</select></td>";
Then after submitting, the ten values are stored in an arrray called $kv. 然后,提交后,这十个值存储在名为$ kv的错误区中。 It goes like this: 它是这样的:
if ($_POST) {
$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = "$value";
etc...
Then I loop through the array to split the values (strings) using splitdata into seperate variables. 然后,我遍历数组以使用splitdata将值(字符串)拆分为单独的变量。 That's what I wanted! 那就是我想要的! It goes like this: 它是这样的:
$maxcount = count($kv) - 1;
for ($teller = 0; $teller <= $maxcount; $teller++) {
$data = $kv[$teller];
$splitdata = explode(',', $data);
$type = $splitdata[0];
$nieuwsbericht_id = $splitdata[1];
$reclame_id = $splitdata[2];
$image_id = $splitdata[3];
$paginanummer = $splitdata[4];
etc.... (now I can create the sql queries I need to update the database tables)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.