I have an array of fields that came from a DB query.
I would like to find the best solution to create multiple tables based on unique values from a field $office_id.
So basically, I have tried using a foreach loop, store the office_id and check each time around the loop if it is unique or not, then close the last table html, start a new table html etc.
I hate this solution because it means the array must be ordered by office_id and it is quite ugly.
Can anyone suggest a better way to do this using cleaner, smarter code. Do I need to do some sort of transformation on the array? Is there a better way to pull out the unique office_id field?
I also want to use the office_id as a header for each table, so that the output looks similar to the following:
office_id One
-----------------------------------
settle | price | Lister | date
-----------------------------------
| | |
office_id Two
-----------------------------------
settle | price | Lister | date
-----------------------------------
| | |
This is what I have (ugly), and as I type, I realise I'm missing the close tag:
<?php $thisOffice = ""; ?>
<?php foreach ($fees as $fee): ?>
<?php if($thisOffice !== $fee->office_id) : ?>
<table class="table table-striped table-condensed">
<tr>
<th>Settlement</th>
<th>Office</th>
<th>Price</th>
<th>Lister</th>
<th>Sold</th>
</tr>
<?php endif; ?>
<tr>
<td> <?= $fee->settle_date ?></td>
<td> <?= $fee->office_id ?></td>
<td> <?= $fee->price ?></td>
<td> <?= $fee->lister ?></td>
<td> <?= $fee->sale_date ?></td>
</tr>
<?php $thisOffice = $fee->office_id; ?>
<?php endforeach; ?>
It will make your life much easier if you split the array into three dimensions:
$offices[$id][] = $fee;
This way you can use:
foreach ($offices as $id => $office) {
foreach ($office as $fee) {
// Build table
}
}
Related
hi i'm trying sort mysql data in table but my problem is that my query is sorting all the data my table looks like
Word | Meaning | Synonym | Antonym
definitely | without doubt | certainly |possibly
great | of an extent, amount | considerable |little
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
so when i search lets say word starting wit z then i get
Word | Meaning | Synonym | Antonym
zeal | great energy | passion | indiference
zealot | a person who is fanatical|fanatic | moderate
zealous | having or showing zeal. | fervent | apathetic
now i want to perform sorting on these searched data but my sort query is sorting the all data
my store procedure looks like
CREATE DEFINER=`root`#`localhost` PROCEDURE `SortContent`()
BEGIN
SELECT * from dictionarysearch ORDER BY word ASC;
END
and my php code is like
<?php
if(isset($_GET['search_btn'])){
$search=$_GET['search'];
$result=GetWords(mysqli_escape_string($conn,$search));
}
/*if(isset($_GET['q'])){
$id=$_GET['q'];
$result=GetWordsById($id);
}*/
if(isset($_GET['sort'])){
$sort=$_GET['sort'];
}
if(isset($_GET['sort'])){
if($sort=="asc"){
$result=SortContent();//Here i'm calling a function which is calling the store procedure
}
if($sort=="desc"){
$result=SortContent2();
}
}
else{
$result=GetAdminWords();
}
if(mysqli_num_rows($result)>0)
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<?php
while($row=mysqli_fetch_array($result)){
?>
<tbody>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td><i class="fa fa-edit"></i> <a onClick="javascript: return confirm('Please confirm deletion');" href="view.php?id=<?php echo $row['id'];?>"><i class="fa fa-trash"></i></a> </td>
</tr>
</tbody>
<?php
}?>
</table>
so i want to know how i can write a query so that it sorts only selected data and i have set id autoincrement
There is no need to use a stored Procedure for this. I suggest that the following code will work for you...
<?php
if(isset($_GET['search_btn']) && strlen($_GET['search'])){
$search = mysqli_escape_string($conn,$_GET['search']);
$sql = "SELECT * FROM dummydata WHERE info LIKE '".$search."%'";
if(isset($_GET['sort']) && in_array($_GET['sort'], array('ASC', 'DESC'))){
$sort=$_GET['sort'];
$sql .= " ORDER BY info ".$sort;
}
//echo "<h3>".$sql."</h3>";
if (($result=mysqli_query($conn,$sql))!==false) {
?>
<thead>
<tr>
<th>Word</th>
<th>Meaning</th>
<th>Synonym</th>
<th>Antonym</th>
</tr>
</thead>
<tbody>
<?php
while ($row=mysqli_fetch_assoc($result)){
//echo "<pre>".var_export($row,true)."</pre>";
?>
<tr>
<td><?php echo $row['word'];?></td>
<td><?php echo $row['meaning'];?></td>
<td><?php echo $row['synonym'];?></td>
<td><?php echo $row['antonym'];?></td>
<td>Your action url here.... </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else {
echo "<h3>Problem with SQL</h3>";
}
}
?>
I hope that this helps.
I have following problem .
I print data from a table name exam having student_id and marks_obtained for each student_id and. My output is as follow
but i want the table in different format
-----------------------
subject | marks | total
-----------------------
toward down side
as this see output
but am confused in code please help me
my CODE IS FOLLOW:
$roll=$_POST['roll'];
$exam=$_POST['exam'];
$int1 = intval(preg_replace('/[^0-9]+/', '', $exam), 10);
$student_info="select student_id,name,father_name,mother_name,roll,class_id,birthday,parent_id from student where roll='$roll'";
$result_student=mysqli_query($link, $student_info) OR trigger_error('login info error');
$data_student= mysqli_fetch_array($result_student,MYSQL_BOTH);
$parent_info="select name from parent where parent_id='$studentparent'";
$result_parent=mysqli_query($link, $parent_info) OR trigger_error('login info error');
$data_parent= mysqli_fetch_array($result_parent,MYSQL_BOTH);
$parentname= $data_parent['name'];
$subject_info="select subject_id,name from subject where class_id='$studentclass'";
$result_subject=mysqli_query($link, $subject_info) OR trigger_error('login info error');
$data_subject= mysqli_fetch_array($result_subject,MYSQL_BOTH);
<table>
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Total </td>
</tr>
<tr>
<?php
for($i=0;i<count($data);i++){
?>
<td><?php echo $data['subject_name'];?></td>
<td><?php echo $data['marks'];?></td>
<td><?php echo $data['total'];?></td>
<?php
}
</tr>
</table>
I have two queries which is working fine and it would retrieve 3 rows each query. I am trying to display a query result on a table using foreach with two(2) queries inside a foreach loop. I tried putting two result() on foreach but its an error. How can i display a two result() on a single foreach loop? I don't know how can i achieve this.
Query 1 would be on column "Investor Name" then query 2 will be on "Amount".
Here is the code:
<?php
$query5 = $this->db->query("SELECT * FROM ".tbl_investors." WHERE id IN (SELECT MAX(investor_id) FROM ".tbl_investors_ledger." GROUP BY investor_id ) AND deleted = 0");
$query6 = $this->db->query("SELECT * FROM ".tbl_investors_ledger." WHERE id IN (SELECT MAX(id) FROM ".tbl_investors_ledger." GROUP BY investor_id ) AND deleted = 0");
?>
<table class="table table-striped table-bordered table-hover" id="dataTables">
<thead>
<tr>
<td>Investor Name</td>
<td>Amount</td>
</tr>
</thead>
<tbody style="text-align: center;">
<?php
foreach ($query5->result() as $row) && ($query6->result() as $row2){
?>
<tr>
<td><?php echo $row->last_name.', '.$row->first_name; ?></td>
<td><?php echo $row2->amount; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can't use two array_expressions on foreach loop. It is better to use join on your query to make it one. something like
$query = "SELECT `tbl_investors`.* , `tbl_investors_ledger`.*
FROM `tbl_investors`
LEFT JOIN `tbl_investors_ledger`
ON `tbl_investors`.id = `tbl_investors_ledger`. investor_id
WHERE `tbl_investors`.deleted = 0 AND `tbl_investors_ledger`.deleted = 0
GROUP BY `tbl_investors_ledger`.investor_id
ORDER BY `tbl_investors_ledger`.id DESC ";
As far as I know, a foreach loop can only handle one query. I see two options out of this, really..
Option 1
Create two different foreach loops, not inside each other since that would make double results, but outside each other. That might not do what you want to, but it could be worth a shot, like so:
<!-- FIRST LOOP -->
<?php
foreach ($query5->result() as $row) {
?>
<td> Your data goes here </td>
<?php
}
?>
<!-- SECOND LOOP -->
<?php
foreach ($query6->result() as $row) {
?>
<td> Your data goes here </td>
<?php
}
?>
Option 2
Create only one query, requiring only one of the loops, by taking use of "JOIN"
See answer at this post
I have a table which display list of item names, its classes, item quantities in each class, and total quantity of item for all items. The expected result should look like this:
Item Name | Item Class | Item Quantity | Total Quantity
| 1 | 4 |
A | 2 | 5 | 11
| 3 | 2 |
-------------------------------------------------------
| 1 | 6 |
B | 2 | 3 | 10
| 3 | 1 |
but what I got is like this:
Item Name | Item Class | Item Quantity | Total Quantity
| 1 | 4 |
A | 2 | 5 |
| 3 | 2 |
----------------------------------------
| 1 | 6 |
B | 2 | 3 |
| 3 | 1 | 11 | 10
How to display the total quantity vertically in the same row with its key(item name) and with rowspan. I use array to display results as there are hidden calculation. My code is a bit complicated because of the calculation but lets just ignore the calculation part because it works fine. If you need to understand my calculation, you can view my previous questions from my profile. What I need to fix now is how to display the total quantity correctly.
Here is my code :
<table>
<tr>
<td>Item Name</td>
<td>Item Class</td>
<td>Item Quantity</td>
<td>Total Quantity</td>
</tr>
<?php
$result=mysql_query("SELECT * FROM tblitem");
$classqty=array();
while($row = mysql_fetch_array($result)){
$item=$row['itemName'];
$class=$row['itemClassName'];
if(!isset($classqty[$item][$class]))
{
$classqty[$item][$class] = 0;
}
if(is_null($row['itemLocCheckOut'])){
$classqty[$item][$class] += $row['itemQty'];
}
else{
$classqty[$item][$class] -= $row['itemQty'];
}
}
$sum=array();
foreach($classqty as $k1=>$v1){
foreach($v1 as $k2=>$v2){
if(!isset($sum[$k1])){
$sum[$k1] = $v2;
}
else
{
$sum[$k1] += $v2;
}
?>
<tr>
<td><?php echo $k1;?></td>
<td><?php echo $k2;?></td>
<td><?php echo $v2;?></td>
<?php
} /*close second foreach*/
} /*close first foreach*/
foreach($sum as $name=>$total){
?>
<td><?php echo $total;?></td>
<?php
} /*close foreach for $sum*/
?>
</tr>
</table>
P/S: Please don't suggest me to use SELECT SUM because there is no problem with my calculation and I can't simply use SUM because my code involves multiple series of calculation and the total quantity is based on the result from the calculation. My problem is just on how to display the total quantity result correctly at its place vertically.
Try this...
SELECT *,SUM(IF(item_name='a',itemQuantity,0)) AS itemQuantityA,SUM(IF(item_name='b',itemQuantity,0)) AS itemQuantityB FROM tblitem
Using this you will directly got the sum of item A and B.
and based on your requirement you can edit or customize this query.
Hope this helps.
The HTML needs to be corrected. Have rectified the code. Try it out
<table>
<tr>
<td>Item Name</td>
<td>Item Class</td>
<td>Item Quantity</td>
<td>Total Quantity</td>
</tr>
<?php
$result=mysql_query("SELECT * FROM tblitem");
$classqty=array();
while($row = mysql_fetch_array($result)){
$item=$row['itemName'];
$class=$row['itemClassName'];
if(!isset($classqty[$item][$class]))
{
$classqty[$item][$class] = 0;
}
if(is_null($row['itemLocCheckOut'])){
$classqty[$item][$class] += $row['itemQty'];
}
else{
$classqty[$item][$class] -= $row['itemQty'];
}
}
?>
<tr>
<?php
$sum=array();
foreach($classqty as $k1=>$v1){
?>
<td><?php echo $k1;?></td>
<td>
<table>
<tr>
<?php
foreach($v1 as $k2=>$v2){
if(!isset($sum[$k1])){
$sum[$k1] = $v2;
}
else
{
$sum[$k1] += $v2;
}
?>
<td><?php echo $k2;?></td>
<td><?php echo $v2;?></td>
<?php
} /*close second foreach*/
?>
</tr>
</table>
</td>
<td><?php echo $sum[$k1];?></td>
} /*close first foreach*/
?>
</tr>
</table>
To create a dynamic table within php, I set the variable $count from a query, which counts the rows in specific table. Then I would like to create a table with the exact number of rows as a html table:
for($i=1;$i=<$count;$i++){
echo"<tr><td>$name</td><td>$rights</td></tr>";
}
That's the way i want the table to be displayed. But everytime the for-loop is called, the values of $name and $rights should be taken from the database-table. But how should i handle this? I thought about a simple query selecting the name from the line where ID equals i. But then i remembered that always when i delete an entry from the table there will be gaps.
For example when there 3 entries and i delete the second one. There just are 2 entries; so the name of the second row, which ID is 3, will never be selected. Is there any way of handling this problem in an appropriated way?
You wouldnt use a for you would use a while or a foreach with the results from the query.
<?php
$db = new PDO($dsn, $user, $pass);
$stmt = $db->query('SELECT id, rights FROM the_table');
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Rights</th>
</tr>
</thead>
<tbody>
<?php if($stmt !== false): ?>
<?php foreach( $stmt as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['rights'] ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>