How to retrieve student scores on each subject from database table infront of their names..
My Model
public function getscore($class, $term, $session)
{
$query = $this->db->query("SELECT scores FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND scores !=0");
return $query;
}
controller
public function showscores()
{
$class=$this->input->post('classes');
$term=$this->input->post('term');
$session=$this->input->post('session');
$data['query_scores'] = $this->model->getscore($class,$term, $session);
$this->load->view('scoresheet', $data);
}
view page
<table>
<tr>
<?php
foreach ($query_scores->result() as $row)
{
?>
<td><?php echo $row->scores; ?></td>
<?php
}
?>
</tr>
</table>
Note: student name is listed horizontally on the left side of d page, subject names is displayed vertically as the heading.
My challenge is to display the score o each subject under d subject name for each students.
Your assistance in this regard would be very appreciated.
public function getscore($class, $term, $session)
{
$query = $this->db->query("SELECT scores FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND scores !=0");
return $query-result();
}
Use result() to format and show thing from database.
Use following table format to display subject names
<table>
<thead>
<tr>
<td> SUB1 </td> <td> SUB2 </td> <td> SUB3 </td>
</tr>
</thead>
Now use your code for Corresponding Content.
The exact PHP code for displaying the subject score will depend on your database structure. For more clarification, please share your database table structure.
try this.
Model:
note: change field name and query structure to match yours
normaly this will require a join but this is a basic example for you to get the idea
public function getscore($class, $term, $session){
$query = $this->db->query("SELECT scores,student_id ,student_name, subject_id,subject_name , FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND scores !=0");
return $query;
}
Controller:
public function showscores(){
$class=$this->input->post('classes');
$term=$this->input->post('term');
$session=$this->input->post('session');
$data['query_scores'] = $this->model->getscore($class,$term, $session);
// create array to hold the new organized data;
$list = array();
// list['scores'] will hold the student scores;
// list['students'] = will hold the student names
// list['subjects'] = will hold the subjects names
// loop throught the result
foreach ($query_scores->result() as $row){
$list['scores'][$row->student_id][$row->subject_id] = $row->scores;
$list['students'][$row->student_id]= $row->student_name;
$list['subjects'][$row->subject_id]= $row->subject_name;
}
$this->load->view('scoresheet', $list);
}
view page:
<table>
<tr>
<?php
foreach ($subjects as $subject){
echo "<td>".$subject."</td>"; // will display subjects names on the first row
}
?> </tr> <?php
$column = 0;
foreach ($scores as $key=>$element){
echo "<tr>";
echo "<td>".$students[$key]."</td>"; // will display student names on the first col
foreach ($element as $subKey=>$subelement){
echo "<td>".$subelement."</td>";
}
echo "<tr>";
<?php
} ?>
</table>
Related
I have my little project coding with Codeigniter, I am just stuck in querying the total count of specific column to show in a View. Here are some pieces of my code where I get stuck:
Model:
function get_dailyprob()
{
$date = date('d-m-Y');
$q1 = $this->db->select('client')->like('created_at', $date)->group_by('client')->get('histprob')->result();
$q2 = $this->db->like('created_at', $date)->count_all_results('histprob'); //HERE I HAVE NO IDEA HOW TO GET A TOTAL COUNT OF EACH ROWS THAT ALREADY GROUPED BY COLUMN client
return array('dp_client' => $q1, 'dp_count' => $q2);
}
Controller:
$data['dailyprobs'] = $this->skejuler_m->get_dailyprob();
View:
<table class="table table-hover">
<?php if ($dailyprobs['dp_count'] > 0) {
foreach ($dailyprobs['dp_client'] as $dpc): ?>
<tr>
<td><?php echo $dpc->client; ?></td>
<td><span style="font-size:16px" class="badge bg-red"><?php echo $dailyprobs['dp_count']; ?></span></td>
</tr>
<?php endforeach; ?>
<tr><td><?php } else { echo 'No Issues!'; } ?></td></tr>
</table>
$dpc->client results some rows and each row has different count (filtered by query in Model)
$dailyprobs['dp_count'] is currently showing the whole results, whereas I need showing a total count grouped by client
Sorry if the explanation was confusing. I just added an image of View. As shown in the picture, both American Standard / Grohe & App Sinarmas have each 2 in total number = 4, whereas the actual total row is 1 of each column (client) = 2. I am sorry for my English.
It just solved by changing the query in Model:
function get_dailyprob()
{
$date = date('d-m-Y');
$query = $this->db->select('*, COUNT(*) as cnt')->like('created_at', $date)->group_by('client')->get('histprob');
return $query->result();
}
Then use foreach in View to get Count and Rows Result.
I'm trying to create a loop in a page that populates a form with several options from a database. In this case the form is intended to create an ASSEMBLY and will require the selection of already existent PARTS in the database.
The approach I'm trying to achieve is the following:
A. I created a class containing the following function in a file called classes.php:
<?php
$depth = "../";
require_once("connection.php");
////////////////////////////////////////////
class Parts
{
// SELECTS ALL PARTS FROM DATABASE
public function getPartInfo() {
global $con;
$partinfo = mysqli_query($con,"SELECT * FROM parts ORDER BY name");
$row = mysqli_fetch_assoc($partinfo);
return $row;
}
} // end of Parts
?>
B. In another file (assemblies.php) I'm trying to catch the value of all fields in the array so I can create a loop listing all rows so the user can select the parts required for the assembly. I'm trying the following code with no success:
<?php
session_start();
include "php_includes/classes.php";
$parts = new Parts();
$parts_detail = $parts->getPartInfo();
?>
<div class="row">
<div class="form-group col-md-12">
<table width="100%" id="partsTable">
<?php
foreach($parts_detail as $row) {
echo '<tr>
<td><input type="checkbox" name="parts[]" value="'.$row["id"].'" /></td>
<td><img src="https://lux365.s3.amazonaws.com/landing-pages/easilite/v1/img/easilite-logo.jpg" alt="" /></td>
<td>'.$row["name"].'</td>
<td><input type="number" name="partQuantity[]" min="0" /></td>
</tr>';
}
?>
</table>
</div>
</div>
Where $row["name"] is trying to get the field NAME from the parts table and $row["id"] tries to call the field ID from the parts table.
But when I try this, it results in an Illegal string offset error, for both, name and id.
Any help is appreciated since my OOP skills are extremely limited.
Thanks in advance
getPartInfo() just returns the first row returned by the query. Then your for ($parts_detail as $row) loop is looping over the columns in that row, not all the rows. So $row is a string containing a column value, and $row['name'] gets an error because $row isn't an associative array.
It should have a loop and return all the rows.
public function getPartInfo() {
global $con;
$partinfo = mysqli_query($con,"SELECT * FROM parts ORDER BY name");
$result = array();
while ($row = mysqli_fetch_assoc($partinfo)) {
$result[] = $row;
}
return $result;
}
Try...
class Parts
{
// SELECTS ALL PARTS FROM DATABASE
public function getPartInfo() {
global $con;
$partinfo = mysqli_query($con,"SELECT * FROM parts ORDER BY name");
$arrRow = array();
while($row = mysqli_fetch_assoc($partinfo)){
$arrRow[] = $row;
}
return $arrRow;
}
}
Hey friends I am trying to make two dropdown boxes. The first one is sensor type select. The second one is sensor names and it should be dependent on the first one .I am using MVC architecture.Here is my code I am using. At the viewpage.php I have:
<table>
<td>
<label><b>Sensors Types:</b></label>
</td>
<td>
<?php echo form_dropdown('sensor_types', $sensor_types, '#', 'id="sensor_types" onchange="copy();" ');?>
</td>
<td> </td>
<td> </td>
<td>
<label><b>Sensor Name:</b></label>
</td>
<td>
<?php echo form_dropdown('sensorzs', $sensorzs, '#', 'id="sensorzs"'); ?>
</td>
</tr>
</table>
and the code in the model.php is
function sensors_list($node_id) {
$this->db->select('sensor_index, sensor_name');
$this->db->where('node_id', $node_id);
//$this->db->where('sensor_type', $sensor_types);
$this->db->order_by('sensor_index');
$query = $this->db->get('node_sensor_dtl');
}
function get_all_sensors($node_id) {
$this->db->select('sensor_pk, sensor_name');
$this->db->where('node_id', $node_id);
$this->db->order_by('sensor_index');
$query = $this->db->get('node_sensor_dtl');
$sensors = array();
if($query->result())
{
$sensors[''] = '-Select-';
foreach ($query->result() as $sensor)
{
$sensors[$sensor->sensor_pk] = $sensor->sensor_name;
}
return $sensors;
}
else {
$sensors = NULL;
return $sensors;
}
}
and the code in the node.php the code is like this
function edit(){
if($this->CI->input->post('editform_id') == NULL) redirect('configuration/node/');
$view_data['page_title'] = "Node edit details";
$view_data['page_name'] = "";
$view_data['detail'] = $this->CI->model->get_node($this->CI->input->post('editform_id'));
$view_data['sensor_types'] = $this->CI->model->get_all_sensor_types();
$view_data['sensorzs'] = $this->CI->model->sensors_list($this->CI->input->post('editform_id'));
$this->CI->load->view('config_node_view', $view_data);
}
I´ve tried this for past few days but could get the result. Searched lot of site and blogs but I can't get the required results.
if you are looking to create dropdown boxes, I believe you should not be creating a table, instead you need the select tag.
I know this doesn't fully answer your question but I believe this should get you started.
If your php code is producing the results you want, populate them into select tags and take it from there.
Suppose I have this native query on my model:
public function print_query($id){
$sql = "Select Field1,Field2,Field3.. FROM Table WHERE id = '".$id."' ";
if($this->db->query($sql, array($id))->num_rows() > 0)
{
return $this->db->query($sql, array($id))->result();
}
}
And this on my Controller:
public function emp_preview(){
$EmpID = $this->input->post('txt_id');
$data['p_det'] = $this->db_sal_details->print_query($EmpID);
$this->load->view('employee/preview',$data,TRUE);
}
And in my View: I wanted to display the values of Field1,Field2,Field3,and so on.,
I used this code to display them;
<table>
<tr>
<?php if($p_det):?>
<?php foreach($p_det as $det):?>
<th><?php echo $det->Field1;?></th>
<th><?php echo $det->Field2;?></th>
<th><?php echo $det->Field2;?></th>
...
<?php endforeach;?>
<?php endif;?>
</tr>
</table>
This is all work fine and display the field and its values. But what if i have more than just 3 fields (50 fields e.g) and I want to dynamically populate the TH with out typing each Fields. How can i do this.,?
I know it's very simple for you experts ,.but I'm just a beginner here.. please bear with me and help me with this one., Thanks in Advance for any replies..
I think you could use foreach to iterate through the property which contains Field1, Field2.etc of the object $det
Model
public function print_query($id){
$sql = "Select Field1,Field2,Field3.. FROM Table WHERE id = '".$id."' ";
if($this->db->query($sql, array($id))->num_rows() > 0)
{
return $this->db->query($sql, array($id));
}
}
View
<table>
<tr>
<?php if($p_det):?>
<?php foreach($p_det->result_array() as $det):?>
<?php foreach($det as $field => values){?>
<th><?php echo $columns[$field] = $values;?></th>
...
<?php
}
endforeach;
?>
<?php endif;?>
</tr>
</table>
I am trying to create a website using codeigniter. I have two tables in the database. Theses tables are Category(Category Name, Category_Id) and Item(Item_Name, Category_id). Now, in my view I want of show the items in different tables for the different categories.
So far, I have come up with this, but obviously it is not working. The code in the model is this.
$query = 'select category_id as id, category_name as name from category;';
$result = $this->db->query($query);
$data['category'] = $result->result_array();
foreach($data['category'] as $d)
{
$query = "select item_name as name from item where item_catagory_id = '{$d['id']}';";
$result = $this->db->query($query);
$data["{$d['id']}"] = $result->result_array();
}
$this->load->view('view', $data);
The code in the view is like this.
<?php foreach($category as $c):?>
<h2><?php echo $c['name']; ?></h2>
<table>
<tr>
<td>Item Name</td>
</tr>
<?php foreach($c['id'] as $d): ?>
<tr>
<td><?php echo $d['name'];?></td>
</tr>
<?php endforeach;?>
</table>
<?php endforeach;?>
But it says that " Invalid argument supplied for foreach()". It also says that the error is in this line :
<?php foreach($c['id'] as $d): ?>
If I understand what you want to accomplish, Then here's what you want:
Change
$data["{$d['id']}"] = $result->result_array();
To:
$d['items'] = $result->result_array();
Then in your view, you'll simply do the second foreach like this:
<?php foreach($c['items'] as $d): ?>
This way you're adding the items to the category itself with a known key.