我有一个表格,其中有4种类型的部分(文本,联系人,作品集,推荐书) 我有一个按钮可以添加一个新部分(ADD SECTION)。单击后将打开一个弹出窗口,以选择部分类型(文本,联系人,投资组合,推荐书) 我不知道如何处理此表格的发帖请求
我尝试循环sections数组 但是如果我有两个文本类型部分,总共有5个部分,则对于文本部分,它会打印错误的无效偏移量。
if(isset($_FILES["content"]) || isset($_POST["content"])){
$errors = 0 ;
$this->load->library("form_validation");
$this->form_validation->set_rules("title[]", "Title", "trim|callback_validate_input");
$this->form_validation->set_rules("tagline[]", "Tagline", "trim|callback_validate_input");
for ($i=0; $i < count($_POST["section_type"]); $i++) {
if($_POST["section_type"][$i] == "testimonial"){
$this->form_validation->set_rules("content[testi_name][]", "Testimonial Name", "trim");
$this->form_validation->set_rules("content[testi_heading][]", "Testimonial Heading", "trim");
$this->form_validation->set_rules("content[testi_content][]", "Testimonial", "trim");
}else{
$this->form_validation->set_rules("content[]", "Text Content", "trim|callback_validate_input");
}
}
$this->form_validation->set_rules("status[]", "Status", "trim|required");
$this->form_validation->set_rules("section_type[]", "Section Type", "trim|required");
$this->form_validation->set_rules("section_ids", "Sections IDs", "trim|required");
$this->form_validation->set_rules("priority", "Priority", "trim|required");
$this->form_validation->set_rules("user_id", "User ID", "trim|required|is_natural");
if($this->form_validation->run() == FALSE){
$errors = 1 ;
$response["message"] .= validation_errors("<p class='text-danger'>", "<p><br>");
}
if(isset($_FILES["content"])){
$allowed_types = array("image/png", "image/jpg", "image/gif", "image/jpeg");
$max_size = 153600;
if(isset($_FILES["content"]["name"]["portfolio"])){
//validate portfolio images
$portfolio_path = "./assets/images/portfolio/";
for($b = 0; $b < count($_FILES["content"]["name"]["portfolio"]); $b++){
$file_name = $_FILES["content"]["name"]["portfolio"][$b];
$type = $_FILES["content"]["type"]["portfolio"][$b];
$tmp = $_FILES["content"]["tmp_name"]["portfolio"][$b];
$size = $_FILES["content"]["size"]["portfolio"][$b];
if(!empty($file_name)){
if(!in_array($type, $allowed_types)){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." is not a valid image for portfolio.<p><br>";
}else if($size > $max_size){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." is lager than allowed upload size(150 kb).<p><br>";
}else if(!move_uploaded_file($tmp, $portfolio_path."$file_name")){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." could not be uploaded.<p><br>";
}
}
}
}
if(isset($_FILES["content"]["name"]["testi_img"])){
//validate testimonial images
$portfolio_path = "./assets/images/testimonial/";
for($a = 0; $a < count($_FILES["content"]["name"]["testi_img"]); $a++){
$file_name = $_FILES["content"]["name"]["testi_img"][$a];
$type = $_FILES["content"]["type"]["testi_img"][$a];
$tmp = $_FILES["content"]["tmp_name"]["testi_img"][$a];
$size = $_FILES["content"]["size"]["testi_img"][$a];
if(!empty($file_name)){
if(!in_array($type, $allowed_types)){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." is not a valid image for testimonial.<p><br>";
}else if($size > $max_size){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." is lager than allowed upload size(150 kb).<p><br>";
}else if(!move_uploaded_file($tmp, $portfolio_path."$file_name")){
$errors = 1 ;
$response["message"] .= "<p class='text-danger'> ".$file_name ." could not be uploaded.<p><br>";
}
}
}
}
}
if($errors === 0){
$section_ids = explode(",", $_POST["section_ids"]);
//update / add new sections
$this->load->model("sections_model");
$output = array();
for ($i=0; $i < count($_POST["section_type"]); $i++) {
$user_id = $_POST["user_id"] ;
$section_id = $section_ids[$i];
$priority = $i;
$title = $_POST["title"][$i] ;
$tagline = $_POST["tagline"][$i] ;
$status = $_POST["status"][$i] ;
$section_type = $_POST["section_type"][$i] ;
$section = array(
"section_id" => $section_id,
"user_id" => $user_id,
"section_name" => $title,
"section_tagline" => $tagline,
"section_priority" => $priority,
"section_type" => $section_type,
"status" => $status,
);
if($section_type == "portfolio"){
foreach ($_FILES["content"]["name"]["portfolio"] as $portfolio) {
$section["section_content"][] = $portfolio ;
}
}else if($section_type == "testimonial"){
$count = 0 ;
foreach ($_FILES["content"]["name"]["testi_img"] as $testi_image) {
$section["section_content"][$count] = array(
"image" => $testi_image,
"name" => $_POST["content"]["testi_name"][$count],
"heading" => $_POST["content"]["testi_heading"][$count],
"content" => $_POST["content"]["testi_content"][$count]
);
$count++;
}
}else {
$section["section_content"] = $_POST["content"][$i];
}
$check_section = $this->sections_model->select_all(array(
"where" => array(
"section_id" => $section_id,
"user_id" => $user_id
)
));
$output[$i] = $section;
// if($check_section->num_rows()>0){
// //update query
// if($this->sections_model->update_row(array(
// "section_id" => $section_id,
// "user_id" => $user_id
// ), $section)){
// $response["message"] = "<p class='text-success'>Section Updated Successfully.</p>";
// }else{
// $response["message"] = "<p class='text-danger'>Unknown Error! Try Again Later.</p>";
// }
// }else{
// //insert query
// if($this->sections_model->insert_row($section)){
// $response["message"] = "<p class='text-success'>Section Added Successfully.</p>";
// }else{
// $response["message"] = "<p class='text-danger'>Unknown Error! Try Again Later.</p>";
// }
// }
}
echo "<pre>";
print_r($output);
echo "</pre>";
}
}else{
$response["message"] .= "<p class='text-danger'>direct script not allowed</p><br>";
}
//这就是我得到的回报
遇到PHP错误严重性:注意
消息:未定义的偏移量:3
文件名:admin / Users.php
行号:642
回溯:
文件: E:\ VIKASH \ xampp \ htdocs \ zrurt \ application \ controllers \ admin \ Users.php 行:642函数:_error_handler
文件:E:\ VIKASH \ xampp \ htdocs \ zrurt \ index.php行:315功能: require_once
遇到PHP错误严重性:注意
消息:未定义的偏移量:4
文件名:admin / Users.php
行号:642
回溯:
文件: E:\ VIKASH \ xampp \ htdocs \ zrurt \ application \ controllers \ admin \ Users.php 行:642函数:_error_handler
文件:E:\ VIKASH \ xampp \ htdocs \ zrurt \ index.php行:315功能: require_once
Array
(
[0] => Array
(
[section_id] => 18
[user_id] => 31
[section_name] => portfolio
[section_tagline] => portfolio tagline
[section_priority] => 0
[section_type] => portfolio
[status] => active
[section_content] => Array
(
[0] => user6.png
[1] => user7.png
[2] => user8.png
)
)
[1] => Array
(
[section_id] => 19
[user_id] => 31
[section_name] => testimoinial
[section_tagline] => testimonial tagline
[section_priority] => 1
[section_type] => testimonial
[status] => active
[section_content] => Array
(
[0] => Array
(
[image] => user1.png
[name] => user 1
[heading] => heading one
[content] => testimonial one.
)
[1] => Array
(
[image] => user2.PNG
[name] => user 2
[heading] => user two
[content] => testimonial two.
)
)
)
[2] => Array
(
[section_id] => 20
[user_id] => 31
[section_name] => about us
[section_tagline] => about tagline
[section_priority] => 2
[section_type] => text
[status] => active
[section_content] => contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section. contact section.
)
[3] => Array
(
[section_id] => 21
[user_id] => 31
[section_name] => contact us
[section_tagline] => contact tagline
[section_priority] => 3
[section_type] => text
[status] => active
[section_content] =>
)
[4] => Array
(
[section_id] => 22
[user_id] => 31
[section_name] => contact us
[section_tagline] => contact tagline
[section_priority] => 4
[section_type] => contact
[status] => active
[section_content] =>
)
)
我想验证所有字段并将其保存到数据库中。