Having Problem with $_get in Codeigniter - codeigniter

I have a page called list.php which retrieves from database and shows all the student names with their respective ids in a list. In raw php, this is how I have done this-
include("connect.php");
$query = "SELECT * FROM marks ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$studentname = mysql_result($result,$i,"studentname");
$studentid = mysql_result($result,$i,"studentid");
?>
And then---
<? echo $studentname ?>
<?
++$i; } } else { echo "No Record Found"; }
?>
When a user clicks on any of the student names, it takes the user to the page of that particular student and in that page I have a code like following-
include("connect.php");
$number = $_GET['studentid'];
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
The above code is working just fine. Now as far as I know $_get is disable in Codeigniter. But how to do the exact same thing that I mentioned above in codeigniter if $_get is disabled ? I went through some tutorials on alternative way of using $_get, but I didn't understand those well. Would you please kindly help? Thanks in Advance :)

The usage of $_GET is discouraged in CI.
The simpliest way to rewrite that files is not using $_GET. Just add method='post' to your forms and tell your ajax requests to use post, if you have any. Use $_POST in PHP instead of $_GET.
If you are absolutely sure you need get requests passing parameters, you have to enable query strings. You can do it in your CI's config file:
$config['enable_query_strings'] = TRUE;
See section 'Enabling query strings' for details. But enabling query strings will cause Url helper and other helpers that generate URLs to malfunction.
So my suggestion is to use POST requests.
UPDATE Replace
<? echo $studentname ?>
With
<? echo $studentname ?>
Create method studentprofile:
<?php
class Yourcontroller extends CI_Controller {
public function studentprofile($id)
{
include("connect.php");
$number = $id;
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
// and so on...
}
}
?>

Related

How do you extract only urls from mysql query in Codeigniter?

This is how my query looks like the Controller :
public function index(){
$this->load->library('lib_pagination');
$pg_config['sql'] = "SELECT * from SjohlLBzads_products JOIN SjohlLBzads_products_meta ON SjohlLBzads_products.post_id = SjohlLBzads_products_meta.post_id ";
$pg_config['per_page'] = 50;
$data = $this->lib_pagination->create_pagination($pg_config);
$this->load->view("product_listing", $data);
}
This is how how I display it in the view (product_listing.php)
<?php echo $data->gallery; ?>
The problem is the results I got from it not sorted out and contain mix of url and text :
a:6:{i:0;s:159:"https://ae01.alicdn.com/kf/HTB1AHOBOVXXXXazXpXXq6xXFXXXf/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:1;s:159:"https://ae01.alicdn.com/kf/HTB1ov9LOVXXXXbMXXXXq6xXFXXXl/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:2;s:159:"https://ae01.alicdn.com/kf/HTB1e4eHOVXXXXcbXXXXq6xXFXXXG/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:3;s:159:"https://ae01.alicdn.com/kf/HTB10qQ2RVXXXXbfXpXXq6xXFXXX5/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:4;s:159:"https://ae01.alicdn.com/kf/HTB164c0RVXXXXaSXpXXq6xXFXXXF/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:5;s:159:"https://ae01.alicdn.com/kf/HTB1OvumOVXXXXcqXFXXq6xXFXXXs/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";}
Is there any way that I can extract those 4 urls and call/display them separately ?
Hope this will help you :
Use unserialize to decode $data->gallery like this
$string = 'a:6:{i:0;s:159:"https://ae01.alicdn.com/kf/HTB1AHOBOVXXXXazXpXXq6xXFXXXf/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:1;s:159:"https://ae01.alicdn.com/kf/HTB1ov9LOVXXXXbMXXXXq6xXFXXXl/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:2;s:159:"https://ae01.alicdn.com/kf/HTB1e4eHOVXXXXcbXXXXq6xXFXXXG/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:3;s:159:"https://ae01.alicdn.com/kf/HTB10qQ2RVXXXXbfXpXXq6xXFXXX5/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:4;s:159:"https://ae01.alicdn.com/kf/HTB164c0RVXXXXaSXpXXq6xXFXXXF/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:5;s:159:"https://ae01.alicdn.com/kf/HTB1OvumOVXXXXcqXFXXq6xXFXXXs/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";}';
/*Replace $data->gallery with $string*/
$arr = unserialize($string);
print_r($arr);
Working example : https://eval.in/1000844
For more : http://php.net/manual/en/function.unserialize.php

Codeigniter route page using value fetch from database

currently I have a page like the following -
abc.com/controller/action/23
Here 23 is Item id,which is dynamic. I have to fetch the name of the item using the id from database and route to a new url like following -
abc.com/controller/p/23-itemname.
How can I do that?
what you can do is when the user visits this page:
abc.com/controller/action/23
You put this code into the controller
CONTROLLER
function action()
{
$id = $this->uri->segment(3); //id is now 23, this is the third uri
$data['details'] = $this->YOUR_MODEL->get_details($id);
foreach($data['details'] as $d)
{
$itemname = $d->YOUR_TABLE_COLUMN_NAME;
}
redirect(base_url(). 'controller/p/' . $id . '-' . $itemname);
//line produces abc.com/controller/p/23-itemname
}
MODEL
function get_details($id)
{
$this->db->select('*');
$this->db->from('YOUR_TABLE_NAME');
$this->db->where('YOUR_ID_COLUMN_NAME',$id);
$query = $this->db->get();
return $query->result();
}
Just comment if you need more clarifications.
Goodluck meyt
EDIT
Since you have action and you redirect to function p, here is what will happen.
function p()
{
$explodethis = $this->uri->segment(3); //Contains "23-itemname"
$pieces = explode("-", $explodethis); //Separate by the dash
$id = $pieces[0]; //Contains "23"
$itemname = $pieces[1]; //Contains "itemname"
$data['details'] = $this->YOUR_MODEL->YOUR_FUNCTION(YOUR_PARAMETERS)
$this->load->view('YOUR_VIEW',$data);
}
Routing not required to do this. You may generate link by item name
<a href="<?php echo site_url('controller/action').'/'.$val['item_id'].'-'.$val['item_name']; ?>"
and now in controller explode this title to get ID of the item and then execute your query
$title = explode('-','23-item-name');
$id = trim($title[0]);//this is your item ID

Codeigniter pagination links dosnt work after the first page

i have a search field and i want to show the results with pagination.
every thing works well in first result page but in other pages there is noting to show without any errors and when back to first page also there is noting.
i putted the posted key in session to avoid losing it.
but still noting.
this is my controller:
public function search()
{
$this->session->set_userdata('searched',$this->input->post('searchterm'));
$searchterm = $this->session->userdata('searched');
$limit = ($this->uri->segment(3) > 0)?$this->uri->segment(3):0;
$config['base_url'] = base_url() . 'home/search';
$config['total_rows'] = $this->home_model->search_record_count($searchterm,$language);
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['display_pages'] = TRUE;
$choice = $config['total_rows']/$config['per_page'];
$config['num_links'] = 3;
$this->pagination->initialize($config);
$data['results'] = $this->home_model->search($searchterm,$limit,$language);
$data['links'] = $this->pagination->create_links();
$data['searchterm'] = $searchterm;
$data['total']= $this->home_model->search_record_count($searchterm,$language);
putHeader();
putTop();
putTopmenu();
putSearch($data);
putFooter();
}
this is my model:
public function search_record_count($searchterm)
{
$sql = "SELECT COUNT(*) As cnt FROM content WHERE body LIKE '%" . $searchterm . "%'";
$q = $this->db->query($sql);
$row = $q->row();
return $row->cnt;
}
public function search($searchterm,$limit)
{
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','noor_content.category = categories.id')
->like('noor_content.title', $searchterm)
->like('noor_content.body', $searchterm)
->limit("5")
->order_by("content.id","DESC")
->get();
if($data->num_rows() > 0)
{
return $data->result();
//print_r($data);exit();
}
else
{
return 0;
}
}
The problem is that you are not limiting your query according to the pagination. In your controller you are getting the 3rd URL segment
$limit = ($this->uri->segment(3) > 0)?$this->uri->segment(3):0;
And passing it to you model but then you are not doing anything with it. In your model you want to do something like
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','noor_content.category = categories.id')
->like('noor_content.title', $searchterm)
->like('noor_content.body', $searchterm)
->limit("10, " . $limit) //Edited this line
->order_by("content.id","DESC")
->get();
i think you should use method = 'get' instead of post... i mean some thing like this ..
<form method="get" action="your_controller/your_function">
//your search form
</form>
by using get instead of post u dont have to use any session variables and u can easily access them in your controller as
$this->input->get('feild_name');
moreover u wont have problem with second page or any other page in pagination as all the search feilds get attached in the url and u can access it easily using $this->input->get('feild_name'); untill your url is changes to something else.. i will aslo save u al ot of coding ..

Display category name in template

I am trying to display a category name in a module position.
I tried:
<?php echo $listing['Category']['title'];?>
It did not work.
I followed this link, but it shows the article title and I need the category one.
I'm working on Joomla 1.7.
Much more simple answer:
<?php echo $this->escape($this->item->category_title);?>
As per the posters comment in the OP:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult();
echo $category;
?>
Travega is really close, his code works on pages, but not on category pages.
When you use $id = JRequest::getString('id'); on a category page (such as a category blog or list page) the id of the category is returned. This means we need more context of the id variable, in this case the 'view'.
Here is my modified version of travega's code:
function getCategoryName() {
//Modified from: http://stackoverflow.com/questions/8928967/joomla-display-catagory-name-in-template
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$view = JRequest::getString('view');
if ($view == 'category') {
$sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
} else {
$sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
}
$db->setQuery($sql);
$category = $db->loadResult();
return $category;
}
Other relevant info:
I've only tested this on Joomla 2.5.3 on cat blog and cat list pages. I've not tested it on anything other than com_content component. This means it probably won't work on weblink, contact, etc pages as you may again loose the context.

How to display Serial Numbers in a table that has been created using table library in codeigniter?

I know how to create pagination and generate a table containing information from database in codeigniter, but I don't how to display the serial numbers for each row in the table. Example:
SL. Name Email
1. Srijon example#yahoo.com
2. Jake jake#yahoo.com
Would you please kindly show me how to do that? Thanks in advance
Here is the controller for how I create pagination and generate table (without serial numbers)
function index(){
$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('Student ID','Student Name','Batch','Edit','Delete');
$config['base_url'] = 'http://localhost/coaching/index.php/student_list/index';
$config['total_rows'] = $this->db->get('student')->num_rows();
$config['per_page'] = 15;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['tab'] = "Student List";
$this->load->model('mod_studentlist');
$data['records']= $this->mod_studentlist->student_list();
$data['main_content']='studentlist';
$this->load->view('includes/template',$data);
}
Here's my model
function student_list()
{
$config['per_page'] = 10;
$this->db->select('studentid, studentname, batch');
$this->db->order_by("studentid", "desc");
$rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array();
foreach ($rows as $count => $row)
{
$rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update');
$rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete');
}
return $rows;
}
Here's my view file
<?php echo $this->table->generate($records);?>
<?php echo $this->pagination->create_links();?>
<script type="text/javascript" charset="utf-8">
$('tr:odd').css('background','#EAEAEA');
</script>
You should create a variable inside your model function:
<?php
function student_list()
{
$config['per_page'] = 10;
$this->db->select('studentid, studentname, batch');
$this->db->order_by("studentid", "desc");
$rows = $this->db->get('student',$config['per_page'],$this->uri->segment(3))->result_array();
$sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0
foreach ($rows as $count => $row)
{
array_unshift($rows[$count], $sl.'.');
$sl = $sl + 1;
$rows[$count]['studentname'] = anchor('student_list/get/'.$row['studentid'],$row['studentname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['studentid'],'Update');
$rows[$count]['Delete'] = anchor('report/'.$row['studentid'],'Delete');
}
return $rows;
}
and modify your controller:
<?php
function index() {
$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('SL.', 'Student ID','Student Name','Batch','Edit','Delete');
....
Codeigniter has a great Table library to display data in html table format. Adding a Serial No is sometime required to a table, but Row num is not easily provided by MySQL hence while using Table library this function is added to do the job.
Here goes the code:
/**
* Set the serial no
*
* Add serial no to left of table
*
* #access public
* #param mixed
* #return mixed
*/
function add_sr_no(&$arr)
{ if (!is_array($arr)) return $arr;
$i=1;
foreach ($arr as &$values) {
if (!is_array($values))
break;
else
array_unshift($values, $i++);
}
return $arr;
}
Just add these lines to table.php in library.
Usage:
$rows = $query->result_array();
$this->table->add_sr_no($rows);
Don't forget to add first column 'Sr No' in set_heading.
Read the post here.
Hope this helps.
Have you thought about doing this on the client? the javascript library datatables is brilliant. it can handle the sorting for you at the user side, and can be made to dynamically retrieve information using ajax, so load times are fast on large tables. for under < 100 rows or so there probably isn't a need though. i have it sorting/filtering tables with >100 000 rows with no problems at all (using ajax)
unless the serial numbers have semantic meaning don't show them to the client. it just more stuff for them to filter. if they are required for your code, but not for the user to see, just hide them away in a data attribute or something.

Resources