CodeIgniter encoding and decoding arguments in url - codeigniter

Ok i wanna encode arguments which i send to controler and function in this case number 1 i wanna use url_decode and encode function is it possible to do?
view page
Create
controler
public function page($selfi){
$this->db->select('*');
$this->db->from('photos');
$this->db->join('bridge', 'bridge.photos_id = photos.id');
$this->db->where('bridge.tags_id',$selfi);
$data['query'] = $this->db->get();
$this->load->view('gallery_view',$data);
}

I'm not sure why you're trying to do it this way, but here's how:
Create
And on the controller side:
public function page($selfi){
$selfi = url_decode($selfi);
$this->db->select('*');
$this->db->from('photos');
$this->db->join('bridge', 'bridge.photos_id = photos.id');
$this->db->where('bridge.tags_id',$selfi);
$data['query'] = $this->db->get();
$this->load->view('gallery_view',$data);
}

Related

why i can't call name from table food?

public function orderlist(Request $request){
$id = $request->id;
$data['order'] = Order::where('shop_id',$id)->orderBy('id')->get();
foreach($data['order'] as $orders){
$orders->shop = Shop::where('id',$orders->shop_id)->first();
$orders->food = Food::where('id',$orders->food_id)->get();
}
return $orders->food->name;
return view ('administrator.users.list_order.index',$data);
}
If you set up proper Eloquent relationships, this code could be rewritten like so:
public function orderlist(Request $request, Shop $shop){
$orders = $shop->orders()->with(['food'])->get();
return view ('administrator.users.list_order.index',compact('orders'));
}
Check out the docs here: https://laravel.com/docs/5.7/eloquent-relationships

How to access Model from Views in CodeIgniter

How to access Model from Views in Codeigniter.
I have a Model with a function, i need to call this function from view
Please Read the CI documentation First:
If You are using a MVC Structured Framework then you should be following the way it works.
You Need the Controller:
public function your_controller_function(){
// To load model
$this->load->model ('your_model');
//To Call model function form controller
$data = $this->your_model->model_function($ex_data);
//TO Send data to view
$this->load->view('your_view',['data'=>$data]);
}
Inside your model:
public function model_function($ex_data){
// Your Querys
// user return to send you query result to the controller anything
// Sample Example of query and return
$query = $this->db->select('*')
->where('your_column',$data['column_name'])
->get('your_table');
$your_result = $query->row_array();
if($this->db->affected_rows() > 0){
return your_result;
} else {
return FALSE;
}
}
Inside your view you can write :
<?php
$this->load->model('your_model');
$this->your_model->some_function();
?>
For example in User_controller load User Model $this->load->model('Users_model');
Then User View page $viewpage = this->Users_model->somefunction($id);

how to get values from model and display in controller

I am making a site in codeigniter, here is my controller, model and view:
controller:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
model:
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
view:
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
but i want to get $new_dbemail['name'] in my controller, how i can get the value of $new_dbemail['name'] in controller rather than view ???
To do so... you have to fetch data into a row_array not in result_array in Model.
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$database_field = $data['dbemail']['database_field']; //to get result in controller
$date['database_field'] = $data['dbemail']['database_field']; //to get result in view
$this->load->view('home', $data);
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
return $query->row_array();
}
View
<p> Database Field value : <?=$database_field;?></p>
You are Calling the Model In A Variable :-
$data['dbemail']=$this->login_model->email();
So in this you have the results which you are fetching from Database. Than you passing this variable data to your View.
$this->load->view('home', $data);
So you already have the value in controller which u fetched from model which is stored in $data.
You can View the Values of $data , using
echo "<pre>";
print_r($data);
and it will show what data you are receiving
Try like this...
In controller...
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();//array containing all emails
//For each loop here for displaying emails
foreach($dbemail as $email){
$mail = $email; //assigning to variable
echo $mail."<br/>";//displaying email
}
$data['dbemail'] = $dbemail;//for passing to view
$this->load->view('home', $data); //passing your value to view
}
First you need to fetch the name from the database in the model. Change the query line:
$query = $this->db->query("SELECT name, email FROM change_password");
Then to use the name data in the controller you could try this:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$name = $data['dbemail']['name']; // example usage to retrieve name in controller
$this->load->view('home', $data);
}
Now the name data is available as $name in controller.
Controller:
public function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemails']=$this->login_model->email();
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$this->db->select("email"); // the row of the table you want to select.
$this->db->get('change_password'); //table name. you can still use your query, its just much neat when you use a specific query builder.
$result = $this->result(); //giving you a result of type object
return $result;
}
View
foreach ( $dbemails as $dbemail )
{
echo $dbemail->email; // the '->' will be the identifier of which row you want to get or display, i assume its email beacause you specify it in the model.
}
Hope this what your looking for.

How to passing value from link in codeigniter

My link in view page
Detail
My controller
public function cari(){
//what i'm doing here ? please help ?
}
My model
function search_by_id($id){
$query = $this->db->where('id',$id);
$query = $this->db->get();
return $query->result();
}
How pass value from link to model and show the result to view again?
it's my first time using codeigniter, need some help
You have to pass argument into method:
public function cari($hasil_id){
if ((int)$hasil_id > 0)
{
$this->load->model('Name_of_model');
$data['search_by_id'] = $this->Name_of_model->search_by_id($hasil_id);
$this->load->view('hasil_view', $data);//assuming page for item
}
else
{
redirect('not_good_id_method', 'refresh');//in case of not valid id
}
}
In your controller ,receive the value passed through link by uri class.And then pass it into your model.
Controller :
public function cari()
{
$id= $this->uri->segment(3);
$this->model_name->search_by_id($id);
}
Take a look on this.

CodeIgniter invoke variable from another function in the same controller

I have a problem. I want to access to a variable from another function. let me explain.
Controller
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = 'BaD';
$data['records'] = $this->usuarios_model->obtener_grupos($plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}
Model
function obtener_grupos($plataforma, $grupo){
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where('Plataforma', $plataforma);
$this->db->where('grupos', $grupo);
$query = $this->db->get();
return $query->result();
}
I want to access to the variable platforma in the function obtener_oplataforma_usuarios from the function obtener_grupos I tried different things and I still have problems.
Can you help me to solve this issue. and I want to do that because I want to make a query with two validations one is group and the other platforma. My problem is the second one. I recibe the information of plataforma fine from a from this is in the function obtener_oplataforma_usuarios and is fine but I do not how to read this variable from the other function.
you lose minuses (-) in pointers :)
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
must be:
$data['records'] = $this->usuarios_model->obtener_plataformas_usuarios($plataforma)
You can use a private variable in your class.
Create the variable above your __construct function, private $plataforma
Set the default value in your __construct function
Set and get in your public function using $this->plataforma
Implemented in your code:
private $plataforma;
public function __construct() {
parent::__construct();
$this->plataforma = "";
}
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$this->plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($this->plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$data['records'] = $this->usuarios_model->obtener_grupos($this->plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}

Resources