I have a problem in codeigniter.
I call a funtion in controller Articles from the view.
<li><a href="index.php/articles/Category/1" ><?php echo $item['CATEGORY'] ?></a></li>
The function
function getCategories() {
$this->db->select('ID_CAT,CATEGORY');
$this->db->from('CATEGORY');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function Category($id_cat) {
if (!$id_cat) {
$id_cat = 0;
}
$this->load->model('graf/home_model');
$data = array();
$data['categories'] = $this->home_model->getCategories();
$filtro_cat = $id_cat;
$data['article'] = $this->home_model->getArticles($filtro_cat);
$this->load->view('graf/articles', $data, TRUE);
}
home_model
function getArticles($filtro_cat) {
$this->db->select('*');
$this->db->from('ARTICLES');
if ($filtro_cat != 0) {
$this->db->where("ID_CAT=".$filtro_cat);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
When i select the button the url changes to http://localhost/Graf/index.php/articles/Category/1 but does not load
http://localhost/Graf/index.php/articles/
Why?
When you set third argument of load->view() to true it returns string value and does not output anything
Try changing:
$this->load->view('graf/articles', $data, TRUE);
to
$this->load->view('graf/articles', $data);
set the routes.php in folder config, add this code;
$route['articles/Category/(.*)'] = 'articles/$1';
and change the link
<li><a href="<?php echo site_url('index.php/articles/'.$item['ID_CAT']); ?>" ><?php echo $item['CATEGORY'] ?></a></li>
and change your controller to this
$this->load->view('graf/articles', $data);
Related
I want to create pagination in a search bar in CodeIgniter, but I don't get actual pagination.
Controller code:
public function compatabilty(){
$this->load->model('user_model');
$this->load->model('admin_model');
$data['result'] = $this->user_model->get_compatibility();
$this->load->view('compatabilty',$data);
}
public function compatabilty_search()
{
$submit = $this->input->post('searchcompatible');
if(isset($submit)){
$new = $this->input->post('search_text');
$datas['result'] =$this->admin_model->fetch_data($new);
$this->load->view('compatabilty_search',$datas);
}
}
Model:
function fetch_data($new)
{
$this->db->select("*");
$this->db->from("au_compatible_products");
if($new != '')
{
$this->db->like('Device', $new);
}
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result();
}
View:
<div class="search-result" >
<ul><font>Serch Results for</font>
<?php foreach ($result as $key ) { ?>
<li><?php echo $key->Model_Number;?> </li>
<?php } ?>
</ul>
</div>
I am listing the registered employee from the db and showing 10 employee per page. I'm using codeigniter pagination for the purpose.
If I deleted an employee in page 2, after deleting I need to go to page 2 showing rest of the employees in page 2.
View:
<a href="<?php echo $path;?>welcome/delete_employee/employeeid/<?php echo $row->empID;?>/pageNum/<?php echo $currentPage; ?>" onClick="return delAlert()";><img src="<?php echo $path;?>/img/delete.png" height="30px" width="30px" /></a>
Controller:
public function delete_employee()
{
$session_id = $this->session->userdata('logged_in');
if($session_id) {
$array = $this->uri->uri_to_assoc(3);
$count=$array['pageNum']-1;
$i=$count*10;
$this->load->model('welcomemodel','m',true);
$this->m->deleteemployee($array['employeeid']);
$config = array();
$config["base_url"] = base_url() . "welcome/employee";
$config["total_rows"] =$this->m->employee_count();
$config["per_page"] = 10;
$config["uri_segment"] = $array['pageNum'];
$data['showData'] = $this->m->getEmployee($config["per_page"], $i);
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data["currentPage"] =$array['pageNum'];
$this->load->view('header');
$this->load->view('employee',$data);
} else {
$this->load->view('session_expired');
}
}
Model:
public function deleteemployee($employeeid)
{
$this->db->where('empID',$employeeid);
$this->db->delete('employee');
return $this->db->affected_rows();
}
public function getEmployee($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select()
->from('employee')
->order_by('emp_fname');
$this->db->join('service', 'employee.serviceID = service.serviceID','left');
$query=$this->db->get();
return $query;
}
public function employee_count()
{
return $this->db->count_all("employee");
}
But now the pagination link shows first page's link,even though contents of second page is displayed....
model method
public function employee_count()
{
$result = $this->db->get('employee');
if ($result->num_rows() > 0) {
//employee record available
return true;
}
else
{
// employee record not available
return false;
}
}
View
<?php
$getData = $this->[your model name]->getEmployee($limit, $start);
?>
<?php if ($getData): ?>
//Your Pagination Part Here.....
<?php else: ?>
// else Nothing
<?php endif ?>
i am trying to create a readmore function in codeigniter where the readmore link will be linked to a controller which would show all the data about that particular id....i am kind of confused on how to go about it... i tried...
<?php
$research_detail_url = site_url()."/research/research_details";
//echo json_encode($research)
if($research)
{
foreach ($research as $_research) {
$author = $_research->author;
$content = $_research->content;
$dsubmitted = $_research->dsubmitted;
echo "<div class='menu-collapse'> <h5>$author</h5>";
echo "<p>";
echo "<span class='support_text'>$content <span><br />";
echo "<span class='support_text'>$dsubmitted <span><br />";
echo "<a href='$research_detail_url' target='_blank' style='text-decoration:underline; color:#0088cc;'>
read more ยป </a>";
echo "</p> </div>";
}
}
?>
but i seem not be getting any results...i need help...
and this is my controller function.....
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->projects_model->get_project($id);
if($_result)
{// success in fetching data hurray
$result['projects'] = $_result;
$users_ids = $this->users_model->get_user_ids(); //return available user id's
$groups_ids = $this->groups_model->get_group_ids(); //return available group id's
//echo json_encode($users_ids);
//echo json_encode($groups_ids);
$group_record = $this->map_names_to_ids($users_ids , $groups_ids );
$result['group_record'] = $group_record;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('projects/project_panel', $result);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}
and this is my model.....
<?php
class research_model extends CI_Model {
function add()
{
$this->db->insert('research',$_POST);
if($this->db->_error_number())
{
return $this->db->_error_number();
}
}
function update($article_id, $data_fields = NULL){
if($data_fields == NULL)
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$_POST);
}
else
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$data_fields);
}
$is_error = $this->db->_error_number();
if($is_error){
echo $is_error;
}
return TRUE;
}
function delete($id){
$this->db->where("article_id =".$id);
return $this->db->delete('research');
}
//return the research with this id
function get_research($id){
$this->db->where("article_id =".$id);
$query = $this->db->get('research');
if ($query->num_rows() > 0){
return $query->row_array();
}
else
echo $this->db->_error_message();
return FALSE;
}
//return the available research in the table
function get_research_all(){
$query = $this->db->get('research');
if ($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$result[] = $row;
}
return $result;
}
}
}
and my entire controller.....
<?php
class Research extends Public_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('research_model');
}
function index()
{
if($this->ion_auth->is_admin())
{
$result = $this->research_model->get_research_all();
$data = array(
'main_content' => 'research/index',
'research' => $result
);
$this->load->view("loader", $data);
}
else
{
redirect('home');
}
}//END INDEX
// public view
function current()
{
$result = $this->research_model->get_research_all();
$data = array('research' => $result);
$this->load->view('__includes__/header');
$this->load->view('__includes__/navbar');
$this->load->view('research/current', $data);
$this->load->view('__includes__/footer');
}
function add()
{
if($this->ion_auth->is_admin())
{
$this->load->view("loader",array('main_content'=>"research/add_research"));
}
}//END ADD
function edit($id='')
{
if(! $id)
{
echo "research Id required";
return;
}
$result = $this->research_model->get_research($id);
if( ! $result)
{
echo "Nothing to edit";
return;
}
$result['main_content'] = "research/add_research";
$this->load->view("loader",$result);
}//END EDIT
function delete($id='')
{
if(! $id)
{
echo "Id required";
return;
}
$this->research_model->delete($id);
$this->get_research();
}//END DELETE
function submit($id='')
{
//validate form [perform validation server-side to make sure of fields]
$this->load->library('form_validation');
$this->form_validation->set_rules('author', 'Author', 'trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//ajax data array
$data = array(
'server_validation' => validation_errors()
);
echo str_replace('\\/', '/', json_encode($data));
}
else{
if($id){
$result = $this->research_model->update($id);
$content = "article has been UPDATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
else{
$result = $this->research_model->add();
$content = "article has been CREATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
//if duplicate key
if($result == 1062){
//ajax data array
$data = array();
$data['is_valid'] = 0;
echo json_encode($data);
}else{
//ajax data array
$data = array(
'is_valid' => 1,
'content' => $content
);
echo json_encode($data);
}
}//end ELSE form valid
}//END SUBMIT
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->research_model->get_research($id);
if($_result)
{// success in fetching data hurray
$result['article'] = $_result;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('research/research_details', $Aresult);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}//END EDIT
}
?>
my public controller
<?php
abstract class Public_Controller extends CI_Controller
{
public $about_data;
function __construct()
{
parent::__construct();
//Making This variable availale for the whole site
$this->load->model('about_model');
$this->load->model('captcha_model');
//get your data
$this->about_data = $this->about_model->get_abouts();
}
}
?>
I see a couple of problems:
In your view, you are not closing the span tags. You need
</span>
In your view, you are creating the link, but you are not
including an ID, which you need in your controller. What I mean by this is the following:
First,you create this $research_detail_url = site_url()."/research/research_details";.
Then echo "<a href='$research_detail_url' target='_blank' style=''>read more</a>";
As you can see, your URL does not have an ID when created. What you should do is something like this: $research_detail_url = site_url()."/research/research_details/31"; where 31 is a random ID. You need to come up with the right ID needed in order to pass it to your controller. Your controller is currently assigning a blank ID since none is being passed which is why you end up with a result of "Project Id required"
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);
}
I am working on a codeigniter site, in which a page is built using multiple views, I have come to a point where I need to work with a variable that is being set in another view is it possible to access it from another view?
This is code I have
Controller:
public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a bang one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image['special'] = $query;
} elseif(!isset($image['special']) && !isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_mysite_background()) {
$image['generic'] = $query;
}
}
if(isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_user_backgrounds($this->session->userdata['user_id'])) {
$image['user_background'] = $query;
}
}
$data = array_merge($data, $image);
$this->load->view('home/main_page.php', array_merge($data, $image));
}
Model:
public function get_mysite_background() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_group_group_id', 1);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query->result_array();
}
public function get_user_backgrounds($user_id) {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_id', $user_id);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
The Views in Question: Firstly where the variable is getting set,
</div>
<div id="background-select">
<?php
$count = 0;
if(isset($special)) {
foreach ($special as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($generic)) {
foreach ($generic as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($user_background)) {
foreach ($user_background as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
?>
And where I want to use the variable:
<body>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>
The variable I looking to use $background
You can use the following helper class to transfer the values from one view to another:
class VarStore {
private static $values = array();
public static function get($key, $default = NULL) {
return (isset(self::$values[$key])? self::$values[$key] : $default);
}
public static function set($key, $value) {
self::$values[$key] = $value;
}
}
Call VarStore::set('background', $background); in the first view and $background = VarStore::get('background'); in the second.
Most of the problems in programming are conceptual and of understanding... The point is a View is to VIEW the data and not calculate it. calculations are carried out in models whose functions can be called by multiple modules when required. So better is before a view is called work-out all the data it has to display and use the view for VIEWING only not callucations... I hope this change of paradigm will help