Pass an id through the url in php codeigniter - php

I am trying to pass the page id through the url Here is my code,problem is i will not get the required page id to the page only get the path . Please help.
View Page
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
Controller Page
public function view()
{
echo $page_id =$this->uri->segment(1);
$this->load->helper('url');
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel1_offers.php');
}
public function view1()
{
echo $page_id =$this->uri->segment(2);
$this->load->helper('url');
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel2_offers.php');
}

Change this:
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
into this:
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
Add $this->uri->segment('segment_number') in your controller to get the value from url
public function view()
{
$page_id =$this->uri->segment(3);
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel1_offers.php');
//I put your codes here because you want Spoon Feed
}
public function view1()
{
$page_id =$this->uri->segment(3);
...//rest of your codes here
}

You can pass as url parameter
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
And can access like this in controller
public function view($page_id)
{
echo $page_id;
}
you can load helper in controller constructor
function __construct() {
$this->load->helper('url');
}

Related

Route [report.expAlert] not defined

Route report.expAlert error
(View: /home/grocersa/edesimarket.com/resources/views/layout/main.blade.php
(i defined all place but not triggered plz help to find out)
This is my web.php file coding
Route::get('report/product_quantity_alert', 'ReportController#productQuantityAlert')->name('report.qtyAlert');
Route::get('report/product_expiry_alert', 'ReportController#productExpiryAlert')->name('report.expAlert');
Route::get('report/warehouse_stock', 'ReportController#warehouseStock')->name('report.warehouseStock');
This my main.blade.php coding
for sidebar (nav - menu )
#if($product_exp_alert_active)
<li id="expAlert-report-menu">
{{trans('file.Product Expiry Alert')}}
</li>
#endif
for user permission
#if($product_qty_alert_active || $product_exp_alert_active)
for database & roll permission
$product_qty_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-qty-alert'],
['role_id', $role->id] ])->first();
$product_exp_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-exp-alert'],
['role_id', $role->id] ])->first();
for define
#if($alert_product > 0)
<li class="nav-item">
<a rel="nofollow" data-target="#" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-item"><i class="dripicons-bell"></i><span class="badge badge-danger">{{$alert_product}}</span>
</a>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default notifications" user="menu">
<li class="notifications">
{{$alert_product}} product exceeds alert quantity
</li>
<li class="notifications">
{{$alert_product}} product expiry alert
</li>
</ul>
</li>
#endif
This is my reportcontroller.php coding
class ReportController extends Controller
{
public function productQuantityAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-qty-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_quantity')->where('is_active', true)->whereColumn('alert_quantity', '>', 'qty')->get();
return view('report.qty_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function productExpiryAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-exp-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_expiry')->where('is_active', true)->whereColumn('alert_expiry', '>', 'name')->get();
return view('report.exp_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}

Route [transaksi.index] is not defined. but the route already exists with route:resource

I don't know why the error is being thrown, because the route already exists.
master.blade.php:
<!-- Nav Item - Transaksi -->
<li class="nav-item">
<a class="nav-link" href="{{route('transaksi.index')}}">
<i class="fas fa-fw fa-folder"></i>
<span>Transaksi</span></a>
</li>
web.blade.php:
Route::resource('transaksi','TransaksiController');
TransaksiController:
public function index()
{
$data = DB::table('tbl_transaksi')
->where('tbl_transaksi.nama_peminjam','like',"%{$request->keyword}%")
->paginate(20);
return view('admin.transaksi.index',['data'=>$data]);
}
The error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [transaksi.index] not defined. (View: C:\xampp\htdocs\SistemPerpustakaan\resources\views\admin\master.blade.php)
http://localhost:8000/dashboard
resolved, I just forgot to delete the route with the same name

Fetch data from a Core Controller in CodeIgniter

I'm working on a Social Network app.
I'm facing a problem in which I'm trying to get the name and username from a table called users in my database.
To be able to do this without accessing to all the controllers individually I had to create a Core controller with the name of MY_Controller.
Here is the problem(MY_Controller without the public $users):
<?php
class MY_Controller extends CI_Controller
{
//public $users = array('username' => $this->input->post('username'));
function __construct()
{
parent::__construct();
//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$this->users = $this->User_model->get_list();
//Load Menu Library
$this->load->library('menu');
$this->pages = $this->menu->get_pages();
// Brand/Logo
$this->favicon = 'https://blogpersonal.net/wp-content/uploads/2017/08/favicon-v2-150x150.png';
$this->brand = 'Some Name';
$this->description = 'Some Description';
$this->credits = 'https://blogpersonal.net/';
$this->version = '1.1.1';
}
}
class Admin_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
if (!$this->session->is_admin) {
redirect('admin/login');
}
//some code here
}
}
class Public_Controller extends MY_Controller{
function __construct(){
parent::__construct();
//some code here
}
}
class Social_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
if (!$this->session->is_member) {
redirect('dashboard/login');
}
//some code here
}
}
As you can see I loaded the User_model using the __construct function to make it available and use it in all controllers.
I want to fetch data in a folder called templates.
views>templates>any document but when I try to fetch it in a view like this:
<?php if($this->users) : ?>
<li class="dropdown user user-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar-->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="user-image" alt="User Image">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span class="hidden-xs"><?php echo $this->users['username']; ?></span>
</a>
</li>
<?php endif; ?>
THIS IS THE OLD ERROR. it shows me an error. Am I doing something wrong? Can somebody explain me how to fix it. This is the first time in which I'm unable to fetch data.
This is the result that I get:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: users
Filename: Templates/public.php
Line Number: 161
Backtrace:
File:
C:\xampp\htdocs\codeigniter\application\views\Templates\public.php
Line: 161 Function: _error_handler
File: C:\xampp\htdocs\codeigniter\application\libraries\Template.php
Line: 34 Function: view
File: C:\xampp\htdocs\codeigniter\application\controllers\Pages.php
Line: 12 Function: load
File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function:
require_once
Here is my model(User_model) in case you might need it:
<?php
class User_model extends CI_MODEL
{
function __construct()
{
parent::__construct();
$this->table = 'users';
}
public function get_list()
{
$query = $this->db->get($this->table);
return $query->result();
}
public function get($id)
{
$this->db->where('id', $id);
$query = $this->db->get($this->table);
return $query->row();
}
public function add($data)
{
$this->db->insert($this->table, $data);
}
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function delete($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
public function login($username, $password)
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row()->id;
} else {
return false;
}
}
}
Now with the changes that were suggested to me, it now shows a new error:
The New error:
A PHP Error was encountered
Severity: Notice Message: Undefined index: username Filename:
Templates/public.php Line Number: 168
Backtrace:
File:
C:\xampp\htdocs\codeigniter\application\views\Templates\public.php
Line: 168 Function: _error_handler
File: C:\xampp\htdocs\codeigniter\application\libraries\Template.php
Line: 34 Function: view
File:
C:\xampp\htdocs\codeigniter\application\controllers\public\Dashboard.php
Line: 13 Function: load
File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function:
require_once
Now if a un-comment the public $users in the Core controller(MY_Controller) ,it shows me a new error as well:
New error with public $users out of __construct function
Fatal error: Constant expression contains invalid operations in
C:\xampp\htdocs\codeigniter\application\core\MY_Controller.php on line
6 A PHP Error was encountered
Severity: Compile Error
Message: Constant expression contains invalid operations
Filename: core/MY_Controller.php
Line Number: 6
Backtrace:
Here is a picture in which I'm trying to get the data to:
Am I doing something wrong? thanks for helping.
Here is the view (public.php) file:
<ul class="nav navbar-nav">
<?php if(!$this->session->is_member) : ?>
<li><?php echo anchor('public/users/login', 'Login'); ?></li>
<li><?php echo anchor('public/users/register', 'Register'); ?></li>
<?php else : ?>
<!-- Messages: style can be found in dropdown.less-->
<li class="dropdown messages-menu">
<!-- Menu toggle button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-envelope-o"></i>
<span class="label label-success">4</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 4 messages</li>
<li>
<!-- inner menu: contains the messages -->
<ul class="menu">
<li><!-- start message -->
<a href="#">
<div class="pull-left">
<!-- User Image -->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="img-circle" alt="User Image">
</div>
<!-- Message title and timestamp -->
<h4>
Support Team
<small><i class="fa fa-clock-o"></i> 5 mins</small>
</h4>
<!-- The message -->
<p>Why not buy a new awesome theme?</p>
</a>
</li>
<!-- end message -->
</ul>
<!-- /.menu -->
</li>
<li class="footer">See All Messages</li>
</ul>
</li>
<!-- /.messages-menu -->
<!-- Notifications Menu -->
<li class="dropdown notifications-menu">
<!-- Menu toggle button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>
<span class="label label-warning">10</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 10 notifications</li>
<li>
<!-- Inner Menu: contains the notifications -->
<ul class="menu">
<li><!-- start notification -->
<a href="#">
<i class="fa fa-users text-aqua"></i> 5 new members joined today
</a>
</li>
<!-- end notification -->
</ul>
</li>
<li class="footer">View all</li>
</ul>
</li>
<!-- Tasks Menu -->
<li class="dropdown tasks-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-flag-o"></i>
<span class="label label-danger">9</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 9 tasks</li>
<li>
<!-- Inner menu: contains the tasks -->
<ul class="menu">
<li><!-- Task item -->
<a href="#">
<!-- Task title and progress text -->
<h3>
Design some buttons
<small class="pull-right">20%</small>
</h3>
<!-- The progress bar -->
<div class="progress xs">
<!-- Change the css width attribute to simulate progress -->
<div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">20% Complete</span>
</div>
</div>
</a>
</li>
<!-- end task item -->
</ul>
</li>
<li class="footer">
View all tasks
</li>
</ul>
</li>
<!-- User Account Menu -->
<?php if($this->users) : ?>
<li class="dropdown user user-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar-->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="user-image" alt="User Image">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span class="hidden-xs"><?php echo $this->users['username']; ?></span>
</a>
<ul class="dropdown-menu">
<!-- The user image in the menu -->
<li class="user-header">
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="img-circle" alt="User Image">
<p><?php echo $this->users['name']; ?> - Web Developer<small>Member since Nov. 2012</small></p>
</li>
<!-- Menu Body -->
<li class="user-body">
<div class="row">
<div class="col-xs-4 text-center">
Followers
</div>
<div class="col-xs-4 text-center">
Sales
</div>
<div class="col-xs-4 text-center">
Friends
</div>
</div>
<!-- /.row -->
</li>
<!-- Menu Footer-->
<li class="user-footer">
<div class="pull-left">
Profile
</div>
<div class="pull-right">
Sign out
</div>
</li>
</ul>
</li>
<?php endif; ?>
<li>
<i class="fa fa-gears"></i>
</li>
<?php endif; ?>
</ul>
You currently have this... and I am only showing snippets here...
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$data['users'] = $this->User_model->get_list();
<<<<snip>>>>
It was suggested that you turn your locally defined $data['users'] into a "Property"
So we now have two changes...
1 Declare the Property
2 Set the property.
class MY_Controller extends CI_Controller
{
public $users = array(); // Add the property $user, safe def of an empty array
function __construct()
{
parent::__construct();
//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
//$data['users'] = $this->User_model->get_list();
// Give the property something to share.
$this->users = $this->User_model->get_list();
<<<<snip>>>>
Now all of your controllers that extend your MY_Controller now have access to $this->users created in MY_Controller...
So you reference $this->users where you want to use it.
I'd suggest you read up on Classes, Properties, Methods and Inheritance to begin with. Usually that is enough to make you dangerous. It takes a little getting used to but its worth the effort...
Update:
In your controller that calls your View you would do
$data['users'] = $this->users;
And in your view, you now refer to it as $users->field_name just like before.
All we have done is define $users in your base class which is now accessible as $this->users so it's accessible to all your controllers / methods that extend the MY_Controller class.
you currently have this.
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$data['users'] = $this->User_model->get_list();
you should use $this->data['users'] in place of $data
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$this->data['users'] = $this->User_model->get_list();
when you load your view use the code below
$this->load->view('view_name',$this->data);
it will show all your variable(in view) stored in parent classes construct function

codeigniter template header white Data

how can I embed a foreach with my header?
I have already tried as in the code.
But with the output I get only "Undefined variable: header_warenkorbs" raus.
Although I am involved in the controller with the value data.
What am I doing wrong?
Controller:
<?php
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function header()
{
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
$this->load->view('templates/header', $data);
// $this->load->view('test/index');
// $this->load->view('templates/footer');
}
}
Model:
<?php
class Warenkorb_model extends CI_Model
{
public function header_warenkorbs()
{
$this->db->select('*');
$this->db->from('db_artikel');
$query = $this->db->get();
return $query->result_array();
}
}
View:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<span class="glyphicon glyphicon-shopping-cart"></span><span class="cart-items-count"><span class=" notification-counter">243</span></span>
<ul class="dropdown-menu dropdown-cart" role="menu">
<?php foreach($header_warenkorbs as $warenkorb): ?>
<li>
<span class="item">
<span class="item-left">
<span class="item-info">
<span>Item name</span>
<span>23$</span>
</span>
</span>
<span class="item-right">
<button class="btn btn-xs btn-danger pull-right">x</button>
</span>
</span>
</li>
<?php endforeach; ?>
<li class="divider"></li>
<li><a class="text-center" href="<?php echo base_url(); ?>warenkorb/index">Warenkorb</a></li>
</ul>
</li>
</ul>
Unless it is a typo or you're not showing us something the problem could be with this line.
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
The model code you show is Warenkorb_model not Admin_model. Seems like the above code should maybe be
$data['header_warenkorbs'] = $this->Warenkorb_model->header_warenkorbs();
You don't show where you load the model. It is loaded somewhere... right? It is common practice to load models in the constructor of the Controller that uses the model.
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Warenkorb_model');
}
// other code
}
Do you realize you are not actually doing anything with the $header_warenkorbs in your foreach loop? You never use $warenkorb.
you need to echo it (example:)
<?php
echo '<table>
<tr>
<td>Name</td>
<td> .$name.</td>
</tr>
</table>';
?>
just edit it to your needs, im too lazy

Undefined variable in views laravel

I want to call variable to views in laravel, but something not working
here is my controller
public function index($id,$slugify, Request $request,WiuCookie $wcookie)
{
$user = ActionUsers::where('id', $id)->first();
$petition = Petitions::where('slug',$slugify)->first();
$isSigned = $wcookie->checkId($petition->id);
return view('layouts.master',compact('user','petition','isSigned'));
}
and here is my view
#if($isSigned)
<li>
<a href="#">
{{$user->name}}
</a>
</li>
#else
<li>
<a href="#">
register
</a>
</li>
<li>
<a href="#">
log-in
</a>
</li>
#endif
and error is Undefined variable: user
return view('layouts.master',compact(['user'=>$user,'petition'=>$petition,'isSigned'=>isSigned]));
It just works for me perfectly.
Try this:
$compactData=array('user','petition','isSigned');
return View::make('layouts.master', compact($compactData));
try this in passing remove compact and pass using with
->with('user',$user)

Categories