using $this in view file codeIgniter - codeigniter

im a newbie in CI, i have some question
i have controller like below
public function show_admin()
{
$data['data_admin'] = $this->M_Admin->get_admin();
$data['page'] = 'show_admin';
$this->load->view('admin/template',$data);
}
and then i have template in my view folder, like below
<body>
<div id="wrapper">
<div id="header">
<?php
$data['session'] = $this->session->all_userdata();
$this->load->view('admin/header',$data);?>
<div id="content">
<?php
$this->load->view('admin/'.$page);?>
</div>
<div id="footer">
<?php $this->load->view('admin/footer');?>
</div>
</div>
</body>
and the show_admin (view file) like below
<div id="isi_content">
<span><img src="<?php echo base_url();?>assets/admin/images/add.png">Tambah</span>
<table class="table table-hover table-bordered">
<tr>
<th>No</th>
<th>Nama</th>
<th>Username</th>
<th>Login Terakhir</th>
<th>Aksi</th>
</tr>
<?php
$no=1;
foreach($data_admin as $admin){
$id = $admin['id_admin'];
$url = site_url('admin/C_Admin/c_delete_admin');
echo
'<tr>
<td>'.$no.'</td>
<td>'.$admin['nama_admin'].'</td>
<td>'.$admin['username'].'</td>
<td>'.$admin['tgl_last_visit'].'</td>
<td class=aksi><a href='.site_url('admin/C_Admin/c_edit_admin/'.$admin['id_admin'].'').'><img src='.base_url().'assets/admin/images/edit.png></a>
<img src='.base_url().'assets/admin/images/delete.png></td>
</tr>';
$no++;
}
?>
</table>
</div>
and that codes run nicely,
is that normal in codeIgniter use $this in view file??
im confuse about this..
thanks for your answer...

No, you should not use that session variable nor load any views on another view. This all should be done in the Controller. What you can do is break your code in pieces, like the header in its one view file and the footer in another view file, then you can load them in your controller like so:
public function show_admin()
{
$data['data_admin'] = $this->M_Admin->get_admin();
$data['page'] = 'show_admin';
$head_data['session'] = $this->session->all_userdata();
$this->load->view('header', $head_data);
$this->load->view('admin/template',$data);
$this->load->view('footer');
}

Related

DomPDF is displaying output as HTML not as PDF document

I'm facing an issue with domPDF. pdf result getting displayed as html page not as pdf document. I'm trying to get the data from database and get the result in pdf document using 'dompdf' for laravel.
pdf-output-image
here is my HTML code.
`<!DOCTYPE html>
<html>
<head>
<title>MHT Order PDF</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body>
<div class="d-block text-center" style="text-align: center; margin-top: -15px;">
<h1 style="text-align: center" class="navbar-brand text-underline text-center"><u> MAJESTIC HOUSE </u></h1>
<p style="margin-top:-20px">General Trading LLC</p>
</div>
<div class="order_heading">
#foreach($orderDtls as $order)
<p class="date">Date: {{$date}}</p>
<h3 class="orderDtls">Order Details </h3>
<h5 class="details">Order UID: <strong style="color: red;">{{$order->order_unq_id}}</strong></h5>
<h5 class="details">Customer Name: <strong style="color: red;">{{$order->customer_name}}</strong></h5>
<p id="date"> </p>
</div>
<table class="table caption-top" id="orderTable">
<caption>Order Details</caption>
<thead>
<tr>
<th scope="col">Customer Name</th>
<th scope="col">Product Name</th>
</tr>
</thead>
<tbody>
#foreach($orderDtls as $order)
<tr>
<td scope="row">{{$order->customer_name}}</td>
<td>{{$order->item_name}}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
</body>
</html>
this is the code from Controller
public function mht_order_pdf(){
$customers = Customer::all();
$linkeds = Linked::all();
$orders = Order::all();
$date = date('y-m-d');
$orderDtls = DB::select("SELECT
customers.customer_name,
orders.id,
orders.order_unq_id,
items.item_name,
orders.item_quantity,
orders.total
FROM customers
JOIN linkeds
ON customers.id = linkeds.customer_id
JOIN items
ON linkeds.item_id = items.id
JOIN orders
ON linkeds.id = orders.linked_id
WHERE orders.order_unq_id = 'MH-Ord/29722/xyz supermarket';"
);
// // return $orderDtls;
return view('mht_order_pdf', compact('customers', 'linkeds', 'orders', 'orderDtls',
'date'));
$pdf = PDF::loadView('mht_order_pdf', $orderDtls);
return $pdf->stream('mht_order_pdf.pdf');
}
this is the code from web.php
<?php
use App\Http\Controllers\Backend\CustomerController;
use App\Http\Controllers\Backend\ItemController;
use App\Http\Controllers\Backend\LinkedController;
use App\Http\Controllers\Backend\OrderController;
use App\Http\Controllers\Backend\ProductController;
use App\Http\Controllers\Backend\UserController;
use App\Http\Controllers\PDFController;
use App\Models\Customer;
use App\Models\Linked;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Support\Facades\Route;
use Dompdf\Dompdf;
use Illuminate\Support\Facades\DB;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])-
>name('home');
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
Route::resource('items', ItemController::class);
Route::resource('customers', CustomerController::class);
Route::resource('linked', LinkedController::class);
Route::resource('orders', OrderController::class);
Route::get('mht_order_pdf', [OrderController::class, 'mht_order_pdf'])-
>name('mht_order_pdf');
PLEASE LET ME KNOW WHAT I'M DOING WRONG?
Thank you!
Your controller stops at this line:
return view('mht_order_pdf', compact('customers', 'linkeds', 'orders', 'orderDtls', 'date'));
thus the 2 lines below it, which related to the DomPDF, gets ignored. Try to delete the return view() above so that the DomPDF lines of code below it can be executed.

Joomla - pagination bar not showing up

I'm trying to get pagination working in my joompla component admin page (table of records from database).
I'm using the standard method found on the net, so in my view class I have:
function display($tmpl=NULL)
{
$this->tabela = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->state = $this->get('State');
...
and then in my default template for this view:
<tfoot>
<tr>
<td colspan="7" >
<?php echo $this->pagination->getListFooter(); ?>
</td>
</tr>
but the pagination is not showing up. In the place where it should occur I can only see this (in website's HTML):
<div class="pagination pagination-toolbar">
<input type="hidden" name="limitstart" value="0">
</div>
How can I get this pagination work?

new to CRUD in Laravel 5

I'm very new to Laravel 5. I'm currently doing a task project with CRUD functions. I did the delete function, but update and add are still messy. Please help me.
My database has only 1 table, 'tasks' with 3 columns: id, name and day.
My TaskModel:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class TasksModel extends Model {
//
protected $table = 'tasks';
}
Here is my home view:
<html>
<header>
<style>
.wrapper{
width:600px;
margin:auto;
}
</style>
<title>Tasks List</title>
</header>
<body>
<div class="wrapper">
<h2 align="center"><font color="#a52a2a">Task List</font></h2>
<table name ="todo_table" border="1px" cellspacing="0" width = "600px">
<tr align="center">
<td>ID</td>
<td>Name</td>
<td>Time</td>
<td>Action</td>
</tr>
<?php
$records = \App\TasksModel::all()->sortBy('id');
foreach($records as $mytask)
{
?>
<tr align="center">
<td><?php echo $mytask->id; ?></td>
<td width="200px"><?php echo $mytask->name; ?></td>
<td><?php echo $mytask->day; ?></td>
<td width="100px">
<img src = "/public/images/add.jpg" width="30px" height="30px">
<img src = "/public/images/del.jpg" width="30px" height="`30px">
<img src = "/public/images/update.jpg" width="30px" height="30px">
</td>
</tr>
<?php
}// End of foreach($records as $mytask)
?>
</table>
</div>
</body>
</html>
Here's my Add view:
<html>
<head>
<style>
.wrapper{
width:600px;
margin:auto;
}
</style>
</head>
<body>
<div>
{!! Form::open(array('url' => 'process', 'method' => 'post')) !!}
<div class="wrapper">
<h2><font color="#a52a2a">Add Task</font></h2>
<p><input type = "text" name = "new-task" placeholder = "Add new task..." /></p>
<p><input type = "text" name = "new-time" placeholder = "Add new time..." /></p>
{!! Form::submit('Add', array('name' => 'bt_add')) !!}
</div>
{!! Form::close() !!}
</div>
</body>
</html>
My Route:
Route::get('/', 'HomeController#index');
Route::get('add', 'HoneController#add');
Route::get('delete/id={del}', 'HomeController#delete');
Route::get('update/id={edit}', 'HomeController#update');
Route::get('process', 'ProcessController#index'); // I think process page will handle update/add
You don't need to do CRUD manually, Laravel can do that automatically using RESTFul Controllers.
Take a look here:
http://laravel.com/docs/5.0/controllers#restful-resource-controllers
To build the RESTFul URLs you use route method
http://laravel.com/docs/5.0/helpers#urls
simple :)
First off the below belongs in your Task controller. Your view should be dumb. There is basically no reason to insert raw php into a view file.
<?php
$records = \App\TasksModel::all()->sortBy('id');
foreach($records as $mytask)
{
...
}
?>
You'll have your TasksController respond to the route and prepare the data for the view. Then in the view do something like:
#foreach($tasks as $task)
...
#endforeach
Your routes should be updated to:
Route::delete('task/{id}', TaskController#delete);
Your form will need to have a method of delete as well.
You can do CRUD in laravel easily by following these steps
Route::group(['prefix' => 'admin'], function () {
...
Route::resource('/categories', 'admin\CategoryController');
...
});
Create the resource using PHP artisan command available in Laravel
php artisan make:controller CategoryController --resource
This will create the resource controller and you will have emtpy methods index, store, update, show, destroy.
You can then tie your methods to views
For detailed explaination please follow the tutorial at
http://deepdivetuts.com/basic-create-edit-update-delete-functionality-laravel-5-3

Load an HMVC module view within an Iframe in Codeigniter

I'm new at this and some help will be apprciated.
I'm building a sidebar in a CI-Boilerplate-Project which contains modules (widgets) that i got run with HMVC https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc.
In the sidebar i have a widget that display a friendslist with status online/offline.
The user has the ability to switch the widgets on/off in the managementsection.
In the Profileview:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting)
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
</aside>
if box_visible == 1 the widget will be displayed.
Controller:
class Myfriends extends SM_Controller
{
function __construct()
{
parent::__construct();
}
public function index($visible = false)
{
$user = $this->session->userdata('user');
$myf = $this->widget_model->get_friends($user['user_id'], 5);
$data['friends'] = $myf;
if ($visible) $this->load->view('myfriends', $data);
}
}
View:
<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
<body>
<div class="box friendsbox">
<div id="header"><h3><?=$boxTitle?></h3></div>
<div id="boxcontent">
<ul>
<?php foreach ($friends as $friend): ?>
<li>
<div id="thb_img">
<img src="<?=img_thumb($friend['file_path'], 50, 50) ?>" />
</div>
<div id="short_desc">
<a href="<?= site_url('widget_functions/show_user/' . $friend['uu_id']) ?>">
<?= ucfirst($friend['user_name']) . ' ' . ucfirst($friend['user_lastname']) . ' ' ?>
</a>
<?php if ($friend['is_online']): ?>
<span style="color: green">online</span>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="footer">ยป mehr</div>
</div>
</body>
</html>
Now, i need to update the friendslist every 1-2 min so i tryed to load the moduleview within an iframe:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting): ?>
<?php if ($boxName == 'myfriends' && $boxSetting['box_visible'] == 1) { ?>
<iframe src="<?php echo site_url('myfriends/index'); ?>" ></iframe>
<?php
}
else
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
<?php endforeach; ?>
</aside>
BUT this dose not work! The place of the widget is emtpy.
Do you have any idea how to get that to work?
appreciate your help
I believe the main issue is with the way you initialize the index method. the index method is kinda tricky with parameters in Codeigniter. In my projects, the only way to get the vlues of arguments passed to the index parameters is by using the URI library method $this->uri->segment(n). In other words, I believe that the value of $visible is not properly passing to the index() body
Anyway, I think you should create another method in your MyFriends Class called render() for example, and call it instead of relaying on the index() method. now render() can play nicely with the $visible=false initialization trick.
Hope this helps

Codeigniter - dynamic right menu

i have a right menu in my ci blog site that contains categories and no. of post that is subjected to change, has the following format,
Categories:
Science(24)
education(32)
....
....
the number is brackets is the total amount of post in that category.
my template file is here:
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
And my right menu is in the footer file.
How could i achieve this?
you better have a main view divided into divs(header, main_content,footer,right_menu) always loaded and load each view in the appropriate div.
<html>
<body>
<div id="header">
<?php $this->load->view('header'); ?>
</div>
<div id="body">
<div id="top_menu">
<?php $this->load->view('top_menu'); ?>
</div>
<div id="main_content">
<?php $this->load->view('main_content'); ?>
</div>
</div>
<div id="footer">
<?php $this->load->view('footer'); ?>
</div>
</body>
</html>
You can also use this system for including header/footer etc:
<?php
/**
* /application/core/MY_Loader.php
*
*/
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
Then, in your controller, this is all you have to do:
<?php
$this->load->template('body');
Code if from user: landons

Resources