Issue trying to fetch data codeigniter - php

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>";
}
}
?>

Related

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?

How to retrieve data from database using Ajax and OOP 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
?>

Outputting Data into an HTML table using php/html

I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.

PHP - How can I nest a while loop inside an if isset condition?

I have this table element with the following code:
<?php
if(isset($_POST["submit"])){
if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
require_once("conn.php");
$sql2 = "SELECT * FROM customer;";
$results = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
echo "no errors found";
}
}
?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
Above this table I have the php code that makes the sql queries inside an if isset condition so that it only loads after pressing submit on the form. I would like to do the same to the table. That is to only make it load after pressing submit. because on page load it is trying to do the mysqli_fetch_array on a non existent $result yet
Wrap the whole table inside:
<?php if (isset($result)) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
I have used isset($result) based on what you have said. You can check for the POST values by checking for count($_POST), or something similar (not a good idea to check for isset($_POST["submit"])). If you are fetching for AJAX Response, it is always better to use a different separate file for this.
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>

Outputting more than one User Ban Data using PDO

I am trying to display the current users bans. They do display, but it only outputs 1 detail, when I'm trying to fetchAll data for that current user.
Here is my code
function getPlayerBans($username, $conn) {
$getPlayerBans = $conn->prepare("SELECT id, player, bannedby, comment, expires, time FROM `bans` WHERE player = :Player");
$getPlayerBans->bindValue(":Player", $username);
$getPlayerBans->execute();
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
foreach($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
}
if($getPlayerBans->rowCount() == 0)
{
echo('No Results Available');
}else{
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>
<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>
</tbody>
</table>";
}
}
You have to put the foreach loop around the code that prints each row of the table.
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
if (count($rows) == 0) {
echo('No Results Available');
} else {
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>";
foreach ($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
echo "<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>";
}
echo "</tbody>
</table>";
}

Categories