Codeigniter loading library - view

i have a controller like this one :
<?php if( ! defined('BASEPATH')) exit ('No direct script acces allowed');
class Halaman extends CI_controller{
function __controller(){
parent::controller;
$this->load->helper(array('url','form'));
$this->load->library('table');
}
function index(){
$this->load->library(array('form_validation'));
$this->load->view('view_halaman');
}
function daftar(){
$this->load->model(url);
}
}
and i have a views like this one
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pembagian menggunakan validasi!!</title>
</head>
<body>
<h1>Daftar Ulang</h1>
<?php echo form_open('halaman/daftar'); ?>
<?php $data=array(
array('Field','isi data'),
array('nama',form_input('user','tulis username')),
array('password',form_password('pass','password')),
array('email',form_input('email','tulis email di sini'))
);
echo $this->table->generate($data);
?>
<?php echo form_close(); ?>
<p><br/>Page rendered in {elapsed_time} seconds</p>
</html>
enter code here
what i get is this one :
where is my mistake ? im sorry i am really newbie in codeigniter. thanks a lot .

$table is not a member of the view object. So you can't call it from the view template. You need to move the echo $this->table->generate($data); to the controller, and either assign it to a view variable or just echo it from the controller.

I think the problem is that the library wasn't loaded (that's the error in the red lined box). This happends when you don't follow the CI library name conventions. The Table library should be in application/libraries/Table.php and class must be someting like:
class Table {
// Your code
}
http://codeigniter.com/user_guide/general/creating_libraries.html
Also when you load the library, use the name with capital T:
$this->load->library('Table');
Regards,

Related

How to create template in cakephp 3.x

I'm a new student in CakePHP 3 please resolve my problem.
This is my controller file:
DirectUseController.php
<?php
class DirectUseController extends AppController {
function index() {
$this->layout = 'directuse';
}
}
?>
This is my layout file:
directuse.ctp
<!DOCTYPE html>
<html>
<head>
<title>
<?= $this->fetch('title') ?>
</title>
</head>
<body>
Bootstrap | Foundation | Materilize
<br><br>
Copyright
<br><br>
</body>
</html>
This is my index file in folder of direct use
index.ctp
<section id="mainBody">
hello
</section>
and my folder structure is:
What am I missing?
Your layout should presumably include this somewhere:
echo $this->fetch('content');
If that doesn't solve your problem, you're going to have to be more specific about what the problem is.
CakePHP prioritizes convention over configuration, so try to change your Controller name to DirectusesController, change also your layout folder's name (to DirectUses) and maybe your Model too (cake bake can easily help you), I don't know your [table name in the database] but it should be plural and in lowercase (directuses) (if you don't use database that's another story)
For your template Greg Schmidt is right
Inside your layout file you have to use this:
<?= $this->Flash->render(); ?>
<?= $this->fetch('content'); ?>
I prefer to add the <html> and <body> tags inside the default.ctp or directuse.ctp layout. On this way you do not have to rebuild your html every time. This will make your code a lot cleaner.

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

Codeigniter - Query inside foreach

So i'm using latest Codeigniter and my problem is that i have this situation where i should use query inside foreach in controller. I have view that shows all production orders in one list, and i want all user made comments and notes underneath them from other table. Quite general situation in coding, but how i should do this with Codeigniter, cause you should keep your SQL clauses in Models.
Before i just fork raw SQL inside controller, i wanted to ask what is more gentle and proper way to achieve this. Thanks!
Controller
<?php
public function show()
{
$this->load->database();
$this->load->model('report_model');
$this->load->helper('url');
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
$this->load->view('all_reports', $data);
}
?>
Model
<?php
function getAllProductionOrders()
{
$this->db->select(*);
$this->db->from('dbo.QualityControl_ProductionOrders');
$query = $this->db->get();
return $query->result();
}
?>
View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
foreach ($productionOrdersas $row)
{
?>
<div>
ProdNo: <?php echo $row->Prodno; ?>
Descriptions:
Hours:
etc etc etc etc
</div>
[I want here comments and all user made notes]
<?php
}
?>
</body>
</html>
You should make an effort to avoid spinning over a result set with additional queries like that..imagine if you had 500 production orders. That's 501 queries to make a simple report
Instead, use a join & parse your results either by only printing out the header info once or using Underscore.php to make a nested result, as demonstrated here:
http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Not sure what you asking but try this -
In controller ,
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
foreach($data['productionOrders']['id'] as $id)
{
$data['comment'][$id] = $this->report_model->getCommantsByOrderId($id);
}

Laravel 4 blade templates causing FatalErrorException?

I'm getting an unexplained FatalErrorException when trying to implement a simple page layout using blade templating. I'm not sure if it's something I'm doing wrong or Laravel is. I'm following the tutorial on L4's documentation about Templating and my code seems to follow it. Here's my code.
app/routes.php:
<?php
Route::get('/', 'HomeController#showWelcome');
app/views/home/welcome.blade.php:
#extends('layouts.default')
#section('content')
<h1>Hello World!</h1>
#stop
app/views/layouts/default.blade.php:
<!doctype html>
<html>
<head>
<title>The Big Bad Barn (2013)</title>
</head>
<body>
<div>
#yield('content')
</div>
</body>
</html>
app/controllers/HomeController.php:
<?php
class HomeController extends BaseController {
protected $layout = 'layouts.default';
public function showWelcome()
{
$this->layout->content = View::make('home.welcome');
}
}
Laravel just throws a FatalErrorException. The output error page says "syntax error, unexpected '?'". The file blade is generating inside the storage/views directory has PHP where the
<?php echo $__env->make('layouts.default')
<?php $__env->startSection('content', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
<h1>Hello World!</h1>
<?php $__env->stopSection(); ?>
Yesterday I encountered the same problem you did, however the other answers didn't fix my problem. You've probably figured it out already, but maybe this post prevents others from spending as much time as I did figuring out what is wrong while it's such a small (but frustrating!) thing.
I'm using Notepad++ as text-editor and for some strange reason it had decided to use "MAC format" as the End-Of-Line (EOL) format. Apparently the Blade framework can't cope with that. Use the conversion function (in notepad++ : Edit -> EOL Conversion) to convert to Windows Format and it will work just fine..
Your controller should be just returning View::make("home.welcome") instead of attaching it to the layout.
The welcome view then calls the layout so the controller is only concerned about the body template in this case.
Edit for showing example controller:
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('home.welcome');
}
}
Eric already gave the right answer, I just wanted to add that if you want to use
protected $layout = 'layouts.default';
in your HomeController, then leave showWelcome action intact and remove this line
#extends('layouts.default')
from welcome.blade.php file. That should work too.
Regards,
Vlad

Redirecting to another page. How do I display a countdown?

I'm just wondering if the following is possible:
I don't know anything about JQuery and I don't know how to make my own javascripts yet.
When a user logs into my website, they'll get a page that says "login completed".
Under that sentence, I would like to get a sentence that counts down the seconds until they are redirected. Is there a simple way to do this?
I already have a countdown function:
When the function is first called, the view 'view_login_success' is loaded and the $data[] array is passed to it. $data['sec'] holds the seconds remaining that should be printed on the screen.
When the do-while loop is over, the user should be redirected to the homepage.
What happends is, the 'view_login_success'-view isn't loaded at all and after 5 seconds, the user is redirected to the homepage.
function timerIn() {
$now = time();
$this->load->view('view_login_success', $this->data);
do {
if (time() - $now != 0) {
$this->data['sec'] = $this->data['sec'] - 1;
$this->load->view('view_login_success', $this->data);
$now = time();
}
} while ($this->data['sec'] != 1);
$this->data['sec'] = 5;
redirect('user/start');
}
Here is the view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login geslaagd</title>
<?php $this->load->view('templates/header2'); ?>
</head>
<body>
<h2 style="text-align: center;"><?php echo strtoupper(substr($this->session->userdata('gebruikersnaam'),0,1));
echo substr($this->session->userdata('gebruikersnaam'),1); ?>, U bent ingelogd!</h2>
<p>U word doorverwezen: <?php echo $sec; ?> seconden...</p>
</body>
<?php $this->load->view('templates/footer'); ?>
If there's a way to do this in JQuery of Javascript, then please explain to me what I should do to get it to work.
Thanks in advance! :)
That's not how php works.
If you want the page to redirect after 5 secs you have to do so in the JS code.
You redirect the user without sending the view to the client.
Right now you are only waiting 5 seconds to redirect. So:
Show the view
The JS will have the timer and then redirects.
The javascript code would be something like this:
setTimeout(function(){
window.location.href = 'theURLyouwant';
},5000)
OR you could use the ol' meta tag
<meta http-equiv="refresh" content="5; URL=theURLyouwant">
Use code-igniter to solve this problem. Use this reference class output. Output class will be loaded automatically. You don't need to load this. Use this function to set header.
$this->output->set_header('refresh:time-to-redirect; url='.site_url("your-url"));
For more detail about this class.

Resources