I have a form in which there are 4 types of sections (text, contact, portfolio, testimonial) I have one button to add a new section (ADD SECTION). On click opens a pop to select section type(text, contact, portfolio, testimonial) I don't know how can I handle the post request of this form
I have tried looping the sections array but if I have two text type section and there is 5 total section then for text section its prints an error invalid offset.
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>";
}
// this is what I got in return
A PHP Error was encountered Severity: Notice
Message: Undefined offset: 3
Filename: admin/Users.php
Line Number: 642
Backtrace:
File: E:\VIKASH\xampp\htdocs\zrurt\application\controllers\admin\Users.php Line: 642 Function: _error_handler
File: E:\VIKASH\xampp\htdocs\zrurt\index.php Line: 315 Function: require_once
A PHP Error was encountered Severity: Notice
Message: Undefined offset: 4
Filename: admin/Users.php
Line Number: 642
Backtrace:
File: E:\VIKASH\xampp\htdocs\zrurt\application\controllers\admin\Users.php Line: 642 Function: _error_handler
File: E:\VIKASH\xampp\htdocs\zrurt\index.php Line: 315 Function: 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] =>
)
)
I want to validate all fields and save them to the database.