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.
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;
}
}
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>
What's the best practice to output the each row's real counter from an array ? I have the object array of retrieved users from database that I'am echoing into the <table />
<tbody>
<?php $i = 1;?>
<?php foreach($members as $member):?>
<tr>
<td class="column-counter">
<?php echo $i;?>
</td>
<td class="column-check">
<input type="checkbox" class="checkbox" name="checked[]" value="<?php echo $member->id;?>" <?php if(intval($member->id) === intval($this->session->userdata('login_status')['id'])):?>disabled<?php endif;?>>
</td>
<td class="column-username">
<?php echo $member->username;?>
</td>
<td class="column-email">
<?php echo $member->email;?>
</td>
<td class="column-id">
<?php echo $member->id;?>
</td>
</tr>
<?php $i++;?>
<?php endforeach;?>
</tbody>
What I've done so far is that $i =1 incrementing each time the loop is triggered.
But the problem is that if I go to the second page, it starts from "1" again, instead of let's say 21 (in case it shows 20 rows per page).
How can I make it right so it will continue counting from last row of previous page ?
By the way I'am using a codeigniter if that helps.
==== UPDATE ====
The model models/members_model.php i'am using in controllers/members.php to retrieve the users holds this function mixed with pagination:
public function members($data = array(), $return_count = FALSE){
$this->db
->select('
members.id,
categories.title as role,
members.catid as role_id,
members.firstname,
members.lastname,
members.username,
members.email,
members.status,
members.image
')
->join('categories', 'members.catid = categories.id');
if( empty($data) ){
// If nothing provided, return everything
$this->get_members();
}else{
// Grab the offset
if( !empty($data['page']) ){
$this->db->offset($data['page']);
}
// Grab the limit
if( !empty($data['items']) ){
$this->db->limit($data['items']);
}
if( $return_count ){
return $this->db->count_all_results($this->_table_name);
}else{
return $this->db->get($this->_table_name)->result();
}
}
}
It sounds like $data['page'] holds the information you need since this is related to the specified offset. You didn't reference the name of this array in your global scope, so I will just call this $data_array in my answer.
You can determine the starting value for $i as follows
$i = $data_array['page'] + 1;
I'm in a class called database programming. We got a data set and and put it into our servers. Now I have to use a jquery plugin to help visualize that data. I am using Graph Us plugin and trying to use the "Fill In" option.
My professor helped me create this function:
<?php
include 'connect.php';
$country_query = "SELECT DISTINCT Country FROM FemaleMaleRatioNew";
$result = mysqli_query($sql_link, $country_query);
$new_row = array();
while ($row = mysqli_fetch_assoc($result)) {
$country = $row['Country'];
$query = sprintf("SELECT Year, Value FROM FemaleMaleRatioNew WHERE Country = '%s'", $country);
$country_result = mysqli_query($sql_link, $query);
while ($country_row = mysqli_fetch_assoc($country_result) ) {
$new_row[$country][] = array('year' => $country_row['Year'],
'value'=> $country_row['Value']
);
}
}
//print_r($new_row);
?>
the print_r($new_row); is only there to make sure it works and it does, it prints out the array when activated.
He then guided me to create the table like this:
<body>
<table id="demo">
<?php foreach($new_row as $row):?>
<tr>
<td><?=$row['year'];?></td>
<td><?=$row['country'];?></td>
</tr>
<?php endforeach;?>
</table>
<script type="text/javascript">
$(document).ready(function() {
// Here we're "graphing up" only the cells with the "data" class
$('#demo td').graphup({
// Define any options here
colorMap: 'heatmap',
painter: 'fill',
// ...
});
});
</script>
</body>
What else do I need to do to get the table to work? I can't seem to figure it out. All it does is come out blank.
I'm sorry if this question isn't worded correctly or if I have not been clear on anything please let me know.
You have multiple rows for each country in your $new_row variable. You have to iterate over countries first and then over the individual rows of data:
<?php foreach($new_row as $country => $rows): ?>
<?php foreach($rows as $row): ?>
<tr>
<td><?=$country;?></td>
<td><?=$row['Year'];?></td>
<td><?=$row['Value'];?></td>
</tr>
<?php endforeach;?>
<?php endforeach;?>
Also please note that you need colon ':' not semicolon ';' after the foreach statement. This syntax (which is less known) is described here: http://php.net/manual/en/control-structures.alternative-syntax.php
If you want to display some sort of aggregate (for example sum) per country and you want to calculate it in PHP (as opposed to MySQL) you can do it like this:
<?php foreach($new_row as $country => $rows):
$sum = 0;
foreach($rows as $row):
$sum += $row['Value'];
endforeach;
?>
<tr>
<td><?=$country;?></td>
<td><?=$sum;?></td>
</tr>
<?php endforeach;?>
You should be using a single JOINed query to do this stuff, but you may not have gotten that in class yet. Since it's homework, I won't give you the flat-out answer, but here's the pseudo-code:
$countries = SELECT DISTINCT Country FROM YourTable;
while($country_row = fetch_row($countries)) {
echo $country_row['Country'];
echo <table>;
$status = SELECT Year, Value FROM YourTable WHERE Country=$country_row['Country'];
while($stats_row = fetch_row($status) {
echo <tr><td>$stats_row['Year']</td><td>$stats_row['Value']}</td>
}
echo </table>
}