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
Related
Hi i am using foreach in php oops to output data from the mysqlbut each data outputs twice please check my code and help it i have tried but no correct result
Here is the code below i have used
class getdata extends db{
public function getdata(){
$sql = "SELECT * FROM users";
$results = $this->connect()->query($sql);
$numrows = $results->num_rows;
if($numrows > 0){
while($row = $results->fetch_assoc()){
$data[] = $row;
}
return $data;
}
else{
echo 'no values';
}
}
}
class showusers extends getdata{
//show users
public function showusers(){
$datas = $this->getdata();
foreach($datas as $data){
echo $data['id'].'<br>';
echo $data['name'].'<br>';
}
}
}
$showusers = new showusers();
$showusers->showusers();
Don't give your function the same name as your class.
With $showusers = new showusers(); you are already executing the showusers function.
To cite php.net:
For backwards compatibility with PHP 3 and 4, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.
Source:https://www.php.net/manual/en/language.oop5.decon.php
So your function showusers() is treated as a constructor for your showusers class and therefore is executed twice. Once when you create an object of the class and once when you call the method.
your code is a bit convoluted I'd suggest passing the database connection object rather than extending continiously.
In this case your constructor showUsers() outputs a list of users. therefore it repeats because you are calling this function twice.
$showusers = new showusers(); // prints users
$showusers->showusers(); // prints users again
move your display function
class showusers extends getdata{
$data;
//initialize
public function showusers(){
$this->data = $this->getdata();
}
//show users
public function displayUsers(){
foreach($this->data as $data){
echo $data['id'].'<br>';
echo $data['name'].'<br>';
}
}
}
$showusers = new showusers();
$showusers->displayUsers();
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
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 stated to build up my Database Class.
Here is a part of my database class.
public function query($sql)
{
return $this->getPdo()->query($sql);
}
My Class is working but i want to improve it.
This is external part of class:
$db = new Database();
$q = $db->query('SELECT * FROM table');
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$results[] = $r;
}
echo "<pre>";
print_r($results);
echo "</pre>";
I want to get while part inside database class like
public function query($sql)
{
return $this->getPdo()->query($sql);
}
public function getAll() {
while($r = $this->query($sql)->fetch(PDO::FETCH_ASSOC)){
$results[] = $r;
}
return $results;
}
But i know this part is wrong : $this->query($sql)->fetch(PDO::FETCH_ASSOC); How can i fix it ? I have to declare a class varible like $sql, and i have to assign sql statement to $sql varible.
But i couldn't. How can i do that ?
$q = $db->query('SELECT * FROM table');
$results = $q->fetchAll();
echo "<pre>";
print_r($results);
echo "</pre>";
One of your biggest problems is that when you write
public function getAll() {
while($r = $this->query($sql)->fetch(PDO::FETCH_ASSOC)){
$results[] = $r;
}
return $results;
}
you are causing the database to run the query with each iteration of the loop.
To correct the code as it is defined simply run the query outside of the loop then use fetch as the loop condition:
public function getAll() {
$stmt = $this->query($sql);
while($r = $stmt->fetch(PDO::FETCH_ASSOC)){
$results[] = $r;
}
return $results;
}
Another problem that is probably probably causing you grief is that the $sql value appears to be coming from out of thin air. You should pass it into your getAll function to have access to it.
To avoid all this noise you might want to just follow #YourCommonSense's suggestion and use the available PDO getAll method.
public function getAll($sql){
return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
You need to pass the $sql parameter to your getAll() method, and then use return to get the result back out. So this method becomes:
public function getAll($sql) {
while($r = $this->query($sql)->fetch(PDO::FETCH_ASSOC)){
$results[] = $r;
}
return $results;
}
Then you use it externally like:
$db = new Database();
$results = $db->getAll('SELECT * FROM table');
echo "<pre>";
print_r($results);
echo "</pre>";
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'];
}