Function within a controller is not working - php

I have a controller with Index function, Now i am trying to make a more fundtion for on scroll pagintion in codeignitor Controller.
This is my function
public function load_more($cat2,$cat1)
{
$group_no = $this->input->post('group_no');
$content_per_page = 12;
$start = ceil($group_no * $content_per_page);
$all_content = $this->product__model->get_all_content($start,$content_per_page,$cat2,$cat1);
if(isset($all_content) && is_array($all_content) && count($all_content)) :
foreach ($all_content as $key => $content) :
echo '<li>'.$content->id.'</li>';
echo '<p>'.$content->ITEM_CODE.'</p>';
endforeach;
endif;
}
Controller name is Product
When i check this fucntion at my url its hows page not found...
this is URL
http://www.dezaro.com/Product/load_more/160/2
How can i resolve it ???

Siddharth vyas The issue is with your URL you are hitting. Use product instead Product.
So Use http://www.dezaro.com/product/load_more/160/2 instead of
http://www.dezaro.com/Product/load_more/160/2
It will work for sure. Because it take the filename instead of Classname.

Try this
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
your route should be
$route['method/(:cat1)/(:cat2)'] = 'product/load_more/$1/$2';

It seemd you are facing issue of index.php
Please try using url:
http://www.dezaro.com/index.php/Product/load_more/160/2

Related

Changing logos of site based on class - PHP Wordpress

I have this code in my header.php to change the website's logo based on the page's slug:
$logo_img = 'default';
if (is_page('brookside')) {
$logo_img = 'brookside';
} elseif (is_page('hilltop')) {
$logo_img = 'hilltop';
} elseif (is_page('reserve')) {
$logo_img = 'reserve';
}
Which works great. However, the pages a need to change the logo are query string URL, so they don't have slugs. I was able to add a unique class to each page. My question is:
How do I translate that code to "if a page has "x" class? So, what function should I use instead of is_page?
Any help would be very appreciated!
Thanks!
You can use:
if($_GET['ct_additional_features'] == 'brookside') {
$logo_img = 'brookside';
}
You could use get_class that outputs the Class Name and there solve your if with what are you saying

cant get passed parameter by url in php

please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}

CodeIgniter pagination won't go past first page

I have a blog page of posts that I am trying to paginate using CodeIgniter. The numbering and limiting seem to be working fine, except I keep getting a 404 when I try to travel to another page.
The strange thing is the normal culprits that cause this issue are correct. The baseUrl and the uri_segment.
My controller looks like this:
$config = array();
$config["base_url"] = $this->config->site_url("/blog");
$config["total_rows"] = $this->blog_model->count();
$config["per_page"] = 2;
$config["uri_segment"] = 2;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->load->view('blog', array(
'user' => $this->user,
'blog' => $this->blog_model->loadPosts($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'footer' => $this->blog_model->loadFooter()
));
And then in my model I am grabbing the posts
public function loadPosts($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->order_by("date", "desc");
//this loads the contact info
$query = $this->db->get('entries');
return $query->result();
}
My full URL is www.mysite.com/blog and then with the pagination it appears as www.mysite.com/blog/2.
For the base_Url I have also tried base_url() . "/blog";.
And I have tried setting the uri_segment to 1 and 3, but nothing seems to work.
As well I have tried playing around with the routing and have added just to see if it would do anything:
$route['blog/(:num)'] = 'blog/$1';
You can use this line of code if your code is inside the index method:
$route['blog/:any'] = "blog/index/$1";
Because you have used the segment(2), and you should change the blog/index/$1 to blog/:any.
Assuming the function name that contain your pagination code is index(), you should change the route to:
$route['blog/(:num)'] = 'blog/index/$1';
And in your index() function, add the $page parameter:
public function index($page = 1){
...
With your routes, try if you can to add as many :anys or :nums after as you like:
$route['blog'] = 'blog/index'; // For the first level
$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; // For extra "uri" segments.
// base_url pagination
$config["base_url"] = base_url("blog"); // Is preferred
You can keep your other code as it is. Just add this at your routes.php file:
$route['blog/:num'] = "blog/index";
//Assumes your code is inside the index method.
//If not, use this way:
//$route['blog/:num'] = "blog/YOUR_METHOD_NAME";
You can't pass a parameter to the index() function of a controller like it is a random function.
If you try to do controller/var instead of controller/function/var, CodeIgniter will search for a function var() inside the controller which does not exists. That's what happen when you try to access blog/2: 2() isn't a function in your controller.
You can create a new function in your controller, let's say page(), and move your code inside. That way, you will call blog/page/2. Function page() will exists and will not get a 404. Also don't fordet to redefine your base_url for pagination.
$config["base_url"] = site_url("blog/page");
Another solution if you absolutely need the URL like /blog/2, routes:
$route['blog/(:any)'] = 'blog/index/$1';
Remap may also be a solution: http://www.codeigniter.com/user_guide/general/controllers.html#remapping

CodeIgniter URL issue

I am having difficulty getting the correct URL when I call a method to load a view.
Heres my controller:
public function post() {
$title = $this->input->post('title');
$data = $this->p_Model->post($title);
$this->qs($data);
}
public function qs($id){
$title = $this->s_Model->getTitle($id);
$result = $title->result();
$this->load->view('q_View', array('results' => $result));
}
Heres my view:(note this view is not the view which gets loaded from the qs function, but one which calls the qs function)
<html>
<body>
<table>
<?php
if (isset($qs)) {
foreach ($qs as $row) {
$id = $row->qID;
echo '<a href="'.site_url('myController/qs/'.$id).'">';
echo $row->title;
echo "<br>";
}
}
?>
</table>
</body>
</html>
So in my controller I have two functions, the qs function works separately by itself so can be called in the view and give the following url myController/qs/1 however when I use the post function I get a url like this myController/post so my question is how can I get my url to be like the first example?
Instead of using the line:
$this->qs($data);
You can use a redirect:
redirect('/mainController/qs/'.$data);
That should work in the same way that you have used in your view
Try base_url and also you can use current_url() returns the full URL (including segments) of the page being currently viewed.
echo '<a href="'.base_url('myController/qs/'.$id).'">';

creating back page links in Codeigniter

I have a page with URL http://arslan/admin/category/index/0/name/asc/10 in Codeigniter.
In this URL, the uri_segment start from 0. This (0) is the default search value, name and asc are the default sort field and order, and 10 is the pagination index.
Now if I move to an add page with URL (http://arslan/admin/category/add/)
similarly like above "add" is the current function.
Now if i want to go back through a link to back page... How can I divert the user back? I can't make the URL go back.
Can somebody help me please?
I am not sure if i understand the question correctly, if not please ignore my answer, but I think you want a link to "go back to previous page", similar to the back-button in a web browser.
If so you could use javascript to solve this by simply using this line:
Go back
I extend the session class by creating /application/libaries/MY_Session.php
class MY_Session extends CI_Session {
function __construct() {
parent::__construct();
$this->tracker();
}
function tracker() {
$this->CI->load->helper('url');
$tracker =& $this->userdata('_tracker');
if( !IS_AJAX ) {
$tracker[] = array(
'uri' => $this->CI->uri->uri_string(),
'ruri' => $this->CI->uri->ruri_string(),
'timestamp' => time()
);
}
$this->set_userdata( '_tracker', $tracker );
}
function last_page( $offset = 0, $key = 'uri' ) {
if( !( $history = $this->userdata('_tracker') ) ) {
return $this->config->item('base_url');
}
$history = array_reverse($history);
if( isset( $history[$offset][$key] ) ) {
return $history[$offset][$key];
} else {
return $this->config->item('base_url');
}
}
}
And then to retrieve the URL of the last page visited you call
$this->session->last_page();
And you can increase the offset and type of information returned etc too
$this->session->last_page(1); // page before last
$this->session->last_page(2); // 3 pages ago
The function doesn't add pages called using Ajax to the tracker but you can easily remove the if( !IS_AJAX ) bit to make it do so.
Edit:
If you run to the error Undefined constant IS_AJAX, assumed IS_AJAX
add the line below to /application/config/constants.php
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
There are two ways to solve your problem: First you could place a link that is using the javascript back-function onclick, like this ...
go back
... or you always save the current full page url into a cookie and use that for generating the back link - a helper could look like this (not tested) ...
/**
* save url to cookie
*/
if(!function_exists('urlhistory_save'))
{
function urlhistory_save()
{
$CI =& get_instance();
$CI->load->library('session');
$array = array(
'oldUrl' = $CI->session->userdata('newurl'),
'newurl' = $CI->uri->uri_string()
);
$CI->session->set_userdata($array);
}
}
/**
* get old url from cookie
*/
if(!function_exists('urlhistory_get'))
{
function urlhistory_get()
{
$CI =& get_instance();
$CI->load->library('session');
return $CI->session->userdata('oldurl');
}
}
In your controller you would use urlhistory_save() to save the current URL and in the view youd could use urlhistory_get() to retreive the old address like this:
<a href="<?php echo base_url().urlhistory_get(); ?>go back</a>
The most simplest way to redirect to your previous page , try this it work for me
redirect($this->agent->referrer());
you need to import user_agent library too $this->load->library('user_agent');
You can create a Session to go to back page as:
$this->session->set_userdata('ses_back_jobs','controller
name'.array_pop(explode('controller name',$this->input->server('REQUEST_URI'),2))); //Back page
Then if u want to redirect to some page use it:
redirect($this->session->userdata('ses_back_jobs'));
or use it to the anchor.

Categories