I have a code like this
$this->load->model("m_crud");
$test= $this->m_crud->get_querry; //this model already have query and result() to return
foreach($test as $retest)
{
echo $retest->primary_id;
$test2=$this->db->query("select * from table2 where id='$retest");
foreach($test2 as $retest2)
{
echo $retest2->name;
}
}
Its easy to just put all this code to "views" but I want to use MCV model.
I tried to store the result using array like this:
$data['test']=$test
foreach($test as $retest)
{
echo $retest->primary_id;
$test2=$this->db->query("select * from table2 where id='$retest");
$data['test2']=$test2
foreach($test2 as $retest2)
{
echo $retest2->name;
}
}
$this->load->view("test_view",$data);
What I got in the view is I have the same value from the echo of $test2, which should be differently by each of $test
thank you, sorry for my bad english
You can use in view page
foreach($test2 as $newarray)
{
foreach($newarray->result() as $row)
{
echo $row->columnnames;//
}
}
It is easy here is an example
Controller Method
function getResult()
{
$this->load->model('mymodel');
$results = $this->mymodel->getRecords();
$data['results'] = $results;
$this->load->view('myview',$data);
}
Model Method
function getRecords()
{
return $this->db->query("select * from table2 where id='$retest")->result_array();
}
And view
foreach($results as $row){
echo $row['id'];
echo '<br>';
echo $row['othercolumn'];
}
Related
Like this:
class example {
public function func($query)
{
...
return $row;
}
}
How can i change below code:
public function func($query)
{
while($row=mysqli_fetch_assoc($query)){
return $row;
}
I tried to push every row to array but it is hard to get rows from array;
For example:
db.php
class db {
public function get($query){
while($row=mysqli_fetch_assoc($query)){
return $row;
}
}
show.php
$db = new db;
$row = $db->get("SELECT * FROM `posts`");
echo $row['title'];
echo "<br/>";
echo $row['body'];
This is what i want to do
<?php function getnews()
{
global $db;
$news=$db->query("select * from news order by news_id desc");
$row=$news->fetch_object();
return $row;
}?>
`
foreach (getnews() as $result)
{
echo $result->news_title . "<br>";
}
?>
but foreach not work out of function
any help
Your getnews() function only ever returns a single row from the database, even though your query will fetch them all.... perhaps consider a loop in the getnews() function that returns each one in turn, maybe using a generator so that you can use them in a foreach loop
function getnews() {
global $db;
$news=$db->query("select * from news order by news_id desc");
while ($row=$news->fetch_object()) {
yield $row;
}
}
foreach (getnews() as $result) {
echo $result->news_title . "<br>";
}
Though using a generator does require PHP >= 5.5
If you're using an earlier version of PHP, then build an array in getnews() and return that, but it isn't as efficient:
function getnews() {
global $db;
$news=$db->query("select * from news order by news_id desc");
$items = array();
while ($row=$news->fetch_object()) {
$items[] = $row;
}
return $items;
}
foreach (getnews() as $result) {
echo $result->news_title . "<br>";
}
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 was following the instruction to get the entire record from table, and load them into html table. this is the model
private $namatabel;
public function __construct() {
parent::__construct();
$namatabel='ms_kategori_material';
}
function read()
{
$sql = $this->db->get($this->namatabel);
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
return null;
}
}
then use the read() function on controller
public function __construct() {
parent::__construct();
$this->load->model('m_kategorimaterial');
}
function index()
{
$data['c_row'] = $this->m_kategorimaterial->read();
//pass the c_row into the views
$this->load->view('v/vkategorimaterial', $data);
}
to display them on the views
<?php
$no = 1;
foreach ($c_row as $row) { ?>
<tr id="row">
<td id="no"><?php echo $no;?></td>
<td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>
<td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>
</tr>
<?php
$no++;
}
?>
but then I got an error saying, undefined variable c_row and invalid argument supplied foreach(). I thought I have sent the c_row variable through the c_kategorimaterial/index and copy pasting foreach statement. what went wrong ?
1)Check whether you are getting the data right by using print_r($data). If that went wrong,you are probably missing something in the query.
2)If you got the db data perfectly,then just change the name of returning array in your model.
function read()
{
$sql = $this->db->get('ms_kategori_material');
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$c_row[] = $row;
}
return $c_row; //comment this return part while debugging
// print_r($c_row);
//exit;
}
else
{
return null;
}
}
It would be better if you print your data array to get exact idea of what you are getting in that array. Try following format, might be helpful
function getPosts() {
$query = $this->db->get('posts');
$posts = array();
foreach ($query->result() as $row) {
$posts[] = array(
'id' => $row->id,
'title' => $row->title,
'content' => $row->content
);
}
return $posts;
}