Php 从数据库中的输入压缩数组值
我使用jQuery添加新输入,table=>tr中的每一行都包含几个输入,我希望在数据库中添加数据araye,如下所示:Php 从数据库中的输入压缩数组值,php,arrays,codeigniter,Php,Arrays,Codeigniter,我使用jQuery添加新输入,table=>tr中的每一行都包含几个输入,我希望在数据库中添加数据araye,如下所示: <table> tr 1: <tr> <input name="you[]" value="Translate"> <input name="you[]" value="Searches"> <input name="you[]" value="Email"> </tr> <br> tr 2:
<table>
tr 1: <tr>
<input name="you[]" value="Translate">
<input name="you[]" value="Searches">
<input name="you[]" value="Email">
</tr>
<br>
tr 2: <tr>
<input name="you[]" value="Phone">
<input name="you[]" value="Chat">
<input name="you[]" value="Business">
</tr>
</table>
tr 1:
tr 2:
“值tr 1是”:翻译&搜索&电子邮件“价值tr 2是”:电话和聊天和业务 我有两个问题:
Q1:如何将“tr 1”和“tr 2”中的所有值数组一起插入数据库表的一行中? Q2:如何通过数据库表的
foreach
仅选择(select*from my_table…
)第一部分值(“tr1='Translate'”和“tr2='Phone')?
我想输出以下内容:Translate&Phone关于第一个问题,您可以使用jQuery serialize()函数 将类添加到不同的字段中会有所帮助
<table>
<tr>
<th>tr 1:</th>
<td><input name="Translate" value="Translate" class="fields_1"></td>
<td><input name="Searches" value="Searches" class="fields_1"></td>
<td><input name="Email" value="Email" class="fields_1"></td>
</tr>
<tr>
<th>tr 2: </th>
<td><input name="Phone" value="Phone" class="fields_2"></td>
<td><input name="Chat" value="Chat" class="fields_2"></td>
<td><input name="Business" value="Business" class="fields_2"></td>
</tr>
</table>
或者var data=$('input').serialize()代码>
请看也许它适合你的问题
A1:
// make data to be fit in your database
$you= $_POST['you']; // or $this->input->post('you') In CodeIgniter
$you_chunk= array_chunk($you, 3);
$datayou= "";
foreach( $you_chunk as $youpart ){
$datayou.= implode(",", $youpart);
$datayou.= "&";
}
$datayou= substr($datayou, 0, -1); // Output: Translate,Searches,Email&Phone,Chat,Business
// Insert Data into database
// Grab data from database
// $getyou= Translate,Searches,Email&Phone,Chat,Business
// Parse Data for grab only first values
$parseyou= explode("&", $getyou);
foreach( $parseyou as $partyou ){
$firstyou= strstr($partyou, ',', true); // As of PHP 5.3.0
echo $firstyou . "<br/>";
}
exit;
A2:
// make data to be fit in your database
$you= $_POST['you']; // or $this->input->post('you') In CodeIgniter
$you_chunk= array_chunk($you, 3);
$datayou= "";
foreach( $you_chunk as $youpart ){
$datayou.= implode(",", $youpart);
$datayou.= "&";
}
$datayou= substr($datayou, 0, -1); // Output: Translate,Searches,Email&Phone,Chat,Business
// Insert Data into database
// Grab data from database
// $getyou= Translate,Searches,Email&Phone,Chat,Business
// Parse Data for grab only first values
$parseyou= explode("&", $getyou);
foreach( $parseyou as $partyou ){
$firstyou= strstr($partyou, ',', true); // As of PHP 5.3.0
echo $firstyou . "<br/>";
}
exit;
//从数据库中获取数据
//$getyou=翻译、搜索、电子邮件和电话、聊天、商务
//分析仅获取第一个值的数据
$parseyou=explode(&,$getyou);
foreach($parseyouas$partyou){
$firstyou=strstrstr($partyou,,',,true);//从PHP5.3.0开始
echo$firstyou.“
”;
}
出口
在jquery中序列化仅在标记内工作,如何使用php在数据库上插入和选择值数组?@Jennifer Anthony只需将输入包装在
标记内,并提供一个表示表行号的id
属性@珍妮花:你为什么要用输入而不使用表单
?你能澄清第二个问题吗?
和
中的文本,而不是
或者
格式不正确。我认为这个问题不够具体,这会引发有关编程语言的问题,而不是要求教授针对此类广泛问题规范的方法。聊天室可能更适合这种调查——我们这里有很多这样的人。