我有一组这样的文本框:
<input type="text" name="tbxt[]" />
<input type="text" name="tbxt[]" />
<input type="text" name="tbxt[]" />
Run Code Online (Sandbox Code Playgroud)
我正在使用
foreach ($_POST['tbxt'] as $tbxt) {
//
}
Run Code Online (Sandbox Code Playgroud)
但是,例如,如果我有:
<input type="text" name="tbxt[]" /><select name="selxt[]"></select>
<input type="text" name="tbxt[]" /><select name="selxt[]"></select>
<input type="text" name="tbxt[]" /><select name="selxt[]"></select>
Run Code Online (Sandbox Code Playgroud)
所以我可以这样写:
foreach (($_POST['tbxt'] as $tbxt) && ($_POST['selxt'] as $selxt)) {
//
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我对邮政编码API有回应.但是,由于两个单词之间的空格,我无法弄清楚如何从'地名'中获取值.不太确定从哪里开始.
object(stdClass)#1 (4) {
["post code"]=>
string(5) "42223"
["country"]=>
string(13) "United States"
["country abbreviation"]=>
string(2) "US"
["places"]=>
array(1) {
[0]=>
object(stdClass)#2 (5) {
["place name"]=>
string(13) "Fort Campbell"
["longitude"]=>
string(8) "-87.5585"
["state"]=>
string(8) "Kentucky"
["state abbreviation"]=>
string(2) "KY"
["latitude"]=>
string(7) "36.5995"
}
}
}
Run Code Online (Sandbox Code Playgroud) 我需要转换我的数组:
$tdata = array(11,3,8,12,5,1,9,13,5,7);
Run Code Online (Sandbox Code Playgroud)
进入这样的字符串:
11-3-8-12-5-1-9-13-5-7
Run Code Online (Sandbox Code Playgroud)
我能够使用它:
$i = 0;
$predata = $tdata[0];
foreach ($tdata as $value)
{
$i++;
if ($i == 10) {break;}
$predata.='-'.$tdata[$i];
}
Run Code Online (Sandbox Code Playgroud)
但是想知道是否有更简单的方法?
我尝试过类似的东西:
$predata = $tdata[0];
foreach ($tdata as $value)
{
if($value !== 0) {
$predata.= '-'.$tdata[$value];
}
}
Run Code Online (Sandbox Code Playgroud)
但它最终导致一堆Undefined Offset错误和错误$predata.
所以我想一劳永逸地学习:
如何从索引1开始循环遍历整个数组(同时排除索引0)?
有没有更好的方法将数组转换为上述方式的字符串?
我有一条消息要使用 alert() 显示
$message = "No result found for the following info:Name: ".$FullName."IC: ".$ID." in database.";
echo "<script>alert('".$message."'); window.history.back();</script>";
Run Code Online (Sandbox Code Playgroud)
这是有效的,但如果我在消息中添加一个新行 '\n'
$message = "No result found for the following info:\nName: ".$FullName."\nIC: ".$ID." in database.";
Run Code Online (Sandbox Code Playgroud)
它不会显示弹出消息。问题是什么?
我正在尝试将我的值绑定到PDO中的预准备语句中.
以下是使用预准备语句块的先决条件代码:
$tab = 'air_user';
$fie = array('USER_NAME', 'USER_PASSWORD' , 'USER_EMAIL');
$name = $_POST['name'];
$pass = $_POST['password'];
$email = $_POST['email'];
$val = array(
'name' => $name,
'pass' => $pass,
'email' => $email
);
$this->connect($tab,$fie,$val);
Run Code Online (Sandbox Code Playgroud)
以下是我准备这些值并进行必要插入的部分:
public function connect($table,$fields,$values)
{
try{
$con = new PDO ('mysql:host=localhost;dbname=air','root','123456');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$fields = implode(", ", $fields);
echo $fields;
$values = implode(", ", $values);
echo $values;
// have to make this prevent sql injection //
$stmt = $con->prepare("INSERT INTO $table(ID,$fields) VALUES (?,?,?,?)");
$stmt->execute(array('',$values)); …Run Code Online (Sandbox Code Playgroud) 我有这些代码:
$arr = array($check);
Run Code Online (Sandbox Code Playgroud)
当我尝试
echo "<pre>";
die(print_r($arr));
Run Code Online (Sandbox Code Playgroud)
它给了我Firebug的结果
<pre>Array
(
[0] => Array
(
[0] => 2
[1] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
现在,我想要一个能让我'2,1'的输出.我怎么能用PHP做到这一点?我需要使用内爆吗?
我有一个from布局,当我按Submit按钮时,此页面上的数据保存到数据库中。我可以在同一页面上打印表单验证错误,但不能在该页面上打印成功消息,可以在另一页面上打印成功消息,如何在同一页面上显示该消息,请帮助。
这是控制器:
function addRoom(){
$data['title'] = 'Add Room';
$data['main_content'] = 'config/addRoom';
$data['roomlists'] = $this->config_mdl->get_room_info();
$this->load->view('_base/layout', $data);
}
function roomAdd(){
$this->form_validation->set_rules('roomType', 'Room Type', 'trim|required|xss_clean');
$this->form_validation->set_rules('roomName', 'Room Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('roomDetails', 'Room Details', 'trim|required|xss_clean');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
if ($this->form_validation->run() == FALSE) {
$this->addRoom();
} else {
$roomdata = array('room_type' => $this->input->post('roomType'),
'room_name' => $this->input->post('roomName'),
'room_details' => $this->input->post('roomDetails'));
$this->config_mdl->roomAdd($roomdata);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
function roomAdd($roomdata)
{
return $this->db->insert('tbl_room_info', $roomdata);
}
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
<div class="panel-body">
<?php echo validation_errors(); ?>
<?php $attributes = array('class' => 'form-horizontal', …Run Code Online (Sandbox Code Playgroud) 我有这个字符串:
$hours = '7, 8, 12';
$hourData = explode(', ', $hours);
Run Code Online (Sandbox Code Playgroud)
如果我var_dump($hourData),它返回:
array(3) {
[0]=>
string(1) "7"
[1]=>
string(2) "8"
[2]=>
string(2) "12"
}
Run Code Online (Sandbox Code Playgroud)
我有这个代码:
<?php for ($i=0; $i <= 23 ; $i++) { ?>
<div class="fl">
<input type="checkbox" name="limit_hour[]" value="<?php echo $i ?>" class="fl limit_hour"
<!-- ======================================================================== -->
<?php echo (isset($hourData[$i]) && $i == $hourData[$i] ? 'checked' : '') ?>>
<!-- ======================================================================== -->
<span class="fl lmBox"><?php echo $i ?>:00 - <?php echo $i ?>:59</span>
</div>
<?php } …Run Code Online (Sandbox Code Playgroud) 关于php,我有一个非常简单的问题.
我有这个字符串:
This is a simple string | badword is here
Run Code Online (Sandbox Code Playgroud)
我需要这个:
The is a simple string
Run Code Online (Sandbox Code Playgroud)
所以,
我在下面使用了以下代码:
$word = substr($word, 0, strpos($word, '|'));
Run Code Online (Sandbox Code Playgroud)
如果我使用该代码,我必须检查|字符串中是否有任何字符,如果是,请删除它.
所以它的速度非常低,我无法使用它.
获取结果的最快方法是什么,而不检查|char是否在主字符串中?
我已尝试在关于此主题的其他问题上发布的每个解决方案,但它们都不起作用.
我很抱歉,如果这是一个基本问题,但我是MySQLi的新手,我无法弄清楚为什么这个连接不起作用.
我的functions.php文件中有一个函数,它具有:
function cleanInput($string) {
$stripsql = $connect->real_escape_string($string);
$addslashes = addslashes($stripsql);
return $addslashes;
}
Run Code Online (Sandbox Code Playgroud)
第二行是在标题中抛出该错误.
我连接到数据库,$connect是一个工作变量.它在config.php中设置使用:
$connect = new mysqli('localhost', 'shop', 'password', 'zen');
Run Code Online (Sandbox Code Playgroud)
我甚至尝试在功能之前移动该线路无济于事.
一var_dump对$connect在functions.php文件不返回NULL,则返回有关数据库开始的数据object(mysqli)#1 (19) { ["affected_rows"]=> int(0),然后用大量的行继续.