[英]Change values in array by forms values send
I have 2 arrays. 我有2个数组。
The first array has the table names for a simple DB in text. 第一个数组具有文本形式的简单数据库的表名。
The second array has the values of each table. 第二个数组具有每个表的值。
When I launch my form the field value send from form and has the equal value of tables array, take the value send from the form. 当我启动表单时,从表单发送的字段值具有与表数组相等的值,请从表单发送值。
The Script : 剧本 :
<?php
if($_POST[send]=="ok") {
/// The structure it´s that and fixed , in the array_1 and 2 ///
$tables="name,phone,alias";
$values="Jhon,55543232,johny25";
/// Explode values in each case ///
$exp_tables=explode(",",$tables);
$exp_values=explode(",",$values);
/// Array for get values for each field ///
$i=0;
foreach($exp_tables as $exp_table) { ${$exp_table}[]="".$exp_values[$i].""; $i++; }
/// Bucle for get the result if vars send by form equal to the other vars and change by new value send form the form ///
foreach($exp_tables as $table) {
foreach($_POST as $key=>$value) {
if($table=="".$key."")
{
print "".$_POST[$table]."";
}
else { print "".${$table}{0}."";}
}
}
}
?>
The html form html表格
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit2" value"send" />
<input type="hidden" name="send" value="ok" />
</form>
In the form I have "name" in the first field and in the array tables I have one field also called name. 在表单中,我在第一个字段中具有“名称”,在数组表中,我具有一个也称为名称的字段。
When I send the form I must get this: 当我发送表格时,我必须得到这个:
Jhon(change value by the value from the form),55543232,johny25
The Problem it´s repeat all time values and no get the results ok. 问题是,它会重复所有时间值,但无法获得正确的结果。
My question is: How can I fix this for put the values I send from form and change when the other values has the same name as in the array tables, but no works very well. 我的问题是:当其他值与数组表中的名称相同,但无法很好地工作时,如何解决此问题,以便放置从表单发送的值并进行更改。
First of all, your variables $tables
an $values
are no arrays but strings. 首先,您的变量$tables
和$values
不是数组而是字符串。
An array looks like 数组看起来像
array(value, value, value)
or 要么
array( key=> value, key=> value, key value)
In your case you could use 您可以使用
$tables=array('name','phone','alias');
$values=array('Jhon','55543232','johny25');
//or, to make even simpler
$values=array('name'=>'Jhon','phone'=>'55543232','alias'=>'johny25');
Then, if you only want to output the name and check if the name is different from that in the array, you simply do: 然后,如果只想输出名称并检查名称是否与数组中的名称不同,则只需执行以下操作:
if($_POST['name']==$values['name']){
print "".$_POST['name']."";
}
else{
print "".$values['name']."";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.