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
Related
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostsController;
use App\Http\Controllers\AboutController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\AdminControllers\DashboardController;
use App\Http\Controllers\AdminControllers\AdminPostsController;
use App\Http\Controllers\AdminControllers\AdminCategoriesController;
use App\Http\Controllers\AdminControllers\TinyMCEController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [HomeController::class,'index'])->name("home");
Route::get('/posts/{post:slug}',[PostsController::class,'show'])->name("posts.show");
Route::post('/posts/{post:slug}',[PostsController::class,'addComment'])->name("posts.add_comment");
Route::get('/contact', [ContactController::class , 'create'])->name("contact.create");
Route::post('/contact', [ContactController::class , 'store'])->name("contact.store");
Route::get('/about', AboutController::class)->name("about");
Route::get('/categories/{category:slug}',[CategoryController::class,'show'])->name("categories.show");
Route::get('/categories',[CategoryController::class,'index'])->name("categories.index");
/// /tags/{tag:slug} === SHOULD BE /tags/{tag:name}
Route::get('/tags/{tag:name}',[TagController::class,'show'])->name("tags.show");
// Admin Dashboard
//Route::get('/admin',[DashboardController::class,'index'])->name("admin.index");
Route::prefix('admin')->name('admin.')->middleware(['auth','isadmin'])->group(function(){
Route::get('/',[DashboardController::class,'index'])->name("index");
Route::post('upload_tinymce_image',[TinyMCEController::class,'upload_tinymce_image'])->name('upload_tinymce_image');
Route::resource('posts',AdminPostsController::class);
Route::resource('categories',AdminCategoriesController::class);
});
require __DIR__.'/auth.php';
AdminCategoriesController.php
<?php
namespace App\Http\Controllers\AdminControllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Models\Category;
class AdminCategoriesController extends Controller
{
public function index()
{
return view('dashboard.categories.index');
}
public function create()
{
return view('dashboard.categories.create');
}
public function store(Request $request)
{
}
public function show(Category $category)
{
return view('dashboard.categories.show', [
'category' => $category
]);
}
public function edit(Category $category)
{
return view('dashboard.categories.edit', [
'category' => $category
]);
}
public function update(Request $request, Category $category)
{
}
public function destroy(Category $category)
{
}
}
nav.blade.php
<!--sidebar wrapper -->
<div class="sidebar-wrapper" data-simplebar="true">
<div class="sidebar-header">
<div>
<img src="{{asset('assets/images/logo-icon.png')}}" class="logo-icon" alt="logo icon">
</div>
<div>
<h4 class="logo-text">MYBLOG</h4>
</div>
<div class="toggle-icon ms-auto"><i class='bx bx-arrow-to-left'></i>
</div>
</div>
<!--navigation-->
<ul class="metismenu" id="menu">
<li>
<a href="" target="_blank">
<div class="parent-icon"><i class='bx bx-home-circle'></i></div>
<div class="menu-title">Dashboard</div>
</a>
</li>
<li>
<a href="javascript:;" class="has-arrow">
<div class="parent-icon"><i class='bx bx-message-square-edit'></i>
</div>
<div class="menu-title">Posts</div>
</a>
<ul>
<li> <i class="bx bx-right-arrow-alt"></i>All Posts
</li>
<li> <i class="bx bx-right-arrow-alt"></i>Add New Post
</li>
</ul>
</li>
<li>
<a href="" class="has-arrow">
<div class="parent-icon"><i class='bx bx-menu'></i>
</div>
<div class="menu-title">Categories</div>
</a>
<ul>
<li> <i class="bx bx-right-arrow-alt"></i>All Categories
</li>
<li> <i class="bx bx-right-arrow-alt"></i>Add New Category
</li>
</ul>
</li>
</ul>
<!--end navigation-->
</div>
<!--end sidebar wrapper -->
I develop a blog and I have a resource controller for posts and worked fine when I created another one for categories gave my that error "Route [admin.categories.index] not defined." .
Just I put the route in dashboard the error raise and gone if I commented the two routes for categories in nav.blade.php file
According to Laravel 9 Documentation
Route::resources([
'posts' => AdminPostsController::class,
'categories'=> AdminCategoriesController::class
]);
then I ran php artisan route:clear.
finally the routes appears.
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
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.
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');
}
I'm pretty new to Codeigniter and tried to follow the Query builder however getting an error.
The view window:
<div class="col-lg-4">
<div class="bs-component">
<div class="list-group">
<li class="list-group-item"><b><font color="white">EXAMBANK DATABASE</b>
</li>
<li class="list-group-item"> <span class="badge">9</span>
Total Subjects:
</li>
<li class="list-group-item"> <span class="badge"><?php echo $ques; ?></span>
Questions In Database:
</li>
</div>
</font>
</div>
</div>
Controller:
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$this->Student_model->get_all_question($question);
}
Model:
function get_all_questions()
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques->result();
}
Any suggestions??
You need to return $ques; from your model and you need to pass $question in your argument
Model:
function get_all_questions($question)
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques;
}
You need to load your view and pass your variable to views
Controller
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$data['ques']=$this->Student_model->get_all_question($question);
$this->load->view('student/list',$data)
}
Please try this way:
Controller:
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$data['ques'] = $this->Student_model->get_all_question($question);
$this->load->view('folder_name/file_name',$data );
}
Model:
function get_all_questions($question)
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques; //not $ques->result() because $this->db->count_all_results() Produces an integer
}
<li class="list-group-item"> <span class="badge"><?php echo #$ques; ?></span>
Questions In Database:
</li>