Codeigniter friendly url - php

I try to pass the parameters from controller to url.
I try to achieve something like this: http://localhost/management_system/Job/search_job?keyword=word&location=location
for now i only get http://localhost/management_system/Job/search_job
Dose somebody know how i can achieve that?
Below you can see my controller:
public function search_job(){
if ($this->input->post('submit')) {
$location = $this->input->post('location', TRUE);
$keyword = $this->input->post('keyword', TRUE);
$data['all_jobs'] = $this->Job_model->search_job($keyword, $location);
if ($data['all_jobs'] > 0 ) {
$data['categoryes'] = $this->Job_model->get_category();
$this->autoload('job_list', 'eJobs', $data);
}
}else{
redirect('Job/jobs');
}
}

In your action you are waiting for a POST request but this: http://localhost/management_system/Job/search_job?keyword=word&location=location is a GET request.
If your request is this: http://localhost/management_system/Job/search_job?keyword=word&location=location
and you want to get location and keyword you must write code like this:
public function search_job() {
if ($this->input->post('submit')) {
$location = $this->input->get('location', TRUE);
$keyword = $this->input->get('keyword', TRUE);
$data['all_jobs'] = $this->Job_model->search_job($keyword, $location);
if ($data['all_jobs'] > 0 ) {
$data['categoryes'] = $this->Job_model->get_category();
$this->autoload('job_list', 'eJobs', $data);
}
} else {
redirect('Job/jobs');
}
}
You need to change
$this->input->post()
to
$this->input->get()

Related

public variable cannot read by other function codeigniter

why my public variable cannot read by other function?
my public variable :
public $dataku = array();
first i set data to my public variable from model:
function cari() {
$penerima = $this->input->post('penerima');
$tanggal = $this->input->post('tanggal');
$tanggal2 = $this->input->post('tanggal2');
$id_komoditas = $this->input->post('id_komoditas');
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
$title = "Data ".$this->judul;
$subtitle = $this->url;
$content = $this->load->view('cari.php', $this->dataku, true);
$this->load_content($title, $subtitle, $content);
}
but, when i want to read the public variable data again, it hasnt a data :
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}
please, i need your help.
you need to access the class properties with $this. Change the following lines:
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
Also edit the following (you have an extra $):
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}

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.

How to get rid of unused $_GET parameters in URL with Codeigniter?

I am designing search functionality with some data with Codeigniter.
My search model looks like:
class Search_Model extends CI_Model {
public function get_results($search_terms = false) {
// Build query
foreach ($search_terms as $field => $value) {
$this->db->like("{$field}", $value);
}
// Execute query
$query = $this->db->get('exams');
// Return results
return $query->result_array();
}
}
And the search controller method looks like this:
public function results() {
$search_terms = array(
'first_name' => $this->input->get('first_name', TRUE),
'last_name' => $this->input->get('last_name', TRUE),
'exam_name' => $this->input->get("exam_name", TRUE)
);
$data['title'] = "Search Results";
$data['exams'] = $this->search_model->get_results($search_terms);
$this->load->view('templates/header.php', $data);
$this->load->view('exams/index.php', $data);
$this->load->view('templates/footer.php');
}
The search is working as it should but I want to clean up the URL which comes out something like this if I only search for "First Name":
search/results?first_name=tim&last_name=&exam_name=
How can I remove those extra parameters that are unused in the URL?
The quickest way is just to filter the $searh_terms array before passing it to the model.
$search_terms = array(
'first_name' => $this->input->get('first_name', TRUE),
'last_name' => $this->input->get('last_name', TRUE),
'exam_name' => $this->input->get("exam_name", TRUE)
);
// Remove empty search values
foreach ($search_terms as $key => $value) {
if (strlen($value) == 0) {
unset($search_terms[$key]);
}
}
include jquery in your header. then add this script.
<script>
$(document).ready(function(){
$('#myform').submit(function(event){
event.preventDefault();
var url = $(this).attr('action')+'?';
var first = false;
var second = false;
if($('input[name="first_name"]').val()){
url += 'first_name='+$('input[name="first_name"]').val();
first = true;
}
if($('input[name="last_name"]').val()){
if(first){
url += '&';
}
url += 'last_name='+$('input[name="last_name"]').val();
second = true;
}
if($('input[name="exam_name"]').val()){
if(second){
url += '&';
}
url += 'exam_name='+$('input[name="exam_name"]').val();
}
window.location.replace(url);
});
});
</script>
(i didnt ran this script yet, so let me know if u get any syntax error or somethign else.)
and finally change your results function to this,
public function results() {
$search_terms = array();
if(isset($_GET['first_name'])){
$search_terms['first_name'] = $this->input->get('first_name', TRUE);
}
if(isset($_GET['last_name'])){
$search_terms['last_name'] = $this->input->get('last_name', TRUE);
}
if(isset($_GET['exam_name'])){
$search_terms['exam_name'] = $this->input->get('exam_name', TRUE);
}
$data['title'] = "Search Results";
$data['exams'] = $this->search_model->get_results($search_terms);
$this->load->view('templates/header.php', $data);
$this->load->view('exams/index.php', $data);
$this->load->view('templates/footer.php');
}
You can try this:
$mode = array();
if(isset($_GET['first_name'])):
$mode[] = 'first_name='.$_GET['first_name'];
endif;
if(isset($_GET['last_name'])):
$mode[] = 'last_name='.$_GET['last_name'];
endif;
if(isset($_GET['exam_name'])):
$mode[] = 'exam_name='.$_GET['exam_name'];
endif;
//etc...
//Then reformulate link
if(!empty($mode)){
$link = '?' . implode('&',$mode);
}else{
$link = '';
}
redirect("form.php".$link);
just filter what you send...
foreach ($search_terms as $field => $value) {
if ($value <> ""){ //or the test you like to use...
$this->db->like("{$field}", $value);
}
}

how to use slug in codeigniter

i don't know how to make url like this
tkd/index.php/article/77/Kejurnas-2013
i am getting error with url like this
tkd/index.php/article/77/Kejurnas%202013
this is my controller
public function index($id = null, $slug = FALSE){
// Fetch the article
$this->db->where('pubdate <=', date('Y-m-d'));
$this->data['article'] = $this->mberita->get_by_id($id,$slug);
//echo '<pre>' . $this->db->last_query() . '</pre>';
// dump($this->data['article']);
// Return 404 if not found
count($this->data['article']) || show_404(uri_string());
// Redirect if slug was incorrect
$requested_slug = $this->uri->segment(3);
$set_slug = $this->data['article']->slug;
if ($requested_slug != $set_slug) {
redirect('article/' . $this->data['article']->id . '/' . $this->data['article']->slug, 'location', '301');
}
// Load view
/*add_meta_title($this->data['article']->judul_berita);*/
$this->data['contents'] = 'article';
$this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data);
}
and this is my modal
public function get_by_id($id = 0, $slug = FALSE)
{
if ($id === 0 && $slug === FALSE)
{
$query = $this->db->get('$this->tbl_berita');
return $query->result_array();
}
return $this->db->get_where($this->tbl_berita, array('id' => $id, 'slug' => $slug),1)->row();
}
please help me how to make the right url. thank you
You can use one of CodeIgniters helper functions url_title()
Takes a string as input and creates a human-friendly URL string. This
is useful if, for example, you have a blog in which you'd like to use
the title of your entries in the URL.
site_url("articles/{ID}/").url_title({TITLE});

How to count post view in codeigniter blog

I am new in codeigniter and php.
I want to count post view number and store it database.
That means, I want to create a popular post plugin by counting post view.
But i can't do this.
Please anyone help me. Tell the way.
Here is my article Controller..
public function __construct(){
parent::__construct();
$this->load->model('article_m');
}
public function index($category_slug, $id, $slug=NULL){
// Fetch the article
$this->db->where('pubdate <=', date('Y-m-d'));
$this->data['article'] = $this->article_m->get($id);
$this->data['articles'] = $this->article_m->get();
// Return 404 if not found
count($this->data['article']) || show_404(uri_string());
if ($this->uri->segment(1) !== $this->data['cat']->category_slug) {
echo "Hi There, This is wrong";
}
$this->load->view('templates/article', $this->data);
}
}
Here is my Models:
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
Sorry for my bad english..
$post_id = "???"
if(isset($_COOKIE['read_articles'])) {
//grab the JSON encoded data from the cookie and decode into PHP Array
$read_articles = json_decode($_COOKIE['read_articles'], true);
if(isset($read_articles[$post_id]) AND $read_articles[$post_id] == 1) {
//this post has already been read
} else {
//increment the post view count by 1 using update queries
$read_articles[$post_id] = 1;
//set the cookie again with the new article ID set to 1
setcookie("read_articles",json_encode($read_articles),time()+60*60*24);
}
} else {
//hasn't read an article in 24 hours?
//increment the post view count
$read_articles = Array();
$read_articles[$post_id] = 1;
setcookie("read_articles",json_encode($read_articles),time()+60*60*24);
}
star complex to do?
//function single post, query get by slug and id post
if(!isset($_SESSION['views'. $id]) || (isset($_SESSION['views'. $id]) && $_SESSION['views'. $id] != $id)){
$this->post_m->viewer($id); //load model viewer
$this->session->set_userdata('views'. $id, $id);
}
// Model "post_m"
function viewer($id)
{
$this->db->where('id', $id);
$this->db->set('views', 'views+1', FALSE);
$this->db->update('posts'); //table update
}
$id is id post

Categories