I have MVC on CI that not show error but cant get the result.
This the Script
Controller
customer.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('customer_view');
}
function tampil_customer()
{
$this->load->model('customer_model');
$data = array('tests' => $this->customer_model->tampil_data_customer());
$this->load->view('customer_view', $data);
}//end fucnntion
}//end class
?>
Model
customer_model.php
<?php
class Customer_model extends CI_Models
{
public function tampil_data_customer()
{
$results = array();
$this->db->select('*');
$this->db->from('customer');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
}
?>
View
customer_view.php
<table border="1" align="center" widht="900">
<tr>
<td colspan="5" align="center"><h2>Data Customer</h2></td>
</tr>
<tr>
<th>Nama</th>
<th>Company</th>
<th>Alamat</th>
<th>Telepon</th>
<th>HP</th>
</tr>
<?php
//$result = $this->result();
if( !empty($results) )
{
foreach($results as $isi)
{
echo "<tr>";
echo "<td>$isi->nama</td>";
echo "<td>$isi->company</td>";
echo "<td>$isi->alamat</td>";
echo "<td>$isi->telepon</td>";
echo "<td>$isi->hp</td>";
//echo anchor('customer/edit_customer/'.$isi->ID, 'Edit')."|";
//echo anchor('customer/hapus_customer/'.$isi->ID, 'Hapus')."|";
echo "</tr>";
}
}
?>
</table>
the Result only show the HTML like this
Can Anyone Help me to Fix this Problem?
Im very new Using CodeIgniter. I never use Framework before.
Im very appreciated your Answer.
Thanks
You are storing the result from database to array tests here
$data['tests'] = $this->customer_model->tampil_data_customer();
and trying to call it in view with an undefined array $results.
Try with this.
if(!empty($tests->result())){
foreach($tests->result() as $isi)
{
echo "<tr>";
echo "<td>".$isi->nama."</td>";
echo "<td>".$isi->company."</td>";
echo "<td>".$isi->alamat."</td>";
echo "<td>".$isi->telepon."</td>";
echo "<td>".$isi->hp."</td>";
echo "</tr>";
}
}
In model can you make it simple like this,
public function tampil_data_customer()
{
$query=$this->db->query('select * from customer');
$results = $query->result();
return $results;
}
when you pass in arguments using $this->load->view() function,you are using the view() function with the instance of Loader load,it's in /system/core/Loader.php. As the block comments say
An associative array of data to be extracted for use in the view.
if you pass in an object,you can get the public properties by simply using the property name,you can't get the protected and private property.
if you pass in an Multi-dimensional Array,you can get the value by using key/keys in the Multi-dimensional Array.
By the way,you must pass in an array or object other a variable.
sample below
model.php(application\models\test)
class model extends CI_Model {
public $ss=1;
protected $bb=1;
private $vv=3;
}
controller.php(application\controllers)
class Hello extends CI_Controller
public function __construct() {
parent::__construct ();
}
public function index() {
$this->load->model('test/model');
$model=new model();
$this->load->view ( 'test/view', $model );
}
view.php(application\views\test)
echo $ss;
then you get the value of object.
Related
I want to create a loop for my database result. But I can't make it work on my VIEW file.
So here's my model:
<?php class Dash_model extends CI_Model {
public function __construct()
{
parent::__construct();
// Loading second db and running query.
$CI = &get_instance();
//setting the second parameter to TRUE (Boolean) the function will return the database object.
$this->db2 = $CI->load->database('db2', TRUE);
}
public function query0()
{
$query = $this->db2->query("SELECT name FROM table1 ORDER BY date");
return ($query->result());
}
public function query1()
{
$query = $this->db->query("MY QUERY HERE, RUNS OK");
$result = $query->result_array();
return $result;
}
public function query2()
{
$query = $this->db->query("MY QUERY HERE, RUNS OK");
$result = $query->result_array();
return $result;
}
public function query3()
{
$query = $this->db->query("MY QUERY HERE, RUNS OK");
$result = $query->result_array();
return $result;
}
public function query4()
{
$query = $this->db->query("MY QUERY HERE, RUNS OK");
$result = $query->result_array();
return $result;
}
}
Let's say that my query works ok, I've tested it on my SQL Server. And then here's my CONTROLLER:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('dash_model');
$this->load->library('table');
}
public function index()
{
$tmpl = array (
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
);
$this->table->set_template($tmpl);
$data['result0'] = $this->dash_model->query0();
$data['result1'] = $this->dash_model->query1();
$data['result2'] = $this->dash_model->query2();
$data['result3'] = $this->dash_model->query3();
$data['result4'] = $this->dash_model->query4();
$this->load->view('dashboard',$data);
}
}
So i have 5 function in my model, the result was supposed to be a LOOP data. After I put the model inside my CONTROLLER, I pass it to my VIEW file like this:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Transaction Success</th>
<th>Transaction Failure</th>
<th>Total Visit</th>
<th>Total Item</th>
</tr>
</thead>
<tbody>
<?php foreach (array_combine($result0, $result1, $result2, $result3, $result4) as $row)
{ ?>
<tr>
<td><?php echo $row->queryalias0; ?></td>
<td><?php echo $row->queryalias1; ?></td>
<td><?php echo $row->queryalias2; ?></td>
<td><?php echo $row->queryalias3; ?></td>
<td><?php echo $row->queryalias4; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
The error I got was:
Message: array_combine(): Both parameters should have an equal number of elements
And:
Message: Invalid argument supplied for foreach()
The result that I would like to achieve should be like this:
Thank you guys, for all your help...
From php.net
array_combine — Creates an array by using one array for keys and
another for its values
array array_combine ( array $keys , array $values )
You using wrong way array_combine function. You can try array_merge instead
foreach (array_merge($result0, $result1, $result2, $result3, $result4) as $row)
I am trying to get two values 'total number of movies' and 'details of movies' from database.
For total number of movies, i am returning with php error "undefined_index:num_films".
please help me.
Sakila_control
<?php
class Sakila_control extends CI_Controller
{
function display($offset=0)
{
$limit = 20;
$this->load->model('films_model');
$result = $this->films_model->search($limit,$offset);
$data['films']=$result['rows'];
$data['num_results']=$result['num_films'];
$this->load->view('films_view',$data);
}
}
Films_model
<?php
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
return $ret;
$q = $this->db->select(`COUNT(*) as count`, FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
Films_view
<body>
<table>
<div>
Found <?php echo $num_results;?> Films
</div>
<thead>
<th>ID</th>
<th>Title</th>
<th>description</th>
<th>category</th>
<th>price</th>
<th>length</th>
</thead>
<tbody>
<?php
foreach($films as $film):?>
<tr>
<td><?php echo $film->FID;?> </td>
<td><?php echo $film->title;?></td>
<!-- <td><?php echo $film->description;?></td> -->
<td><?php echo $film->category;?></td>
<td><?php echo $film->price;?></td>
<td><?php echo $film->length;?></td>
<td><?php echo $film->rating;?></td>
<!-- <td><?php echo $film->actors;?></td> -->
</tr>
<?php endforeach;?>
</tbody>
</table>
</body>
</html>
First you will need to comment out this:
// return $ret;
Then, you need to fix another error with backticks that you didn't get yet because of the return statement...
Backticks in PHP will attempt to run code from the command line.
Change this:
$q = $this->db->select(`COUNT(*) as count`, FALSE)
To this:
$q = $this->db->select('COUNT(*) as count', FALSE)
Final result:
<?php
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
Read more on backticks here: http://php.net/manual/en/language.operators.execution.php
Execution Operators
PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec().
According to your code you are returning the value just after the first query. So your function is not returning the num_films.
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
// return $ret;
$q = $this->db->select('COUNT(*) as count`, FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
Check multiple return in same function
What about this
In Model
class Films_model extends CI_Model
{
function search($limit,$offset)
{
# Just pass all data to controller
$query = $this->db->query("SELECT * FROM film_list LIMIT $limit, $offset");
$result = $query->result_array(); # Changed
return $result;
}
}
In Controller
class Sakila_control extends CI_Controller
{
function display()
{
$limit = 20;
$offset=0
$this->load->model('films_model');
$data['films'] = $this->films_model->search($limit,$offset);
$count = count($data['films']); # get count on here
$data['num_results'] = $count;
$this->load->view('films_view',$data);
}
}
In View
All Fine and its up to you
So my problem is that the values from this specific id won't print. I really don't know what's the problem since there is no error. please help guys. Still a newbie at using this framework. thanks!
controller:
public function teacher(){
$this->load->model('model_teacher');
$id = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($id);
$this->load->view('teacher/teacher', $data);
}
model:
class Model_teacher extends CI_Model {
public function scoreboard($id) {
//$this->db->where('login_id', $this->input->post('idnum'));
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$id."'");
return $query->result();
}
}
view:
<?php
foreach ($result as $a) {
echo $a['login_id'];
echo $a['lname'];
echo $a['mname'];
echo $a['fname'];
}
?>
Alternative to Ghost's answer:
To keep you model the same you would just need to change your view file from:
foreach ($result as $a) {
echo $a['login_id'];
echo $a['lname'];
echo $a['mname'];
echo $a['fname'];
}
To:
foreach ($result as $a) {
echo $a->login_id;
echo $a->lname;
echo $a->mname;
echo $a->fname;
}
This is because result() with the DB driver returns an array of Objects where as result_array() returns an array of arrays.
Hope this helps!
I Have a Main class Employee which fetches Employees Details,Passed Inputs it fetches mysql results and RETURNS into a associate array into $row.
public function Result(){
$this->IsEmptyCheck();
$this->ConnectDb();
$this->QueryDb();
$this->RowCount();
$this->IfEmployeeFound();
}
public function IfEmployeeFound(){
if ($this->row_cnt > 0)
{
while ($row = $this->result->fetch_assoc()){
return($row);
}
}else{echo "No Results" ;}
}
public function CustomhtmlTabledisplay($row){
foreach ( $row as $key => $value ) {
echo .....
echo "<td>".$value['employee_name']."</td>\n";
echo "<td>".$value['age']."</td>\n";
echo "<td>".$value['familydetails']."</td>\n";
echo .....
}
}
I am running the below php call calling the employee class and executing above functions in it like this.
$check = new Employee($employeeid);
$check->Result()->CustomhtmlTabledisplay();
$check->CloseDb();
How can i achieve it ?
I would like to fetch the data by passing the mysql returned rows array from
IfEmployeeFound() into CustomhtmlTabledisplay();
I would like to display employee details by executing this type of query
$check->Result()->CustomhtmlTabledisplay();
(If I understood). For doing next - by executing this type of query:
$check->Result()->CustomhtmlTabledisplay();
You need return $this in end of every method for chaining methods. And store need data in property-fields of your Main Class.
EDIT
If you want use your class like this
$check->CustomhtmlTabledisplay(($check->Result());
change class methods to:
public function Result(){
$this->IsEmptyCheck();
$this->ConnectDb();
$this->QueryDb();
$this->RowCount();
return $this->IfEmployeeFound();
}
public function IfEmployeeFound(){
$out = array();
if ($this->row_cnt > 0){
while ($row = $this->result->fetch_assoc())
$out[] = $row;
}
return empty($out)? null: $out;
}
public function CustomhtmlTabledisplay($rows){
if($rows){
foreach ( $rows as $row) {
echo .....
echo "<td>".$row['employee_name']."</td>\n";
echo "<td>".$row['age']."</td>\n";
echo "<td>".$row['familydetails']."</td>\n";
echo .....
}
}
}
You can use
require_once (employee.php); //employee is the file where you have the methods
And instance the class employee
I tried a lot of search but unable to figure out why array $wordlinks in function DoWordLink is not carrying values from function __construct. PHP class code as below:
<?php
class autolinkkeyword
{
public $wordlinks = array();
public function __construct(){
$query = mysql_query("SELECT keyword FROM library");
while ($row = mysql_fetch_array($query))
{
$this->wordlinks [$row["keyword"]] = $row["keyword"];
}
}
public function linkkeywords ($posts)
{
function DoWordLink($match)
{
$rpl=$match[1];
if(isset($wordlinks[$rpl]))
{
$kword = $this->wordlinks[$rpl];
$rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv');
ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
unset($this->wordlinks[$match[1]]);
}
return $rpl;
}
$wl=array_keys($this->wordlinks);
$pm="/((?<=\s|^)(?:" . implode('|',$wl) .")(?=\.|\!|\?|\,|\'|\s|$))/im";
foreach($posts as $key => $mainbody)
{
$mainbody=preg_replace_callback($pm, 'DoWordLink', $mainbody) ;
echo $mainbody;
}
}
}
?>
You can make it an actual method of that class and call it using this method:
http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
like:
preg_replace_callback($pm, array($this, 'DoWordLink'), $mainbody);
Change DoWordLink function so it is part of the class like:
class autolinkkeyword
{
function DoWordLink($match)
{
$rpl=$match[1];
if(isset($this->wordlinks[$rpl]))
{
$kword = $this->wordlinks[$rpl];
$rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv');
ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
unset($this->wordlinks[$match[1]]);
}
return $rpl;
}
}
aren't you missing a "this->" construct here? if(isset($this->wordlinks[$rpl]))
Use the $this everywhere you refer to $wordlinks.
$this->wordlinks
You need to access the property in your linkkeywords-method with the object-accessor, too!
public function linkkeywords ($posts)
{
// Here use $this->wordlinks not $wordlinks
}