Form's data can not post (Codeigniter) - php

i am working on Codigniter,i have a issue with posting form's data..
in my form,there is different section,i have put array for that different section
Here is my code (Controller)
<?php
class Resume extends CI_Controller{
var $user_id;
function __construct(){
parent::__construct();
$this->user_id = $this->session->userdata('user_id');
$this->load->model("Resume_model");
if($this->session->userdata('user_id') == NULL){
redirect('user/login','refresh');
}
}
public function index(){
$this->Resume();
}
public function Resume(){
if($this->input->post('Skills')){
$Resume = $this->input->post();
$ResumeInsert=array();
$ResumeUpdate=array();
$Key_Resume__ExistIDs=array();
foreach($this->input->post('Person_id') as $key =>$value ){
$ResumeInsert[$key]['updated'] = Date("Y-m-d");
$ResumeInsert[$key]['Importance_JK'] = $Resume['Importance_JK'];
$ResumeInsert[$key]['user_FK'] = $user_id;
$ResumeInsert[$key]['Person_id'] = $Resume['Person_id'][$key];
$ResumeInsert[$key]['email_contact'] = $Resume['email_contact'][$key];
$ResumeInsert[$key]['Contact_Phone'] = $Resume['Contact_Phone'][$key];
$ResumeInsert[$key]['communication'] = $Resume['communication'][$key];
$ResumeInsert[$key]['creativity'] = $Resume['creativity'][$key];
$ResumeInsert[$key]['team_work'] = $Resume['team_work'][$key];
$ResumeInsert[$key]['organizational'] = $Resume['organizational'][$key];
$ResumeInsert[$key]['Leadership'] = $Resume['Leadership'][$key];
$ResumeInsert[$key]['Productivity'] = $Resume['Productivity'][$key];
$ResumeInsert[$key]['Postal_Address'] = $Resume['Postal_Address'][$key];
$ResumeInsert[$key]['Career_objective'] = $Resume['Career_objective'][$key];
}
foreach($this->input->post('emp_id') as $key =>$value ){
$ResumeInsert[$key]['resume_keyid'] = $Resume['emp_id'][$key];
$ResumeInsert[$key]['employer_name'] = $Resume['employer_name'][$key];
$ResumeInsert[$key]['start_Date'] = $Resume['start_Date'][$key];
$ResumeInsert[$key]['end_date'] = $Resume['end_date'][$key];
$ResumeInsert[$key]['type_id'] = $Resume['type_id'][$key];
$ResumeInsert[$key]['position'] = $Resume['position'][$key];
$ResumeInsert[$key]['responsibility'] = $Resume['responsibility'][$key];
$ResumeInsert[$key]['Skills'] = $Resume['Skills'][$key];
print_r ($ResumeInsert);exit;
}
foreach($this->input->post('resume_id1') as $key =>$value ){
$ResumeInsert[$key]['edu_id'] = $Resume['resume_id1'][$key];
$ResumeInsert[$key]['year_gain'] = $Resume['year_gain'][$key];
$ResumeInsert[$key]['qualification_name'] = $Resume['qualification_name'][$key];
$ResumeInsert[$key]['institution_name'] = $Resume['institution_name'][$key];
$ResumeInsert[$key]['outstanding_Ach'] = $Resume['outstanding_Ach'][$key];
$ResumeInsert[$key]['aquired_skill'] = $Resume['aquired_skill'][$key];
$ResumeInsert[$key]['expiry'] = $Resume['expiry'][$key];
$ResumeInsert[$key]['Application_of_Skill'] = $Resume['Application_of_Skill'][$key];
}
foreach($this->input->post('resume_id2') as $key =>$value ){
$ResumeInsert[$key]['resume_skillID'] = $Resume['resume_id2'][$key];
$ResumeInsert[$key]['skill_name'] = $Resume['skill_name'][$key];
$ResumeInsert[$key]['Licences_permits'] = $Resume['Licences_permits'][$key];
$ResumeInsert[$key]['date_Achived'] = $Resume['date_Achived'][$key];
$ResumeInsert[$key]['expiry_renewal'] = $Resume['expiry'][$key];
$ResumeInsert[$key]['skill_work'] = $Resume['application_of_skill'][$key];
$ResumeInsert[$key]['course_tilte'] = $Resume['course_tilte'][$key];
}
foreach($this->input->post('resume_id3') as $key =>$value ){
$ResumeInsert[$key]['hobbyId'] = $Resume['resume_id3'][$key];
$ResumeInsert[$key]['hobby_name'] = $Resume['hobby_name'][$key];
$ResumeInsert[$key]['achievement_name'] = $Resume['achievement_name'][$key];
$ResumeInsert[$key]['Application_OF_hobby_work'] = $Resume['Application_of_Skill'][$key];
}
foreach($this->input->post('resume_id4') as $key =>$value ){
$ResumeInsert[$key]['ref_resume_ID'] = $Resume['resume_id4'][$key];
$ResumeInsert[$key]['referee_name'] = $Resume['referee_name'][$key];
$ResumeInsert[$key]['Position_referee'] = $Resume['Position'][$key];
$ResumeInsert[$key]['company_name_ref'] = $Resume['company_name'][$key];
$ResumeInsert[$key]['phone_ref'] = $Resume['phone_ref'][$key];
$ResumeInsert[$key]['contact_email_ref'] = $Resume['contact_email'][$key];
$ResumeInsert[$key]['reference_Type'] = $Resume['reference_Type'][$key];
$ResumeInsert[$key]['Application_of_Skill_ref'] = $Resume['Application_of_Skill'][$key];
}
foreach($this->input->post('resume_id5') as $key =>$value ){
$ResumeInsert[$key]['Application_Id'] = $Resume['resume_id5'][$key];
$ResumeInsert[$key]['app_date_sent'] = $Resume['date_sent'][$key];
$ResumeInsert[$key]['app_busisness_name'] = $Resume['app_busisness_name'][$key];
$ResumeInsert[$key]['app_address'] = $Resume['app_address'][$key];
$ResumeInsert[$key]['app_contact_phone'] = $Resume['app_contact_phone'][$key];
$ResumeInsert[$key]['app_outcome'] = $Resume['app_outcome'][$key];
$ResumeInsert[$key]['app_date_closed'] = $Resume['app_date_closed'][$key];
}
if(isset($Resume['id'][$key]))
{
// echo "test";exit;
$Key_Resume__ExistIDs[]=$Resume['id'][$key];
if( $ResumeInsert[$key]['Person_id'] != ""){
$ResumeUpdate[$key]['Person_id'] = $ResumeInsert[$key]['Person_id'];
}
if( $ResumeInsert[$key]['email_contact'] != ""){
$ResumeUpdate[$key]['email_contact'] = $ResumeInsert[$key]['email_contact'];
}
if( $ResumeInsert[$key]['Contact_Phone'] != ""){
$ResumeUpdate[$key]['Contact_Phone'] = $ResumeInsert[$key]['Contact_Phone'];
}
if( $ResumeInsert[$key]['communication'] != ""){
$ResumeUpdate[$key]['communication'] = $ResumeInsert[$key]['communication'];
}
if( $ResumeInsert[$key]['creativity'] != ""){
$ResumeUpdate[$key]['creativity'] = $ResumeInsert[$key]['creativity'];
}
if( $ResumeInsert[$key]['team_work'] != ""){
$ResumeUpdate[$key]['team_work'] = $ResumeInsert[$key]['team_work'];
}
if( $ResumeInsert[$key]['organizational'] != ""){
$ResumeUpdate[$key]['organizational'] = $ResumeInsert[$key]['organizational'];
}
if( $ResumeInsert[$key]['Leadership'] != ""){
$ResumeUpdate[$key]['Leadership'] = $ResumeInsert[$key]['Leadership'];
}
if( $ResumeInsert[$key]['Productivity'] != ""){
$ResumeUpdate[$key]['Productivity'] = $ResumeInsert[$key]['Productivity'];
}
if( $ResumeInsert[$key]['Postal_Address'] != ""){
$ResumeUpdate[$key]['Postal_Address'] = $ResumeInsert[$key]['Postal_Address'];
}
if( $ResumeInsert[$key]['Career_objective'] != ""){
$ResumeUpdate[$key]['Career_objective'] = $ResumeInsert[$key]['Career_objective'];
}
if( $ResumeInsert[$key]['resume_keyid'] != ""){
$ResumeUpdate[$key]['resume_keyid'] = $ResumeInsert[$key]['resume_keyid'];
}
if( $ResumeInsert[$key]['employer_name'] != ""){
$ResumeUpdate[$key]['employer_name'] = $ResumeInsert[$key]['employer_name'];
}
if( $ResumeInsert[$key]['start_Date'] != ""){
$ResumeUpdate[$key]['start_Date'] = $ResumeInsert[$key]['start_Date'];
}
if( $ResumeInsert[$key]['end_date'] != "") {
$ResumeUpdate[$key]['end_date'] = $ResumeInsert[$key]['end_date'];
}
if( $ResumeInsert[$key]['type_id'] != ""){
$ResumeUpdate[$key]['type_id'] = $ResumeInsert[$key]['type_id'];
}
if( $ResumeInsert[$key]['position'] != ""){
$ResumeUpdate[$key]['position'] = $ResumeInsert[$key]['position'];
}
if( $ResumeInsert[$key]['edu_id'] != ""){
$ResumeUpdate[$key]['edu_id'] = $ResumeInsert[$key]['edu_id'];
}
if( $ResumeInsert[$key]['year_gain'] != ""){
$ResumeUpdate[$key]['year_gain'] = $ResumeInsert[$key]['year_gain'];
}
if( $ResumeInsert[$key]['qualification_name'] != ""){
$ResumeUpdate[$key]['qualification_name'] = $ResumeInsert[$key]['qualification_name'];
}
if( $ResumeInsert[$key]['institution_name'] != ""){
$ResumeUpdate[$key]['institution_name'] = $ResumeInsert[$key]['institution_name'];
}
if( $ResumeInsert[$key]['outstanding_Ach'] != ""){
$ResumeUpdate[$key]['outstanding_Ach']= $ResumeInsert[$key]['outstanding_Ach'];
}
if( $ResumeInsert[$key]['aquired_skill'] != ""){
$ResumeUpdate[$key]['aquired_skill'] = $ResumeInsert[$key]['aquired_skill'];
}
if( $ResumeInsert[$key]['expiry'] != ""){
$ResumeUpdate[$key]['expiry'] = $ResumeInsert[$key]['expiry'];
}
if( $ResumeInsert[$key]['Application_of_Skill'] != ""){
$ResumeUpdate[$key]['Application_of_Skill'] = $ResumeInsert[$key]['Application_of_Skill'];
}
if( $ResumeInsert[$key]['resume_skillID'] != ""){
$ResumeUpdate[$key]['resume_skillID'] = $ResumeInsert[$key]['resume_skillID'];
}
if( $ResumeInsert[$key]['skill_name'] != ""){
$ResumeUpdate[$key]['skill_name'] = $ResumeInsert[$key]['skill_name'];
}
if( $ResumeInsert[$key]['Licences_permits'] != ""){
$ResumeUpdate[$key]['Licences_permits'] = $ResumeInsert[$key]['Licences_permits'];
}
if( $ResumeInsert[$key]['date_Achived'] != ""){
$ResumeUpdate[$key]['date_Achived'] = $ResumeInsert[$key]['date_Achived'];
}
if( $ResumeInsert[$key]['expiry_renewal'] !="") {
$ResumeUpdate[$key]['expiry_renewal'] = $ResumeInsert[$key]['expiry_renewal'];
}
if( $ResumeInsert[$key]['skill_work'] != ""){
$ResumeUpdate[$key]['skill_work'] = $ResumeInsert[$key]['skill_work'];
}
if( $ResumeInsert[$key]['course_tilte'] != ""){
$ResumeUpdate[$key]['course_tilte'] = $ResumeInsert[$key]['course_tilte'];
}
if( $ResumeInsert[$key]['hobbyId'] != ""){
$ResumeUpdate[$key]['hobbyId'] = $ResumeInsert[$key]['hobbyId'];
}
if( $ResumeInsert[$key]['hobby_name'] != ""){
$ResumeUpdate[$key]['hobby_name'] = $ResumeInsert[$key]['hobby_name'];
}
if( $ResumeInsert[$key]['achievement_name'] != ""){
$ResumeUpdate[$key]['achievement_name'] = $ResumeInsert[$key]['achievement_name'];
}
if( $ResumeInsert[$key]['Application_OF_hobby_work'] != ""){
$ResumeUpdate[$key]['Application_OF_hobby_work'] = $ResumeInsert[$key]['Application_OF_hobby_work'];
}
if( $ResumeInsert[$key]['ref_resume_ID'] != ""){
$ResumeUpdate[$key]['ref_resume_ID'] = $ResumeInsert[$key]['ref_resume_ID'];
}
if( $ResumeInsert[$key]['referee_name'] != ""){
$ResumeUpdate[$key]['referee_name'] = $ResumeInsert[$key]['referee_name'];
}
if( $ResumeInsert[$key]['Position_referee'] != ""){
$ResumeUpdate[$key]['Position_referee'] = $ResumeInsert[$key]['Position_referee'];
}
if( $ResumeInsert[$key]['company_name_ref'] != ""){
$ResumeUpdate[$key]['company_name_ref'] = $ResumeInsert[$key]['company_name_ref'];
}
if( $ResumeInsert[$key]['phone_ref'] != ""){
$ResumeUpdate[$key]['phone_ref'] = $ResumeInsert[$key]['phone_ref'];
}
if( $ResumeInsert[$key]['contact_email_ref'] != ""){
$ResumeUpdate[$key]['contact_email_ref'] = $ResumeInsert[$key]['contact_email_ref'];
}
if( $ResumeInsert[$key]['reference_Type'] != ""){
$ResumeUpdate[$key]['reference_Type'] = $ResumeInsert[$key]['reference_Type'];
}
if( $ResumeInsert[$key]['Application_of_Skill_ref'] != ""){
$ResumeUpdate[$key]['Application_of_Skill_ref'] = $ResumeInsert[$key]['Application_of_Skill_ref'];
}
if( $ResumeInsert[$key]['app_date_sent'] != ""){
$ResumeUpdate[$key]['app_date_sent'] = $ResumeInsert[$key]['app_date_sent'];
}
if( $ResumeInsert[$key]['Application_Id'] != ""){
$ResumeUpdate[$key]['Application_Id'] = $ResumeInsert[$key]['Application_Id'];
}
if( $ResumeInsert[$key]['app_busisness_name'] != ""){
$ResumeUpdate[$key]['app_busisness_name'] = $ResumeInsert[$key]['app_busisness_name'];
}
if( $ResumeInsert[$key]['app_address'] != ""){
$ResumeUpdate[$key]['app_address'] = $ResumeInsert[$key]['app_address'];
}
if( $ResumeInsert[$key]['app_contact_phone'] != "")
{
$ResumeUpdate[$key]['app_contact_phone'] = $ResumeInsert[$key]['app_contact_phone'];
}
if( $ResumeInsert[$key]['app_outcome'] != ""){
$ResumeUpdate[$key]['app_outcome'] = $ResumeInsert[$key]['app_outcome'];
}
if( $ResumeInsert[$key]['app_date_closed'] != ""){
$ResumeUpdate[$key]['app_date_closed'] = $ResumeInsert[$key]['app_date_closed'];
}
if( $ResumeInsert[$key]['resume_id'] != ""){
$ResumeUpdate[$key]['resume_id'] = $ResumeInsert[$key]['resume_id'];
}
$ResumeUpdate[$key]['resume_id']=$Resume['id'][$key];
unset($ResumeInsert[$key]);
}
else{
$ResumeInsert[$key]['resume_id'] = $GetLastID;
$GetLastID++;
}
$status=$this->Resume_model->ProcessData($idsToDelete,$ResumeUpdate,$user_id,$ResumeInsert,$imgInsert,$imgUpdate);
redirect('Resume','refresh');
}
}
?>
Here the insert query (model)
function ProcessData($tbl_resumeInsert){
if(!empty($tbl_resumeInsert))
{
$this->insert_tbl_resume($user_id,$tbl_resumeInsert);
}
}
function insert_tbl_resume($id,$arrtbl_resume)
{
$this->db->insert_batch('tbl_resume', $arrtbl_resume);
}
in above code,when im going to submit the form only first Foreach loop's data can insert,when i print $ResumeInsert,it shows me only first foreach loop's data foreach($this->input->post('Person_id') as $key =>$value )
Rest foreach loop's data appears NULL in DB
please any help?
Thank You!

The function $this->input->post('something') is returning a value from the $_POST array with a key of 'something'. If the key 'something' does not exist it returns NULL. (or FALSE in CI version < 3.0)
$_POST is an associative array and will have at most only one item with any given key. So the code
foreach($this->input->post('Person_id') as $key =>$value )
{
...
will only run once as there is only one $_POST array item with the key 'Person_id'.
If your form html has more than one field named 'Person_id' only the value from the last field will be posted to the server.

Related

What would be better Practice when sanitizing/validating php from html inputs?

When doing php validation should I First do the filter sanitizing/validating or is it okay to do it as part of an if statement see the examples below
First Example
$vvalidation = 0;
if (isset($_POST['db9_name']) && $_POST['db9_name'] != ''){
$name = $_POST['db9_name'];
if (filter_var($name, FILTER_SANITIZE_STRING === null)){
$vvalidation++;
}
} else{
$vvalidation++;
}
Second Example
$vvalidation = 0;
if (isset($_POST['db9_name']) && $_POST['db9_name'] != ''){
$name = $_POST['db9_name'];
$vname = filter_var($name, FILTER_SANITIZE_STRING);
if ($vname === null)){
$vvalidation++;
}
} else{
$vvalidation++;
}
and for email ?
example 1
if (isset($_POST['txtemail']) && $_POST['txtemail'] !== '') {
$vEmail = strtolower(strip_tags(trim($_POST['txtemail'])));
$vEmail = str_replace(' ', '', $vEmail);
if (filter_var($vEmail, FILTER_SANITIZE_EMAIL) === null) {
$vValidation++;
} elseif (filter_var($vEmail, FILTER_VALIDATE_EMAIL) === null) {
$vValidation++;
}
} else {
$vValidation++;
}
example 2
if (isset($_POST['txtemail']) && $_POST['txtemail'] !== '') {
$vEmail = strtolower(strip_tags(trim($_POST['txtemail'])));
$vEmail = str_replace(' ', '', $vEmail);
$email = (filter_var($vEmail, FILTER_SANITIZE_EMAIL);
$email .= (filter_var($vEmail, FILTER_VALIDATE_EMAIL);
if (email === null){
$vValidation++;
} else {
$vValidation++;
}
or does it not really matter?

wordpress - frontend post form issue

I have a post form on front end where users can post (post_type = product) from the form. As a part of it I have tried implementing few server side validations as in below code. The issue is that the validations are all working fine but the data is getting saved on form submission even when the validation fails.
Ideally the form submission should fail when there is a field validation failure.
I am not sure if $hasError = true is working or not, there might be a very simple logic I am missing which I am not getting. Any help regarding this?
Thanks in advance.
$postTitleError = '';
if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if (trim($_POST['postTitle']) === '') {
$postTitleError = 'msg 1';
$hasError = true;
}
if (trim($_POST['postCat1']) === '') {
$postTitleError = 'msg2';
$hasError = true;
}
if (trim($_POST['postPrice']) === '') {
$postTitleError = 'msg3';
$hasError = true;
}
if (trim($_POST['postTime']) === '') {
$postTitleError = 'msg4';
$hasError = true;
}
if (trim($_POST['postTimeMin']) === '') {
$postTitleError = 'msg5';
$hasError = true;
}
if (trim($_POST['postContent']) === '') {
$postTitleError = 'msg6';
$hasError = true;
}
<?php
//$postTitleError = '';
$resultArr = array();
$error_msg = false;
if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if (isset($_POST['postTitle']) && !empty($_POST["postTitle"])) {
//$postTitleError = 'msg 1';
//$hasError = true;
$postTitle=$_POST['postTitle'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTitle']= "msg 1";
$error_msg = true;
}
if (isset($_POST['postCat1']) && !empty($_POST["postCat1"]) ) {
// $postTitleError = 'msg2';
// $hasError = true;
$postCat1=$_POST['postCat1'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postCat1']= "msg2";
$error_msg = true;
}
if (isset($_POST['postPrice']) && !empty($_POST["postPrice"]) ) {
// $postTitleError = 'msg3';
//$hasError = true;
$postPrice=$_POST['postPrice'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postPrice']= "msg3";
$error_msg = true;
}
if (isset($_POST['postTime']) && !empty($_POST["postTime"]) ) {
//$postTitleError = 'msg4';
//$hasError = true;
$postTime=$_POST['postTime'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTime']= "msg4";
$error_msg = true;
}
if (isset($_POST['postTimeMin']) && !empty($_POST["postTimeMin"]) ) {
// $postTitleError = 'msg5';
// $hasError = true;
$postTimeMin=$_POST['postTimeMin'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTimeMin']= "msg5";
$error_msg = true;
}
if (isset($_POST['postContent']) && !empty($_POST["postContent"]) ) {
//$postTitleError = 'msg6';
// $hasError = true;
$postContent=$_POST['postContent'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postContent']= "msg6";
$error_msg = true;
}
if($error_msg == false)
{
//here publish post code
}
else
{
//here Error message prine
}
?>

Codeigniter update records error

I am using codeigniter framework, and I keep getting this error when I submit my from to post to my database.
Controller
public function profilePic()
{
if ($this->session->userdata('userLogin')) {
$user_id = $this->session->userdata('user_id');
$data = array();
if (isset($_POST['add'])) {
$pic = $this->input->post('profileFace');
$front = $this->input->post('frontView');
$left = $this->input->post('leftView');
$right = $this->input->post('rightView');
$back = $this->input->post('backView');
$updtResult = $this->main_model->updateProfilePic($pic,$front,$left,$right,$back,$user_id);
redirect("userProfile");
} else {
$data['userdata'] = $this->main_model->getUserData();
$this->load->view("frontend/ajax-view", $data);
}
} else {
redirect("/fitness");
}
}
Model
function updateProfilePic($pic,$front,$left,$right,$back,$user_id) {
$check = $this->getUserData();
if(count($check)!=0)
{
if($pic != "")
{
$data['image'] = $pic;
}
if($front != "")
{
$data['front_view'] = $front;
}
if($left != "")
{
$data['left_view'] = $left;
}
if($right != "")
{
$data['right_view'] =$right;
}
if($back != "")
{
$data['back_view'] = $back;
}
// pr($data);
$this->db->where('user_id',$user_id);
$result = $this->db->update('fitness_users', $data);
}
return $result;
}
and the error i am getting is:
You can use set method to update your data and check your variable is set or not using isset()
if (isset($pic) && $pic != "") {
$this->db->set("image", $pic);
}
if (isset($front) && $front != "") {
$this->db->set("front_view", $front);
}
if (isset($left) && $left != "") {
$this->db->set("left_view", $left);
}
if (isset($right) && $right != "") {
$this->db->set("right_view", $right);
}
if (isset($back) && $back != "") {
$this->db->set("back_view", $back);
}
// pr($data);
$this->db->where('user_id', $user_id);
$result = $this->db->update('fitness_users');

How to Auto Login and Redirect After Registration in opencart

I am using opencart 2.0 and i have made custom registeration page in opencart.After successful registration i am redirecting to myaccount page but it will redirect to login page.
Can anybody help me in this to sort out the problem?
Here's my controller code :-
public function register() {
$this->load->language('checkout/checkout');
$json = array();
//if (!$json) {
$this->load->model('account/customer');
if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
$json['error']['firstname'] = $this->language->get('error_firstname');
}
/*if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
$json['error']['lastname'] = $this->language->get('error_lastname');
}*/
if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $this->request->post['email'])) {
$json['error']['email'] = $this->language->get('error_email');
}
if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
$json['error']['warning'] = $this->language->get('error_exists');
}
if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
$json['error']['telephone'] = $this->language->get('error_telephone');
}
/*if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) {
$json['error']['address_1'] = $this->language->get('error_address_1');
}
if ((utf8_strlen(trim($this->request->post['city'])) < 2) || (utf8_strlen(trim($this->request->post['city'])) > 128)) {
$json['error']['city'] = $this->language->get('error_city');
}
$this->load->model('localisation/country');
$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
$json['error']['postcode'] = $this->language->get('error_postcode');
}
if ($this->request->post['country_id'] == '') {
$json['error']['country'] = $this->language->get('error_country');
}
if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
$json['error']['zone'] = $this->language->get('error_zone');
}*/
if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
$json['error']['password'] = $this->language->get('error_password');
}
if ($this->request->post['confirm'] != $this->request->post['password']) {
$json['error']['confirm'] = $this->language->get('error_confirm');
}
/*if ($this->config->get('config_account_id')) {
$this->load->model('catalog/information');
$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
if ($information_info && !isset($this->request->post['agree'])) {
$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
}
} */
// Customer Group
if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
$customer_group_id = $this->request->post['customer_group_id'];
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
// Custom field validation
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
foreach ($custom_fields as $custom_field) {
if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
}
//}
//if (!$json) {
$customer_id = $this->model_account_customer->addCustomer($this->request->post);
// Clear any previous login attempts for unregistered accounts.
$this->model_account_customer->deleteLoginAttempts($this->request->post['email']);
$this->session->data['account'] = 'register';
$this->load->model('account/customer_group');
$customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
/*if ($customer_group_info && !$customer_group_info['approval']) {
$this->customer->login($this->request->post['email'], $this->request->post['password']);
// Default Payment Address
$this->load->model('account/address');
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
if (!empty($this->request->post['shipping_address'])) {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
} else {*/
$json['redirect'] = $this->url->link('account/account');
//}
unset($this->session->data['guest']);
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
// Add to activity log
$this->load->model('account/activity');
$activity_data = array(
'customer_id' => $customer_id,
'name' => $this->request->post['firstname'] . ' ' . $this->request->post['lastname']
);
$this->model_account_activity->addActivity('register', $activity_data);
//}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}

Including already existing variable in a function

Im trying to make a function, that takes an input, determines its value, and outputs a word from an already existing array that has been included in the script. The problem is the output is blank, i believe the function ignores variables already in the script, is there a way to change this so existing variables arent ignored by the function?
Here is the function:
The words need to be from an array because of multi-lingual requirements.
function get_genre($id)
{
if($id == "1"){
$genre = $lang['277'];
}
if($id == "2"){
$genre = $lang['278'];
}
if($id == "3"){
$genre = $lang['279'];
}
if($id == "4"){
$genre = $lang['280'];
}
if($id == "5"){
$genre = $lang['281'];
}
if($id == "6"){
$genre = $lang['282'];
}
if($id == "7"){
$genre = $lang['283'];
}
if($id == "8"){
$genre = $lang['284'];
}
if($id == "9"){
$genre = $lang['285'];
}
if($id == "10"){
$genre = $lang['286'];
}
if($id == "11"){
$genre = $lang['287'];
}
if($id == "12"){
$genre = $lang['288'];
}
if($id == "13"){
$genre = $lang['289'];
}
if($id == "14"){
$genre = $lang['290'];
}
if($id == "15"){
$genre = $lang['374'];
}
return $genre;
}
function get_genre($id)
{
global $lang;
....
}
or
function get_genre($id, $lang) //Must pass $lang array to function here
{
}
function get_genre($id)
{
global $lang;
if($id == "1"){
$genre = $lang['277'];
}
if($id == "2"){
$genre = $lang['278'];
}
if($id == "3"){
$genre = $lang['279'];
}
if($id == "4"){
$genre = $lang['280'];
}
if($id == "5"){
$genre = $lang['281'];
}
if($id == "6"){
$genre = $lang['282'];
}
if($id == "7"){
$genre = $lang['283'];
}
if($id == "8"){
$genre = $lang['284'];
}
if($id == "9"){
$genre = $lang['285'];
}
if($id == "10"){
$genre = $lang['286'];
}
if($id == "11"){
$genre = $lang['287'];
}
if($id == "12"){
$genre = $lang['288'];
}
if($id == "13"){
$genre = $lang['289'];
}
if($id == "14"){
$genre = $lang['290'];
}
if($id == "15"){
$genre = $lang['374'];
}
return $genre;
}
While this isn't ideal, have you considered using an associative array?
var $lookupArray = array();
$lookupArray["1"] = $lang['274'];
....
Then you can call it like this:
function get_genre($id)
{
return(array_key_exists($id,$lookupArray)) ? $lookupArray[$id] : null;
}

Categories