I would like to create HTML table with my query results. The HTML table should look like this:
I don't really understand about Codeigniter's HTML Table Class, so I just create a loop for each column of the table on my view like this:
<tr>
<?php foreach ($nameloop as $row) { ?> <td><?php echo $row->username; ?> </td><?php } ?>
<?php foreach ($addressloop as $row) { ?> <td><?php echo $row->useraddress; ?> </td><?php } ?>
<?php foreach ($ageloop as $row) { ?> <td><?php echo $row->userage; ?></td> <?php } ?>
<?php foreach ($sexloop as $row) { ?> <td><?php echo $row->usersex; ?></td> <?php } ?>
</tr>
Turns out it was a very bad decision. It makes my table look horizontally instead vertically like it used to.
Here's my model:
<?php class Front_model extends CI_Model {
public function __construct()
{
parent::__construct();
$CI = &get_instance();
$this->db2 = $CI->load->database('db2', TRUE);
}
function namelist()
{
$query = $this->db->query("SELECT name as uername FROM table1 ORDER BY name");
return $query->result();
}
function addresslist()
{
$query = $this->db->query("SELECT address as useraddress FROM table1 INNER JOIN name
ON address.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
function agelist()
{
$query = $this->db->query("SELECT age as userage FROM table1 INNER JOIN name
ON age.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
function sexlist()
{
$query = $this->db->query("SELECT sex as usersex FROM table1 INNER JOIN name
ON sex.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
}
And then here's my function on my CONTROLLER:
public function index()
{
$data['nameloop'] = $this->front_model->namelist();
$data['addressloop'] = $this->front_model->addresslist();
$data['ageloop'] = $this->front_model->agelist();
$data['sexloop'] = $this->front_model->sexlist();
$this->load->view('home',$data);
}
This make me quite confused. Whether it's the HTML tag or I code it wrong.. I don't know...
Thank you for your help, guys...
You can really do this in a simpler way.
Your model can be simplified to get all the data with one method
<?php class Front_model extends CI_Model {
public function __construct()
{
parent::__construct();
$CI = &get_instance();
$this->db2 = $CI->load->database('db2', TRUE);
}
function userInformation()
{
$query = $this->db->query("SELECT name as username, address as useraddress, age as userage, sex as usersex FROM table1 INNER JOIN name ON table1.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
}
And then in your controller you can just call this one method instead
public function index()
{
$data['information'] = $this->front_model->userInformation();
$this->load->view('home',$data);
}
And finally in your view, you can just your one foreach
<tr>
<?php foreach ($information as $row) { ?>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->useraddress; ?></td>
<td><?php echo $row->userage; ?></td>
<td><?php echo $row->usersex; ?></td>
<?php } ?>
</tr>
Hope that helped.
Although I recommend using CI's table library!
UPDATE
If you want to work with your existing queries, then I recommend you modify your controller like this -
public function index()
{
$names = $this->front_model->namelist();
$addresses = $this->front_model->addresslist();
$ages = $this->front_model->agelist();
$sexes = $this->front_model->sexlist();
$length = sizeOf($names);
$data['information'] = [];
for ($i = 0; $i <= $length; $i++) {
$data['information'][$i] = [$names[$i], $addresses[$i], $ages[$i], ];
}
$this->load->view('home',$data);
}
Update your view like this -
<tr>
<?php foreach ($information as $info) { ?>
<td><?php echo $info[0]->username; ?></td>
<td><?php echo $info[1]->useraddress; ?></td>
<td><?php echo $info[2]->userage; ?></td>
<td><?php echo $info[3]->usersex; ?></td>
<?php } ?>
</tr>
Related
I'm trying to get this output
Province
1.city 1 33poits
2.city 2 33poits
3.city 3 33poits
and what my current output is
Province
1.city 1 33points
Province
2.city 2 33points
Province
3.city 3 33points
please help just a newbie here, below is my php code
<?php
$sql = "SELECT * FROM ccty WHERE ID=".$ID;
$results = $mysqli->query($sql);
if ($results->num_rows > 0) {
while ($row = $results->fetch_object()) {
?>
<table>
<tr><?= $row->PID; ?>
<td><?= $row->SPID; ?><?= $row->points; ?></td>
</tr>
<?php
}
} else {
echo "No record available!";
}
?>
For one, your table-structure is invalid.
To only print the PID once per group, store the previous PID in a variable and check against it - only print it if its different from before.
You should also be using a prepared statement instead of a direct query.
<?php
$sql = "SELECT PID, SPID, points FROM ccty WHERE ID=? ORDER BY PID";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $ID);
$stmt->execute();
$stmt->bind_result($PID, $SPID, $points);
if ($stmt->num_rows) {
echo '<table>';
$previousPID = null;
while ($stmt->fetch()) {
if ($PID != $previousPID) {
echo '<tr><td>'.$PID.'</td></tr>';
$previousPID = $PID;
}
?>
<tr>
<td><?= $SPID." ".$points; ?></td>
</tr>
<?php
}
echo '</table>';
} else {
echo "No records available!";
}
I want to select all the rows that emp id's are match to my session userdata (emp_id). Here's my code. I get so many errors and no row has been selected. Someone help me please. Thanks
Model:
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->row_array();
}
Controller:
public function show() {
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
$this->load->view('show',$data);
}
Views:
<?php foreach ($save as $row) { ?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php }>
Try This
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result();
}
you can try solution for your problem :
Changes your modal function :
Modal.php
public function get_save_samp($emp_id) {
$this->db->select("*");
$this->db->where('id', $emp_id);
$this->db->get('tblsavesample');
return $query->row();
}
Views:
<?php foreach ($save as $row) {?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code ?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php } ?>
row_array() returns the first row only. If you want all the records to be returned, use result_array() instead.
$result = $query->result_array();
return $result;
Link : click
Change your Modal Code like this :
public function get_save_samp($emp_id) {
//$query = $this->db->get_where('tblsavesample', array('emp_id' =>$emp_id));
$this->db->select("*");
$this->db->where_in('id', $emp_id);
$query = $this->db->get('tblsavesample');
return $query->result();
}
And Change your Controller code like this to check what data are we getting inside controller.
public function show()
{
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo "<pre>";
print_R($data['save']);
exit();
$this->load->view('show', $data);
}
row_array() used to return only single record. change your code in model as follows :
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result_array();
}
and check number of records returned in your controller as follows:
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo $data['save']->num_rows();
if this displays 0, then you have no matching records in your database table.
to test the query, try to execute the same query in mysql
I have a very simple search page running a query for one table in a DB. I have the query and fetch working. But if it doesn't find any matches to the term the user put in then I need it to say "No Rows Returned".
Here is my PHP CODE:
<?php
include('./includes/dbConnection.php');
$result = null;
if (isset($_GET['submit'])) {
// connect to database
$conn = dbConnect('localhost', 'db_admin', 'kfor.com', 'receiving');
// query the database
$stmt = $conn->stmt_init();
$searchTerm = "SELECT * FROM inventory WHERE pallet = {$_GET['pallet_id']}";
$result = $conn->query($searchTerm);
}
?>
Here is the PHP displaying the results and errors:
<?php
if ($result != null) {
if (!empty($result)) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['pallet']; ?></td>
<td><?php echo $row['serial']; ?></td>
</tr>
<?php
}
}
} else {
echo "No Results";
}
?>
Is there a better way to display "No Results Returned"? Right now it's just displaying that error upon refresh because $result does equal null.
Thanks for any help!
Remove $result = null;, it's not needed. You can check num_rows returned your query.
<?php
include('./includes/dbConnection.php');
if (isset($_GET['submit'])) {
// connect to database
$conn = dbConnect('localhost', 'db_admin', 'kfor.com', 'receiving');
$_GET['pallet_id'] = addslashes($_GET['pallet_id']);
// query the database
$stmt = $conn->stmt_init();
$searchTerm = "SELECT * FROM inventory WHERE pallet = {$_GET['pallet_id']}";
$result = $conn->query($searchTerm);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['pallet']; ?></td>
<td><?php echo $row['serial']; ?></td>
</tr>
<?php
}
} else {
echo "No Results Returned";
}
}
?>
I am creating a search function that will allow a user to search for a house in my database by postcode initially. The function can be seen below, when the function is executed and finds a true statement I get no errors however when I execute the search and I get a no fields been returned I am left with this error:
No Records Found
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 26
I want it to display No Records Found however I don't know how I should correct the above error.
search.php:
<table width="500" border="1" cellpadding="5">
<tr>
<th width="16" scope="row">id</th>
<td width="95">bedrooms</td>
<td width="140">description</td>
<td width="104">roadname</td>
<td width="71">postcode</td>
</tr>
<?php
require("classes/class.House.inc");
$obj = new House();
$obj->search($_POST['term']);
foreach($obj->data as $val){
extract($val);
?>
<tr>
<td scope="row"><?php echo $id; ?></td>
<td><?php echo $bedrooms; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $roadname; ?></td>
<td><?php echo $postcode; ?></td>
</tr>
<?php
}
?>
</table>
classes/class.House.inc:
<?php
include("connect/class.Database.inc");
class House extends Database {
public function read(){
$query = "SELECT * FROM houses";
$result = $this->mysqli->query($query);
$num_result = $result->num_rows;
if($num_result > 0){
while($rows =$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
}
}
public function search ($term){
$query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
$result = $this->mysqli->query($query);
$num_result = $result->num_rows;
if($num_result > 0){
while($rows =$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
} else{
echo 'No Records Found';
}
}
}
?>
in this variable ($obj->data) you just get null data.
First check if not empty and than use foreach and don't have error if yout method don't return null data
just check if (!empty($obj->data)
{
foreach code
}
$obj is a House object. It has no $data property, even if you use it. The search method sets this property, but only if records are found. If no records are found, the method echoes a value.
I would change it like this: Instead of echoing an error, make the method return false:
public function search ($term){
$query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
$result = $this->mysqli->query($query);
$data = false;
$num_result = $result->num_rows;
while($row =$result->fetch_assoc()){
$data[]=$row;
}
return $data;
}
Now, the function return an array of false if there is no data. You can now use it like this:
$obj = new House();
if ($data = $obj->search($_POST['term']))
{
foreach($obj->data as $val){
extract($val);
}
}
The changes I made:
- No longer set data as a property, since you also return it. You can still do that, if you like, but I think it's confusing to do both.
- Return false if there's no data.
- Change the variable rows to row, since it only contains one row.
if(is_array($obj->data)){
foreach code
}
else{
no record
}
i am using codeigniter. i have a table to be displayed in a view file. so i have a model where i fire a query to get the data from table.
function my_active_requests()
{
$user_id = $this->session->userdata('user_id');
$this->db->select('id,request_date,required_by_date');
$this->db->where('requested_by',$user_id);
return $this->db->get('requests');
///also tried with
//$query = $this->db->get('requests');
///$number_of_rows = $query->num_rows;
//return $query;
//return $number_of_rows;
//but no result
}
this is the model function.
this is my controller
function my_active_req()
{
$this->bloodline_model->my_active_requests();
//also tried without this//
$query = $this->db->get('requests');
//and this//
$number_of_rows = $query->num_rows;
$this->load->view('my_active_req');
}
and this is my view
<?php foreach ($query->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->request_date; ?></td>
<td><?php echo $row->required_by_date; } ?></td>
</tr>
</table>
but the error is undefined variable $query.
i think , i am not able to return the $query and other data from my model to controller and controller to my view.
Please provide me with the solution.
`
Change this line:
<?php
$this->load->view('my_active_req', array(
'query' => $query
));
The second parameter of the view() loader function allows you to pass variables into the view.
However, you're largely circumventing the purpose of MVC separation. You want the controller to "ask" for information from the model (not just straight from the database; otherwise, what's the point of a model?), and then "pass" it to the view. The controller knows what the view needs to function.
Model
function my_active_requests()
{
$user_id = $this->session->userdata('user_id');
$this->db->select('id,request_date,required_by_date');
$this->db->where('requested_by',$user_id);
$query = $this->db->get('requests');
if($query->num_rows) {
return $this->db->result();
}
return false;
}
Controller
function my_active_req()
{
$results = $this->bloodline_model->my_active_requests();
$this->load->view('my_active_req', array('user_data' => $results));
}
View
<?php
if($user_data) {
foreach ($user_data as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->request_date; ?></td>
<td><?php echo $row->required_by_date; } ?></td>
</tr>
</table>