In PHP how to parse database data into various tables - php

I am new to php. I have following type of database table data which I want to display in PHP. Table data is available in a variable "Outputdata"
Now I want to display this data in php in the following way.
Product ID should not be repeated and "Child Columns" should be nested on the basis of "category".
Following is my code:
Model.php Code:
public function getproddata()
{
$Outputdata=$this->query("CALL Product();");
return $Outputdata;
}
Controller COde:
public function Procdata() {
$Outputdata=$this->Systemstate->getproddata($testId);
$this->set('Outputdata',$Outputdata);
}
PHP Code:
<div class='data1'>
<table class='data11 defaultTable'>
<th colspan='2'>Data <?php echo $i ?></th>
<tr>
<td><?php echo "Product ID" ?></td>
<td><?php echo $current['Product ID'] ?></td>
</tr>
<tr>
<td><?php echo "Percentage" ?></td>
<td><?php echo $current['Percentage'] ?></td>
</tr>
</table>
<div class='diagnose_toggle' onClick='toggleDiagnose($i)' id='detailsButton$i'></div>
<table id='details$i' class='defaultTable'>
<tr>
<th><?php echo "Category ID" ?></th>
<th><?php echo "Child Category ID"?></th>
<th><?php echo "Child Category Name"?></th>
</tr>
<tr>
<td><?php echo $current['Category ID']?></td>
<td><?php echo $current['Child Category ID']?></td>
<td><?php echo $current['Child Category Name']?></td>
</tr>
</table>
<?php $i++; ?>
Database Query:
Select Product_ID, Percentage, CategoryID, Child_Categ_ID, Child_categ_name
from products order by Product_ID, CategoryID
Problem is all product ids are displayed in separate rows and no nesting is also there.
Any idea to add the nesting and grouping logic?
Thank you

``assuming for your 1st table your query is a select all query you would get all values in an array then user foreach loop
var_dump will help you view your query that you captured
foreach ($variable as $key as $value){
<table>
<tr>
<td><?php echo $value->column_name1 ?><td>
<td><?php echo $value->column_name2 ?><td>
<td><?php echo $value->column_name3 ?><td>
<td><?php echo $value->column_name4 ?><td>
</tr>
</table>
}
if fetching from 2 tables if (primary key == foreign ) use && inside on how precise you want to filter

Related

Codeigniter double rows result when using mysql join in model

I'm working on codeigniter web app and I was doing some JOIN on model to show specific data from 3 tables but it gives me double row which should be only 1 row per data. It didn't happen when I only have 1 row data in my database but when I input the second data, it showed me 2 rows per data so I have 4 rows
The table name is siswa and I want to JOIN another table
This is the model for JOIN the database
function delete_siswa($id)
{
return $this->db->delete('siswa',array('id'=>$id));
}
function get_data_siswa()
{
$this->db->select('sw.id, sw.nama, sw.j_kelamin, sw.tmp_lahir, sw.tgl_lahir, sw.agama, sw.alamat, sw.no_tlp, sw.email, ot.n_ibu, ot.n_ayah, sk.nama_sek');
$this->db->from('siswa sw');
$this->db->join('orang_tua ot', 'ot.id=sw.id_orang_tua');
$this->db->join('sekolah sk', 'sk.id=sw.id_sekolah');
return $this->db->get('siswa')->result_array();
}
This is the controllers
function index()
{
$data['siswa'] = $this->Siswa_model->get_data_siswa();
$data['_view'] = 'siswa/index';
$this->load->view('layouts/main',$data);
}
This is the view
<tr>
<th>No</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Agama</th>
<th>Alamat</th>
<th>No. Telepon</th>
<th>Ibu</th>
<th>Ayah</th>
<th>Email</th>
<th>Nama Sekolah</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach($siswa as $s){ ?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $s['nama']; ?></td>
<td><?php echo $s['j_kelamin']; ?></td>
<td><?php echo $s['tmp_lahir']; ?></td>
<td><?php echo $s['tgl_lahir']; ?></td>
<td><?php echo $s['agama']; ?></td>
<td><?php echo $s['alamat']; ?></td>
<td><?php echo $s['no_tlp']; ?></td>
<td><?php echo $s['n_ibu']; ?></td>
<td><?php echo $s['n_ayah']; ?></td>
<td><?php echo $s['email']; ?></td>
<td><?php echo $s['nama_sek']; ?></td>
<td>
<a class="btn btn-warning" href="<?php echo site_url('siswa/edit/'.$s['id']); ?>">Edit</a> |
<a class="btn btn-danger" href="<?php echo site_url('siswa/remove/'.$s['id']); ?>">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
This is the screenshoot of this error
It should be only one row per data but I don't really know because I'm still new for codeigniter. No error in the log
can you take the distinct record from the table, i think the join query will duplicate the data
$this->db->distinct();
will work for this

how to looping array string with increment on table

I have an array like this
$Peringkat = array("Pertama","Ke-dua","Ke-tiga","Ke-empat","Ke-Lima");
and a table like this
<table class="table table-striped table-bordered text-center">
<tr>
<th>No</th>
<th>Nis</th>
<th>Nama</th>
<th>Kelas</th>
<th>Nilai Rata-rata</th>
<th>Nama Sekolah</th>
<th>Peringkat</th>
</tr>
<?php foreach ($Peringkat as $key => $value) {
while ($baris = $sql->fetch_assoc()) { ?>
<tr>
<td><?php echo $n++ ?></td>
<td><?php echo $baris['nis'] ?></td>
<td><?php echo $baris['nama_siswa'] ?></td>
<td><?php echo $baris['nama_kelas'] ?></td>
<td><?php echo $baris['Hasil'] ?></td>
<td><?php echo $baris['nama_sekolah'] ?></td>
<td><?php echo $value ; ?></td>
</tr>
<?php } }?>
</table>
How would I make a loop in ascending order to the value of the data string?
To sort your array of strings in ascending order, use asort
$Peringkat = array("Pertama","Ke-dua","Ke-tiga","Ke-empat","Ke-Lima");
asort($Peringkat); //ascending order
Now you can proceed as before.
Live demo
Note that if you sort $Peringkat, your table rows will be the same as before with the only difference being the last cell of each row. Depending on the nature of your $sql data, this could result in a mismatch.

Cannot connect rows in html with php (logical error display)

The html table rows aren't displayed properly. I want to combine the two while loops in the table row but sadly the Update and Delete button are not arranged properly.
here's my code I used two queries that's why it has two while loops
$sql_sel=mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name FROM students, enrollments, subjects WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
$sql_sel1=mysql_query("SElECT * FROM enrollments");
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<?php
while($row=mysql_fetch_array($sql_sel1)) //for the second query
{ <!-------The Update and Delete Button are not displayed properly------>
?>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
<?php
}
?>
</tr>
<?php
}
?>
Here is the visual scenario of the problem:
The desired output must be like this:
In table header use colspan='4' for the last column.
Also be sure that you fill the table with empty columns, where you don't have information to fill with.
Edit 1:
Sorry, I haven't seen what's really the problem was. Here should be the working code:
while($row=mysql_fetch_array($sql_sel))
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<!-- You were already in a while loop -->
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
As you can see you were already in a while loop, and the second one was unnecesary.
Edit 2:
There is a single SQL query now:
<?php
// UPDATED SQL QUERY
$sql_sel = mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name, enrollments.enroll_num
FROM students, enrollments, subjects
WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
while($row = mysql_fetch_array($sql_sel)){
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
Why it didn't worked?
You were reading the whole informations for every student. Then you were reading the whole informations in enrollments table.
You started writing the first row with student information, and inside it you told the server to start writing all the information he had regarding enrollments (without even be linked to that student's id).
When the server reached the second row, all the information available for enrollments were depleted.
Now you have them linked in your first query. Please ask in comments if you need further explanations.
Try this,
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.gender, sub.sub_code, sub.subject_name, ets.enroll_num
FROM students sts
JOIN enrollements ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.sub_code = ets.sub_code)
GROUP BY sts.stud_id, sub.sub_code";
$sql_sel=mysql_query($sql);
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
I have added enroll_num column in select, And you dont need two queries for this. One query with proper join will be fine.

get unique ID's of rows in php foreach loop for JQuery

Good Afternoon,
I have 3 foreach loops on my page, the first gets the teams, the second gets each person in the team, and the third gets each unique reference to a task that the agent has completed. I have collected this data and it is being displayed fine. I now want to add some JQuery to it so it will hide the agents and references unless the relevant team or agent is clicked on.
So if I load the page everything will be hidden apart from the teams, when I click on a team it will show the agents, when I click on an agent it will show the references.
I am having trouble assigning unique ID's to each row and finding those in the JQuery script.
Here is my code...
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $aTeamData['iBusinessStreamId'];?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId]?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $aPropertyData['iUserId'];?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I have given each of the rows a unique class by adding in the unique identifier from the database. I just dont know how to find these within the Jquery script.
EDIT:
Maybe not explained myself properly, I would like help with how to set up this script but obviously a lot better.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick',function(){
$('.Agent').toggle('show');
$('.Property').toggle('show');
});
});
</script>
But in this case it will show/hide all of the rows as they will all have the same id's, whereas now I have added on the unique id on the end with the php code in the class, I dont know how to call those as they all are called different classes.
So if I click on TeamClick1, it shows the actual rows for that team (Agent1), and not all of them. but obviously I cant type out all of the unique id's I just dont know how to get them from the php in JQuery.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick(UNIQUE ID)',function(){
$('.Agent(UNIQUE ID)').toggle('show');
$('.Property(UNIQUE ID)').toggle('show');
});
});
Hope this makes sense.
I am hasty but it is like this
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $iBusinessStreamId;?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId; ?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId2 => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $iUserId2 ;?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I'm putting the identifiers in classes, but if you want put in IDs you can, is the same thing, like this id="<?php ... ?>"

How to break out of a foreach-loop when all filtered objects have been deleted?

I am using the following foreach-loop to display some products and their attributes in a html-table, which works without problems:
<?php
$obj=new Crud;
$obj->read();
?>
<div class="product_table">
<table class="center">
<tr>
<td>ID</td>
<td>Category</td>
<td>Name</td>
<td>Description</td>
<td>Price</td>
<td>Image</td>
<td>action</td>
</tr>
<?php
foreach($obj->data as $val){
extract($val);
?>
<tr>
<td scope="row"><?php echo $id; ?></td>
<td><?php echo $category_id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $price; ?></td>
<td><?php echo $image; ?></td>
<td>Edit | Delete</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
However, when I delete all products, the foreach loop will display the following error-message, because logically there are no products left:
Notice: Undefined property: Crud::$data in ...
Warning: Invalid argument supplied for foreach() ...
I already tried to check if Crud::$data or $val are empty like so:
if (empty(Crud::$data) {
break;
echo "No products listed.";
}
But either no products are listed any longer (although present) or the error message remains.
How do I break out of the foreach-loop when no items are left?
Depends on if $obj->data is not an array or doesn't exist:
If not an array, don't run the loop:
if(is_array($obj->data)) {
foreach($obj->data as $val){
If not set or not an array, don't run the loop:
if(isset($obj->data) && is_array($obj->data)) {
foreach($obj->data as $val){

Categories