How do you extract only urls from mysql query in Codeigniter? - 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

Related

Get data from database with condition and show it in a view

This is the data that I have in database
This is what I want to make in the view.blade.php
What I want to do is I want to get the data from the database, if the data inside the column is 1, I want to get the column name as you can see in image 2, but there could be more than 1 column name because the column with data can be column A,B,C... etc.. and I want to show the student name and the subject (a,b,c... etc) if the data in it is '1' in the view. I stuck on how to get all those subject A,B,C.. this is the code that I have written, but it is incomplete because I don't know what to add on it to make it as what I have mentioned above. Hopefully, someone can help me. Thanks in advance
if($row->'A'=='1'){i dont know what should i put here so that i cant get the column name 'A' and print it in view.blade.php}
Assuming your table in database is student_details, create an eloquent model StudentDetail inside app/models/StudentDetail.php :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class StudentDetail extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'student_details';
/**
* Get subjects in the view
*
* #return string
*/
public function getSubjects()
{
$subjects = [];
$subjects[] = $this->A == 1 ? 'A' : null;
$subjects[] = $this->B == 1 ? 'B' : null;
$subjects[] = $this->C == 1 ? 'C' : null;
$subjects = array_filter($subjects);
return implode(',', $subjects);
}
}
Then you can retrieve data in your controller :
public function view()
{
$studentDetails = StudentDetail::get();
return view('view.path', compact('studentDetails'));
}
And inside view you can do :
#foreach($studentDetails as $detail)
{{ $detail->name }} : {{ $detail->getSubjects() }}
#endforeach
You can use appended property as well, I have not used that because appended property is added every-time when model is instantiated. I believe having it in a function makes it flexible to use as and when needed.
Okay i tried it bit different way it's not proper but it will give you desired output you want :-
Route::get('/test',function(){
$query = TestTable::all();
$calculateData = [];
foreach ($query as $key){
$subjects = '';
if($key->A === 1){
$subjects .= 'A';
}
if($key->B === 1){
$subjects .= 'B';
}
if($key->C === 1){
$subjects .= 'C';
}
$calculateData[] = [$key->name,$subjects];
}
foreach ($calculateData as $key){
dump("NAME : " . $key[0]."Subject : " . $key[1]);
}
dd("STOP");
})->name('test');

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 ..

Codeigniter variable not found message in sub views

I have stacked with the situation , please anyone help me !
Controller ->
public function index() {
$data = array();
$data ['tittle'] = 'Membership Payment';
$data ['page'] = $this->load->view('membership_payment', $data, true);
$data ['breadcrumb'] = $this->load->view('breadcrumb', $data, true);
$data['result'] = $this->payment_model->membership_payment();
$data['db']='---------------';
$this->load->view('master', $data);
View->master page
<?php echo $db; ?>
Master pages working fine , but problem is i have subpages membership_payment , in this page the data array variable are not working ,
Display the variable not found , there is any trick to pass data array to master subpage
Thanks in Advance !
ROB
I'm assuming that you've used $result which is the returned data of membership_payment(); method in the membership_payment view file.
If so, just change the order of the following line and put that over the $data['page'], as follows:
// ...
$data['result'] = $this->payment_model->membership_payment();
$data ['page'] = $this->load->view('membership_payment', $data, true);
// ...

Having Problem with $_get in 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...
}
}
?>

Resources