I can't seem to get my table to have the right output. The code is the following:
<div class="bubbleTitle">Spetsialistide tööaeg graafiku alusel</div>
<table style="width: 600px" class="slicedTable">
<tr>
<th>Spetsialist</th>
<th>Tunnid</th>
</tr>
<tr>
<?php foreach($specs as $specName => $spec): ?>
<td><?php echo $specName?></td>
<?php foreach($tunnid as $tund): ?>
<td><?php echo $tund?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
I have tried everything in this thread: Nested Loop in table PHP , but none of that has worked either
The output is the following:
name - value
value2
value3
name2 - value
value2
value3
etc.
I would like it to be:
name1 - value1
name 2 - value2 etc.
Where $tunnid comes from:
result = mysql_query("SELECT `worker_id`, SUM(TIMESTAMPDIFF(HOUR, `start`, `end`)) as `total` FROM
`spa_worker_times` WHERE (`start` BETWEEN '".$validated['start']."' AND '".$validated['end']."') AND
(`end` BETWEEN '".$validated['start']."' AND '".$validated['end']."') GROUP BY `worker_id`") or die(mysql_error());
$tunnid = array();
while ($row = mysql_fetch_assoc($result)) {
$tunnid[] = $row['total'];
Where specs come from:
$data = $this->BookingProcedures->query("SELECT AProcedure.name, BookingGroup.booking_package_id > 0 AS pack_proc," .
"SUM(BookingProcedure.price) AS price, Worker.name, COUNT(BookingProcedure.id) AS num" .
" FROM spa_booking_procedure_specialists BookingProcedureSpecialist, " .
"spa_booking_procedures BookingProcedure, " .
"spa_booking_groups BookingGroup, spa_procedures AProcedure, spa_workers Worker" .
" WHERE !BookingProcedure.deleted" .
" AND DATE(BookingProcedure.start) >= '".$validated['start']."'" .
" AND DATE(BookingProcedure.start) <= '".$validated['end']."'" .
" AND BookingProcedureSpecialist.booking_procedure_id = BookingProcedure.id" .
" AND Worker.id = BookingProcedureSpecialist.specialist_id" .
" AND BookingGroup.id = BookingProcedure.group_id" .
" AND AProcedure.id = BookingGroup.procedure_id" .
" GROUP BY AProcedure.name, BookingGroup.booking_package_id > 0, Worker.name");
<div class="bubbleTitle">Spetsialistide tööaeg graafiku alusel</div>
<table style="width: 600px" class="slicedTable">
<tr>
<th>Spetsialist</th>
<th>Tunnid</th>
</tr>
<tr>
<?php foreach($specs as $specName => $spec): ?>
<td><h2><?php echo $specName?></h2>
<table><tr>
<?php foreach($tunnid as $tund): ?>
<td><?php echo $tund?></td>
<?php endforeach; ?>
</tr></table></td>
<?php endforeach; ?>
</tr>
</table>
This will output like this
Name1
value1 | value2 | value3
Name2
value1 | value2 | value3
<?php foreach($specs as $specName => $spec) { ?>
<tr>
<td><?php echo $specName?></td>
<?php foreach($tunnid as $tund) { ?>
<td><?php echo $tund?></td>
<?php } ?>
</tr>
<?php } ?>
will result in
name value value value
name value
name value value
and so on
Where does $tunnid come from. I would assume it contains only values, but in which way, are they in a regular array or an associative? My code would work if $tunnid contains the values to one name. But i assume that is not the case!?
<table style="width: 600px" class="slicedTable">
<tr>
<th>Spetsialist</th>
<th>Tunnid</th>
</tr>
<tr>
<?php foreach($specs as $specName => $spec): ?>
<td><?php echo $specName?></td>
<!-- seperate with anything -->
<td><?php echo implode(',', $tunnid); ?></td>
<?php endforeach; ?>
</tr>
</table>
<?php
$i = 0;
foreach($specs as $specName => $spec){
?>
<tr>
<td><?php echo $specName?></td>
<td><?php echo $tunid[$i++]; ?></td>
</tr>
}
?>
Related
I have the following data in MySQL table:
emp_id
date
shift
1001
2022-08-01
M
1001
2022-08-02
M
1001
2022-08-03
N
1002
2022-08-01
M
1002
2022-08-02
E
1002
2022-08-06
M
Output should be like this (One Month list show):
emp_id
2022-08-01
2022-08-02
2022-08-03
2022-08-04
2022-08-05
2022-08-06
1001
M
M
N
0
0
0
1002
M
E
0
0
0
M
My code is:
<?php
for($j = 1; $j <= date('t'); $j++)
{
$dat = str_pad($j, 2, '0', STR_PAD_LEFT). "-" . date('m') . "-" .
date('Y');
$d[] = date('Y'). "-" . date('m') . "-" . str_pad($j, 2, '0',
STR_PAD_LEFT);
$datess[] = str_pad($j, 2, '0', STR_PAD_LEFT)."<br>".date('D',
strtotime($dat));
}
?>
<?php
$link = mysqli_connect('localhost','root','','rostertest');
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Employee</th>
<?php foreach($datess as $dates) {?>
<th>
<?php echo $dates; ?>
</th>
<?php } ?>
</tr>
</thead>
<tbody id="tbl_body">
<?php
$i=0;
$get_sql = "select * from ros";
$run = mysqli_query($link,$get_sql);
while($row = mysqli_fetch_array($run)){
$date = $row['date'];
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row['emp_id']; ?></td>
<?php foreach($d as $da) { if($date==$da){?>
<td><?php echo $row['shift']; ?></td>
<?php }else{ ?>
<td><?php echo "0";?></td>
<?php }}?>
</tr>
<?php }?>
</tbody>
</table>
</div>
for these kind of data you need to have one other Table, for example Table.emp_date with a relation between (emp_id from ros) and (id from emp_date)
and you have to move 'emp_date' and 'shift' to this new table.
let me try to explain with your code too:
<?php
$i=0;
$get_sql = "select * from ros";
$run = mysqli_query($link,$get_sql);
while($row = mysqli_fetch_array($run)){
$get_sql2 = "select * from emp_date WHERE id=" . $row['emp_id'];
$run2 = mysqli_query($link,$get_sql2);
while($row2 = mysqli_fetch_array($run2)){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row2['emp_date']; ?></td>
<td><?php echo $row2['shift']; ?></td>
</tr>
<?php }}?>
I'm trying to select a particular row using jquery from a looped foreach. For now I'm just using a simple alert to see if I get the right row. Problem is, it only selects the last row, and not the row I clicked.
Example code:
<?php foreach ($customers as $c) : ?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
The Jquery call:
$('tr td ').click(function(){
var fn = '<?php echo $c['firstname']; ?>';
alert(fn);}
);
This will output all td values. Credits
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
$count = 0;
$customers = array (
array('firstname'=>'fname1', 'lastname'=>'lname1','phone'=>'111','email'=>'a#gmail.com','id'=>1),
array('firstname'=>'fname2', 'lastname'=>'lname2','phone'=>'222','email'=>'b#gmail.com','id'=>2),
array('firstname'=>'fname3', 'lastname'=>'lname3','phone'=>'333','email'=>'c#gmail.com','id'=>3),
array('firstname'=>'fname4', 'lastname'=>'lname4','phone'=>'444','email'=>'d#gmail.com','id'=>4),
);
?>
<table class="table table-bordered">
<?php foreach ($customers as $c) : ?>
<tr class="getdetails">
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
</table>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(".getdetails").click(function(){
$(this).find("td").each(function(){
console.log($(this).html());
});
});
</script>
</body>
</html>
You can access particular row's column like bellow
$('tr.item').each(function() { // selected tr
var col1 = $(this).find("td:eq(0) > input").val(); // find the input field value inside the first column of the row
var col2 = $(this).find("td:eq(1) > input").val();}
I want to prevent amaun_caj , amaun_pelbagai , amaun_penalti , amaun_tunggakan from being repeated so if the id_akaun is the same the data will not appear, my code only works on amaun_caj, and for the rest, not a single data appear, what's the problem?
<?php
$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $jenis; ?>
</td>
<td>
<?php echo $id_akaun;
?>
</td>
<td>
<?php echo $no_telefon; ?>
</td>
<td>
<?php echo $lokasi; ?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_caj;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_pelbagai;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_penalti;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_tunggakan;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $jumlah_bayaran;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<?php
}
?>
Using temporary variable $old_id_akaun might not be the best practice. If you still want to do it like that, i suggest you user ORDER BY id_akaun in your SQL syntax.
In my oppinion, you might get rid of temporary variable and follow these steps,
1. Create empty array outside your while loop. For the sake of easy
understanding, let's called it $list_id_akaun.
2. Inside your while loop, after you get $id_akaun, check whether $id_akaun
is inside $list_id_akaun
3. If not exists, insert it to $list_id_akaun and continue echoing your
table row
4. If exists, skip to the next row.
I don't know why you don't want to use a select distinct clause in this case, but just put them inside a container then check inside the loop:
<?php
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)):
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $jenis; ?></td>
<td><?php echo $id_akaun; ?></td>
<td><?php echo $no_telefon; ?></td>
<td><?php echo $lokasi; ?></td>
<?php if(!in_array($id_akaun, $temp)): ?>
<td><?php echo $amaun_penalti; ?></td>
<td><?php echo $amaun_tunggakan; ?></td>
<td><?php echo $jumlah_bayaran; ?></td>
<?php $temp[] = $id_akaun; ?>
<?php else : ?>
<td></td><td></td><td></td>
<?php endif; ?>
</tr>
<?php endhile; ?>
Have array named for example $data_debit_turnover
Array
(
[0] => Array
(
[VatReturnRowNumberForDebitTurnover] => 63
[Total] => 0.00
)
[1] => Array
(
[VatReturnRowNumberForDebitTurnover] => 64
[Total] => 44.28
)
)
Have HTML that need to look like this
<table><tr>
<td><strong>63</strong></td>
<td>0.00</td>
</tr><tr>
<td><strong>64</strong></td>
<td>44.28</td>
</tr></table>
At first tried with php foreach, but in such case instead of one table get multiple tables [0], [1] etc.
Then tried
<td><strong>64</strong></td>
<td><?php
if( $data_debit_turnover[1][VatReturnRowNumberForDebitTurnover] == '64'){
echo $data_debit_turnover[1][Total]. ' Total<br>';
}?>
</td>
but problem is with [1] etc. I do not know number of []; may be [1] and may be [30].
Tried something like this
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
<td><strong>64</strong></td>
<td><?php
if (in_array('64', $resultVatReturnRowNumberForDebitTurnover)) {
echo $resultTotal[1];
}?>
</td>
The same problem [1]. How to echo corresponding (index) value from the another array.
For example if 64 is the second value in array $resultVatReturnRowNumberForDebitTurnover then need to echo the second value from array $resultTotal.
Possibly there is some other way.
Please advice.
Showing what I did with foreach
<?php
foreach($data_debit_turnover as $i => $result){
?>
<table>
<tr>
<td><strong>63</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '63') {
echo $result[Total];
} ?>
</td>
</tr>
<tr>
<td><strong>64</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '64') {
echo $result[Total];
} ?>
</td>
</tr>
</table>
<?php
}
?>
Update Thanks to #user2340218 advice get some solution. For each <td> must use foreach. Possibly there is some better solution.
<table><tr>
<td><strong>64</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '64') {echo $vatReturn['Total'];}}?></td>
</tr><tr>
<td><strong>67</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '67') {echo $vatReturn['Total'];}}?></td>
</tr></table>
Solution Finally used solution #Daniel P advised.
$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}
$VatReturnRowNumberForDebitTurnoverModified = array_combine($resultVatReturnRowNumberForDebitTurnover, $resultTotal);
<table>
<tr>
<td><strong>62</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[62]; ?>
</td>
</tr>
<tr>
<td><strong>63</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[63]; ?>
</td>
</tr>
</table>
Actually have to accept #Daniel P answer:) But as understand can not accept 2 answers
I am guessing that VatReturnRowNumberForDebitTurnover numbers are unique so your array should look like this :
Array
(
[63] => 0.00
[64] => 44.28
)
The array index is your VatReturnRowNumberForDebitTurnover and the value is your total.
To test if a VatReturnRowNumberForDebitTurnover has a value your simply use isset()
if (isset($array[63])) {
echo $array[63]
}
Building the table :
<table>
<?php foreach($data_debit_turnover as $i => $total){ ?>
<tr>
<td><strong><?php echo $i; ?></strong></td>
<td><?php echo $total; ?></td>
</tr>
<?php } ?>
</table>
Do you mean this?
<table>
<?php foreach($data_debit_turnover as $vatReturn){ ?>
<tr>
<td><strong><?php print $vatReturn['VatReturnRowNumberForDebitTurnover'] ?></strong></td>
<td><?php print $vatReturn['total'] ?></td>
</tr>
<?php } ?>
</table>
Use foreach like this:
<table>
<?php
foreach ($Array as $item)
{
echo '<tr>';
echo '<td><strong>' . $item['VatReturnRowNumberForDebitTurnover'] . '</strong></td>';
echo '<td>' . $item['Total'] . '</td>';
echo '</tr>'
}
?>
</table>
My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template