Very Strange Input issue - php

I have got a very strange input issue in which it seems that my seo description box and main content box are linked due to if I input any data into the seo input box it changes in the database in the content area as well but not the otherway around.
View:
<?php
//Setting form attributes
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit');
$formInputTitle = array('id' => 'title', 'name' => 'title');
$formSEODescription = array('id' =>'seoDescription', 'name' => 'seoDescription');
$formTextareaContent = array('id' => 'textContent', 'name' => 'textContent');
?>
<?php print_r($page);?>
<div id ="formLayout" class="editPage">
<?php echo form_open('admin/editpage/index/'.$page[0]['id'].'/'.url_title($page[0]['name'],'dash', TRUE),$formpageEdit); ?>
<?php echo form_fieldset(); ?>
<h4>You are editing: <?= $page[0]['name']; ?> </h4>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';
}
?>
<label><?php echo form_label ('SEO Description:', 'description');?><span class="small">Required Field</span></label>
<?php echo form_input($formSEODescription, $page[0]['description']); ?>
<label><?php echo form_label ('Content:', 'content');?><span class="small">Required Field</span></label>
<?php echo form_textarea($formTextareaContent, $page[0]['content']); ?>
<script type="text/javascript">CKEDITOR.replace('textContent');</script>
<?php echo form_submit('submit','Submit'); ?>
<?php echo form_fieldset_close();
echo form_close(); ?>
</div>
Model
<?php
/**
* This model handles the sql for the checking of the username in the database
*/
class Page_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function Page_model(){
parent::Model();
}
function getCMSContent($id = NULL) {
$this->db->where('id', $id);
$query = $this->db->get('pages', 1);
if($query->num_rows() > 0) {
$row = $query->result_array();
return $row;
}else{
return FALSE;
}
}
function updatePage($id = NULL, $data = NULL){
#set the $data passed to the function into an array, content being the column name.
$data = array('content' => $data, 'description' => $data);
$this ->db->where('id',$id);
$this->db->update('pages', $data);
return TRUE;
}
}
?>
Controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Editpage extends CI_Controller {
function __construct(){
parent::__construct();
}
function index($id){
if(!$this->session->userdata('logged_in'))redirect('admin/home');
if ($this->input->post('submit')){
#The User has submitted updates, lets begin!
#Set The validation Rules
$this->form_validation->set_rules('textContent', 'Content', 'trim|required|xss_clean');
$this->form_validation->set_rules('seoDescription', 'SEO Description', 'trim|required|xss_clean');
#if the form_validation rules fail then load the login page with the errors. Otherwise continue validating the user/pass
if ($this->form_validation->run() == FALSE){
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
#connect to getCMSCotent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
$data['page'] = $this->page_model->getCMSContent($id);
$data['sales_pages'] = $this->sales_model->getSalesPages();
$data['content'] = $this->load->view('admin/editpage', $data, TRUE);
$this->load->view('admintemplate', $data);
}
#Form Validation passed, so lets continue updating.
#lets set some variables.
$content = $this->input->post('textContent', TRUE);
$content = $this->input->post('seoDescription', TRUE);
$this->db->escape($content);
#Now if updatePage fails to update hte database then show "there was a problem", you could echo the db error itself
if($this->page_model->updatePage($id, $content)) {
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
#connect to getCMSContent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
$data['page'] = $this->page_model->getCMSContent($id);
$data['success'] = TRUE;
$data['content'] = $this->load->view('admin/editpage', $data, TRUE);
$this->load->view('admintemplate', $data);
}//END if updatePage
}else{
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages();
#connect to getCMSCotent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
$data['page'] = $this->page_model->getCMSContent($id);
$data['content'] = $this->load->view('admin/editpage', $data, TRUE);
$this->load->view('admintemplate', $data);
}//END if post submitted
} //END function index()
}

Found it:
$content = $this->input->post('textContent', TRUE);
$content = $this->input->post('seoDescription', TRUE);
[update below]
You are overwriting the $content variable with the content of seoDescription so textContent never reaches your db.
You need to update your updatePage function:
function updatePage($id = NULL, $content = NULL, $description = NULL){
#set the $data passed to the function into an array, content being the column name.
$data = array('content' => $content, 'description' => $description);
$this ->db->where('id',$id);
$this->db->update('pages', $data);
return TRUE;
}
and call it appropriately:
[...]
#Form Validation passed, so lets continue updating.
#lets set some variables.
$content = $this->input->post('textContent', TRUE);
$description = $this->input->post('seoDescription', TRUE);
$this->db->escape($content);
$this->db->escape($description);
#Now if updatePage fails to update hte database then show "there was a problem", you could echo the db error itself
if($this->page_model->updatePage($id, $content, $description)) {
[...]
By the way are you sure you are using db->escape correctly? The way you're calling it will only work if the escape function accepts parameters by reference (e.g. using & in front of the parameter name, and setting this value to the escaped value). I'd expect this code to be the correct version, but I don't know your db class so I may be wrong:
$content = $this->db->escape($this->input->post('textContent', TRUE));
$description = $this->db->escape($this->input->post('seoDescription', TRUE));

Related

How to Resolve Page Redirect issue in Codeigniter

When I click on href the dashboard page is loaded but the the location on the href is a different page.
<li>Add supplier</li>
Controller
public function add_supplier($id = null)
{
$this->tbl_supplier('supplier_id');
if ($id) {//condition check
$result = $this->global_model->get_by(array('supplier_id' => $id), true);
if ($result) {
$data['supplier'] = $result;
} else {
//msg
$type = 'error';
$message = 'Sorry, No Record Found!';
set_message($type, $message);
redirect('admin/purchase/manage_supplier');
}
}
// view page
$data['title'] = 'Add New Supplier';
//$data['editor'] = $this->data;
$data['subview'] = $this->load->view('admin/purchase/add_supplier', $data, true);
$this->load->view('admin/_layout_main', $data);
}
have you tried to do this:
<li>Add supplier</li>
Passing the path into the site_url() helper function will return the correct path what are you looking for.

Using multiple controllers cause session error codeigniter

When I use only 1 controller in my application, user, everything works fine. When I try to use 2 or 3 to divide the code and create some structure my application always give the following error :
A PHP Error was encountered
Severity: Warning
Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php:99)
Filename: Session/Session.php
Line Number: 140
Backtrace:
File: /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php
Line: 11
Function: __construct
File: /Applications/MAMP/htdocs/BT_dashboard/index.php
Line: 292
Function: require_once
Googled it and tried many things but can't find an answer. I'm using codeigniter 3. My User controller looks like :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User class.
*
* #extends CI_Controller
*/
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
$this->load->model('project_model');
}
public function createProject() {
if ($_SESSION['userlevel']) {
if ($_SESSION['userlevel'] < 3) {
$userid = $this->uri->segment(3);
} else {
$userid = $this->input->post('userproject');
}
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('project_name', 'project name', 'trim|required|min_length[2]|callback_is_project_name_unique[' . $this->input->post('project_name') . ']');
$this->form_validation->set_rules('project_address', 'project address', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_description', 'project description', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_city', 'project city', 'trim|required|min_length[2]');
if ($this->form_validation->run() == FALSE) {
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
if ($_SESSION['userlevel'] < 3) {
$this->load->view('dashboard_add_project', $data);
} else {
$data['userslist'] = $this->user_model->get_users_list();
$this->load->view('dashboard_add_project_admin', $data);
}
$this->load->view('wrapper', $data);
} else {
$Address = urlencode($this->input->post('project_address'));
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $Address . "&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status == "OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
//pass validation
$data = array(
'project_name' => $this->input->post('project_name'),
'project_address' => $this->input->post('project_address'),
'project_description' => $this->input->post('project_description'),
'project_city' => $this->input->post('project_city'),
'project_finished' => $this->input->post('project_finished'),
'lat' => $Lat,
'lng' => $Lon,
);
//$this->db->insert('tbl_user', $data);
if ($this->user_model->create_project($data, $userid, $this->input->post('project_name'))) {
if ($_SESSION['userlevel'] > 1) {
$data['projectlist'] = $this->user_model->get_project_list();
$data['uncompleted_projects'] = $this->user_model->get_uncompleted_projects();
$data['completed_projects'] = $this->user_model->get_completed_projects();
} else {
$data['projectlist'] = $this->user_model->get_project_list_userid($userid);
}
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
$this->load->view('dashboard_projects', $data);
$this->load->view('wrapper', $data);
} else {
$data->error = 'There was a problem creating your new employee. Please try again.';
$data['userdata'] = $this->session->userdata;
// send error to the view
$this->load->view('header', $data);
$this->load->view('dashboard_add_project', $data);
$this->load->view('wrapper', $data);
}
}
}
}
My second controller, the project controller. This is the controller I want to use know to paste the createproject code so this gives more structure. But when I paste it here and adapt my views it gives the error. here's my project_controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* File Name: employee.php
*/
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
}
public function createProject() {
//same as usercontroller
My User/login code in User controller :
public function login() {
$loggedin = $this->session->userdata('logged_in');
if (!$loggedin) {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('user_mail', 'user mail', 'required');
$this->form_validation->set_rules('user_password', 'user password', 'required');
if ($this->form_validation->run() == false) {
$data = new stdClass();
$data->error = 'Check your user and password';
$this->load->view('dashboard_login', $data);
} else {
$usermail = $this->input->post('user_mail');
$password = $this->input->post('user_password');
if ($this->user_model->resolve_user_login($usermail, $password)) {
$user_id = $this->user_model->get_user_id_from_mail($usermail);
$user = $this->user_model->get_user($user_id);
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = (string) $user->user_name;
$_SESSION['logged_in'] = (bool) true;
$_SESSION['user_gsm'] = (string) $user->user_gsm;
$_SESSION['user_address'] = (string) $user->user_address;
$_SESSION['user_city'] = (string) $user->user_city;
$_SESSION['userlevel'] = $this->user_model->get_user_level((int) $user->user_id);
$_SESSION['user_mail'] = $usermail;
$data['userdata'] = $this->session->userdata;
if ($_SESSION['userlevel'] == "3") {
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
}
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
} else {
$data = new stdClass();
// login failed
$data->error = 'Wrong username or password.';
// send error to the view
$this->load->view('dashboard_login', $data);
}
}
} else {
$data['userdata'] = $this->session->userdata;
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
}
}
In config/autoload I've got following:
$autoload['libraries'] = array('database','session');
The session library should be automatically loaded for you. There's no reason for you to include it in each of your constructor functions.
Remove it from both controllers, and it'll work fine.
If it does not work, check in your config/autoload.php file to make sure it is being loaded there correctly.
This is a common problem , notice that the error is on line 99 on your controller Project.php (you should have only 76 lines if I am correct). I bet your controller doesn't have 99 lines ... delete the extra lines and the error will vanish.
or if you have an echo on line 99 comment it
hope that helps!

PHP Update in codeigniter

I am trying to write a function to insert, update, and delete data. I can insert data, but can't update the data. The problem is I can't pass id value to my updateform.
Controller:
function update($id = 0){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('employees_model');
if($this->input->post('upsubmit')){
echo '<pre>';print_r($id);echo '</pre>';
exit();
if($this->input->post('eid')){
$this->employees_model->entry_update();
}
}
$data = $this->employees_model->general();
if((int)$id > 0){
$query =this->employees_model->get($id);
$data['txt_id']['value'] = $query['eid'];
$data['txt_name']['value'] = $query['ename'];
$data['txt_salary']['value'] = $query['esalary'];
$data['txt_emailid']['value'] = $query['eemailid'];
}
$this->load->view('employees_update',$data);
}
Model:
function general(){
$data['base'] = $this->config->item('base_url');
$data['css'] = $this->config->item('css');
$data['id']['value'] = 'eid';
$data['name'] = 'txt_name';
$data['salary'] = 'txt_salary';
$data['emailId'] = 'txt_email';
return $data;
}
function entry_update($id){
$this->load->database();
echo '<pre>';print_r($id);echo '</pre>';
exit();
$data = array(
'ename'=>$this->input->post('txt_name'),
'esalary'=>$this->input->post('txt_salary'),
'eemailid'=>$this->input->post('txt_email'),
);
echo '<pre>';print_r($data);echo '</pre>';
exit();
$this->db->where('eid',$this->input->post($data));
$this->db->update('employee',$data);
//echo '<pre>';print_r($data);echo '</pre>';
echo "Updated successfully";
}
function get($id)
{
$this->load->database();
$query = $this->db->get_where('employee',array('eid'=>$id));
return $query->row_array();
}
Please suggest how to pass Select id values to update form page in views section.
Here is my working sample code. Hopes it helps.
controller
//I am using update from to preload data
function update_form($id)
{
$query=$this->product_model->get($id);
$data['id']=$query['id'];
$data['name']=$query['name'];
$data['price']=$query['price'];
$this->load->view('products/update_product',$data);
}
function update()
{
$this->product_model->save();
redirect('products/index');
}
Model
function get($id=0)
{
if($id==0)
{
$query=$this->db->get('product');
return $query->result_array();
}
else
{
$this->db->where('id',$id);
$query=$this->db->get('product');
return $query->row_array();
}
}
// if **id** is greater than zero mean update, no **id** mean insert
function save()
{
$id=$this->input->post('id');
$name=$this->input->post('name');
$price=$this->input->post('price');
$data=array
(
'name'=>$name,
'price'=>$price
);
if($id>0)
{
$this->db->where('id',$id);
$this->db->update('product',$data);
}
else
{
$this->db->insert('product',$data);
}
}
}
View (view/products/update_product.php)
<?php echo form_open('products/save');?> //form_open('controller_name/function_name');
<?php echo form_hidden('id',$id);?>
<?php echo form_input('name',$name,'placeholder="enter doctor name"');?><br>
<?php echo form_input('price',$pricce,'placeholder="enter doctor phone number"');?><br>
<?php echo form_submit('','Update');?>
<?php echo form_close();?>
id play vita role in updating , you shouldn't include it in View as Hidden Field
At first you created function with parameter $id. But when you call the function without parameter.
change this line
in Controller change this line
$this->employees_model->entry_update();
to
$this->employees_model->entry_update($this->input->post('eid'));
In Model Change this line:
$this->db->where('eid',$this->input->post($data))
to
$this->db->where('eid',$this->input->post('eid'));
Change your update function like as below:
function update($id = 0){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('employees_model');
if($this->input->post('upsubmit')){
$id= $this->input->post('eid');
if($this->input->post('eid')){
$this->employees_model->entry_update();
}
}
$data = $this->employees_model->general();
if((int)$id > 0){
$query =this->employees_model->get($id);
$data['txt_id']['value'] = $query['eid'];
$data['txt_name']['value'] = $query['ename'];
$data['txt_salary']['value'] = $query['esalary'];
$data['txt_emailid']['value'] = $query['eemailid'];
}
$this->load->view('employees_update',$data);
}

Encountering a php error when trying to view my messages with delete button on screen

Instead of my messages appearing on the screen I just see this:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
This is despite my foreach loop in my view page looking ok:
foreach($messages as $message):// this is the line causing the error
$delete = $message['message_id']; // i've even tried removing this line to no avail
//var_dump($message); ?>
<li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("home/deleteMsg/$delete", 'delete')?>)</li>
<?php endforeach?>
Here is my model:
class Messages extends CI_Model
{
function Messages()
{
parent::__construct();
}
function deleteMsg($message_id) // this message_id is the id field for each message in my database
{
$record = array('message_id' => $message_id);
$this->db->delete('messages', $record);
}
function addMessage($message_id, $from, $to, $message)//this fields come after the message_id field in the database
{
$record = array(
'to' => $to,
'from' => $from,
'message' => $message);
$this->db->insert('messages', $record);
}
function getMessages($user)
{
$this->db->select('*');
$this->db->from('messages');
$this->db->where('to', $user);
$messagesSet = $this->db->get();
$messages = array ();
foreach ($messagesSet->result() as $row)
{
$messages[] = array(
'from' => $row ->from,
'message' => $row ->message);
}
return $messages;
}
}
Here is my controller:
class Home extends CI_Controller
{
function Home()
{
parent::__construct();
$this->load->model('messages');
$this->load->model('friends');
$this->load->model("profiles");
}
function drop($member)
{
$username = $this->session->userdata('username');
$this->friends->deleteFriend($username, $member);
redirect('home');
}
function deleteMsg($messageid) {
$this->messages->deleteMsg($messageid);
redirect('home');
}
function index()
{
$username = $this->session->userdata('username');
$membername = $this->session->userdata('membername');
$viewData['membername'] = $membername;
$viewData['username'] = $username;
$viewData['following'] = $this->friends->getFollowing($username);
$viewData['followers'] = $this->friends->getFollowers($username);
$viewData['messages'] = $this->messages->getMessages($membername);
$viewData['friends'] = $this->friends->getFriends($username);
$viewData['messages'] = $this->messages->deleteMsg($membername);
$this->load->view('shared/header');
$this->load->view('home/hometitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('home/homeview', $viewData);
$this->load->view('shared/footer');
}
}
Your $messages is empty because it's overwritted with this:
$viewData['messages'] = $this->messages->deleteMsg($membername);
in the index function.
Your messages may be empty
<?php
if(!empty($messages)){
foreach($messages as $message):// this is the line causing the error
$delete = $message['message_id']; // i've even tried removing this line to no avail
//var_dump($message); ?>
<li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("home/deleteMsg/$delete", 'delete')?>)</li>
<?php endforeach?>
<?php }else{ ?>
<?php echo 'Its empty !!!'; }?>

Sending two data arrays to view from controller in codeigniter

I want to send two data arrays from my controller to view how can I do it ?
Following is my controller code
class Home extends CI_Controller {
public function box() {
$url = $this->pageURL();
$id_from_url = explode('/', $url);
$id = $id_from_url[6];
$query = $this->db->get_where('mc_boxes', array('idmc_boxes' => $id));
$row = $query->row();
$rowcount = $query->num_rows();
if ($rowcount <= 0) {
echo 'ID not found';
} else {
$box_id = $row->idmc_boxes;
$customer_id = $row->customers_idcustomers;
$language_id = $row->languages_idlanguages;
$template_id = $this->getTemplateID($box_id);
$template_data = $this->getTemplateData($template_id);
$variables_data = $this->getVariables($customer_id, $language_id);
$title = $variables_data[0]['value'];
$this->load->view('template', $template_data);
}
}
}
In my template view when I echo $title it says it is undefined
how can I send the whole $variables_data array with $template_data array
Thanks :)
Instead of using each array ,all do set to one
Like that,giving important sections only
...................
$data['template_data'] = $this->getTemplateData($template_id);
$data['variables_data'] = $this->getVariables($customer_id, $language_id);
$data['title'] = $variables_data[0]['value'];
$this->load->view('template', $data);
you can take $template_data and $variables_data in view files
Generally you pass in data as:
$data['template_data'] = $template_data;
$data['title'] = $$title;
....
$this->load->view('template', $data);

Categories