How to retrieve data from database using Ajax and OOP php - php

I am going to develop web page with ajax and php. but there is problem in my get_data_in_table function. it will show data in a table structure. but it shows error when i run this code. others are working properly. it shows error in while loop. but i am unable to find any issue.
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
?>

I guess I found it:
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
} // This was missing
?>

Related

Undefined property: stdClass::$point

I want to display user data in table but when I display point i got the error
Undefined property: stdClass::$point (View:
C:\xampp\htdocs\Gelecek_Üyeliği_Sistemi_f\Gelecek_Üyeliği_Sistemi_f\resources\views\backend\usersaction\total.blade.php)
this is the function i use it to display the data in view
public function ViewActionUserTotal() {
$total = DB::table("action_users")
->join("users", "users.id", "action_users.user_id")
->select("users.id","users.name","users.email", DB::raw("sum(action_users.point)"))
->groupBy("users.id","users.name","users.email",)->get();
return view('backend.usersaction.total',compact('total'));
}
here is the foreach in the table
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>Total Point</th>
</tr>
</thead>
<tbody>
#foreach($total as $tot)
<tr>
<td>{{$tot->name}}</td>
<td>{{$tot->email}}</td>
<td>{{$tot->point}}</td>
</tr>
#endforeach
</tbody>
</table>
when i use dd for the data it show like that
I think you forgot to make an alias for sum query .Your query should be like this :
$total = DB::table("action_users")
->join("users", "users.id", "action_users.user_id")
->select("users.id","users.name","users.email", DB::raw("sum(action_users.point) as point"))
->groupBy("users.id","users.name","users.email",)->get();
For more :https://laravel.com/docs/7.x/database#running-queries

Array into function (export xls file)

I do not understand how to pass all the ARRAY within all the columns in order to export them as an xls file.
I was thinking about something like this:
<a href='".site_url('main_page/EXPORT')
Take a look:
I am fetching all the different rows from a table and they are displayed in the screen, check it out (it does work) - this part is inside page_view (VIEW folder):
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">TABLE: USERS</h2>
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="table">
<thead>
<th>id</th>
<th>user</th>
<th>name</th>
<th>lastname</th>
<th>type</th>
<th>status</th>
<th>date</th>
</thead>
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
Take a look at this (this is probably wrong)
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
<a href='".site_url('main_page/EXPORT')."/$record->id'> //THIS IS POBABLY WRONG
<button type='button' class='btn btn-primary'>EXPORT</button></a>
</tr>";
</tbody>
Then, I want to pass that information inside this function:
public function EXPORT(){
$data['records']=$this->MODEL->EXPORT_XLS();
$this->load->view('page_view',$data);
Could you please help me?

Issue trying to fetch data codeigniter

I got this errors in the view file (and so many more about the indexes id,username, name, lastname, password, type, status, date):
https://i.gyazo.com/d8bda30b1fafce47ed2125d590c5b4e4.png
I gotta show ONLY the rows that contain "simple" users ("type = 1") which are stored inside the table "usuarios"
Table "usuarios".
"usuarios" includes:(id,username,name,lastname,password,type,status,date).
When i enter the system as an ADMIN the program must show a table with all the "simple" users stored in the table "usuarios".
I need something like this:
https://i.gyazo.com/36f11b368a339964f1b734cea0177734.png
This is my code:
My view file ("user_view"):
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
<thead>
<th>id</th>
<th>User</th>
<th>Name</th>
<th>Lastname</th>
<th>Password</th>
<th>Type</th>
<th>Status</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$records['id']."</td>
<td>".$records['username']."</td>
<td>".$records['name']."</td>
<td>".$records['lastname']."</td>
<td>".$records['password']."</td>
<td>".$records['type']."</td>
<td>".$records['status']."</td>
<td>".$records['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My model function:
public function getINFOUSER(){
$query = $this->db->get_where('usuarios',array('type'=>1));
if ($query->num_rows() > 0 ) {
$result = $query->result_array();
}
return $result;
}
Do not know what to do now :S
It is record, not records inside foreach:
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
Hope this will work.
There is some error in your code. you have written wrong variable name. I just corrected it.
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>

bootstrap display data from mysql in table

I am trying to display my mysql rows to html bootstrap table. Database connection is working, displaying data is working, but it is not fancy as i want. I'd like to maybe save in arrayData maybe and then print this arrayData in html tag. Please any suggestions much appreciated. I want to do this easiest way and most convenient for editing later on. php code to display data :
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["lastName"]. "<br>"; /* and so on..*/
}
} else {
echo "0 results";
}
and this is my html code for bootstrap
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>ales</td>
<td>king</td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">2</th>
<td>love</td>
<td>2</td>
<td>code</td>
<td></td>
</tr>
</tbody>
</div>
</table>
EDIT: i want this, but loaded from database not manually typed :)!
You can use a php loop this way
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php
....
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["id"]. '</td>
<td>' . $row["name"] .'</td>
<td> '.$row["lastName"] .'</td>
</tr>';
}
} else {
echo "0 results";
}
?>
</tbody>
</div>
</table>
I would do something like this:
( but you should really read some basic PHP )
<?php
echo "<table>";
// table header
echo "<tr><th>id</th><th>Name</th><th>Lastname</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["lastName"]."</td></tr>";
}
// table footer
echo "</table>";
?>

HTML not showing after PHP Function

I have a page that will not show the bottom half of my HTML after my PHP Code. The only way I could get it to show was to place a RETURN at the end of the WHILE Loop, which also ended the loop. I know it is probably something simple and just need to find out what it is. Thanks in advance for your help.
The HTML:
<table border='0'>
<thead>
<tr>
<th scope="cols">Select</th>
<th scope="cols">Name</th>
<th scope="cols">Units</th>
<th scope="cols">Amounts</th>
<th scope="cols">Calories</th>
<th scope="cols">Sugars</th>
</tr>
</thead>
<tbody>
<?php
//Establishs the DB connection
$sql = new Mysql();
//Queries the Foods
$sql->food();
?> <!--NOTHING SHOWS FROM HERE DOWN-->
</tbody>
</table>
<h2>Training Plan</h2>
<table id="dairy">
<thead>
<tr>
<th scope="cols">Select</th>
<th scope="cols">Name</th>
<th scope="cols">Units</th>
<th scope="cols">Amounts</th>
<th scope="cols">Calories</th>
<th scope="cols">Sugars</th>
</tr>
...more HTML....
The PHP Function:
function food() {
//Query's the DB
$result = $this->conn->query("SELECT * FROM ingredient") or die(mysql_error());
//Display's all of the Contents in the DB
while ($row = $result->fetch_assoc() or die(mysql_error()))
{
echo'<tr class="normal" id="row'.$row['id'].'" onclick="onRow(this.id);">'."\n\t\t\t\t\t\t\t".'<td><input type="checkbox" value="'.$row['id'].'" id="checkbox" /></td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Name'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Units'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Amount'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Calories'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Sugar'].'</td>'."\n\t\t\t\t\t\t";
echo'</tr>'."\n\t\t\t\t\t\t";
}
}
while ($row = $result->fetch_assoc() or die(mysql_error()))
The problem is in this line, if $result->fetch_assoc() returns falsy when there are no more rows(which I suspect it does) you script will be stopped. Leave out the die part
while ($row = $result->fetch_assoc())
It's not showing because you have an error. Is food a member of the $sql class?

Categories