Pretty new to PHP so here's the question. I have a SQL resultset with the following structure:
ItemID Desc
0 A
1 B
2 C
0 D
3 E
0 F
4 G
5 H
For each ItemID = 0 I want to create a table. Then for each ItemID under ItemID = 0, I want to create a row in a new table, until I hit the next ItemID = 0. Repeat until end.
Desired results:
<table>
<tr>
<td>A</td>
</tr>
</table>
<table>
<tr>
<td>B</td>
</tr>
<tr>
<td>C</td>
</tr>
</table>
<table>
<tr>
<td>D</td>
</tr>
</table>
<table>
<tr>
<td>E</td>
</tr>
</table>
<table>
<tr>
<td>F</td>
</tr>
</table>
<table>
<tr>
<td>G</td>
</tr>
<tr>
<td>H</td>
</tr>
</table>
I can get each table to print when ItemID = 0, but doubt it's the correct method. Here's the php code I have so far:
$legendSql="select ItemID, Desc from ...";
$getLegend=sqlsrv_query($conn, $legendSql);
while( $row = sqlsrv_fetch_array( $getLegend, SQLSRV_FETCH_ASSOC) ) {
if ($row['ItemID'] === '.00') { // when ItemID = 0 create table
print '<table>';
print '<tr>';
print '<td>';
print $row['Desc'];
print '</td>';
print '</tr>';
print '</table>';
}
// foreach( )?????
}
Something like this :
$first=1;
while( $row = sqlsrv_fetch_array( $getLegend, SQLSRV_FETCH_ASSOC) )
{
if ($row['ItemID'] === '.00') // when ItemID = 0 create table
{
if ($first==0)
{
print '</table>';
}
print '<table>';
print '<tr>';
print '<td>';
print $row['Desc'];
print '</td>';
print '</tr>';
print '</table>';
$first=1;
}
else
{
if ($first==1)
{
print '<table>';
$first=0
}
print '<tr>';
print '<td>';
print $row['Desc'];
print '</td>';
print '</tr>';
}
}
if ($first==0)
{
print '</table>';
}
Just change your if statement slightly:
if ($row['ItemID'] === '.00') { // when ItemID = 0 create table
print '<table>';
}
print '<tr>';
print '<td>';
print $row['Desc'];
print '</td>';
print '</tr>';
And outside the while loop, put this:
print '</table>';
The entire loop structure becomes:
while( $row = sqlsrv_fetch_array( $getLegend, SQLSRV_FETCH_ASSOC) ) {
if ($row['ItemID'] === '.00') {
print '<table>';
}
print '<tr>';
print '<td>';
print $row['Desc'];
print '</td>';
print '</tr>';
}
print '</table>';
Related
I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425
I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.
I'm completely new to PHP, so any hints on how to solve this will be much appreciated!
echo '<table>';
echo '<tr>';
echo '<th>';
echo '<td>Word</td>';
echo '<td>Frequency</td>';
echo '</th>';
echo '</tr>';
$response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");
while ($row = $response->fetch())
{
echo '<tr>';
echo '<td>'.$row['word'].'</td>';
echo '<td>'.$row['freq'].'</td>';
echo '</tr>';
}
echo '</table>';
$response->closeCursor();
A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.
echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
I prefer combining php and html
<table >
<thead>
<tr>
<th >Word</th>
<th >Frequency</th>
</tr>
</thead>
<?php
$response = $db->query("SELECT * FROM frequencies WHERE freq
BETWEEN 900 AND 910 ORDER BY freq");
?>
<tbody>
<?php
while ( $row = $response->fetch()) {
?>
<tr>
<td><?php echo $row['word']; ?></td>
<td><?php echo $row['freq']; ?></td>
</tr>
<?php }
$response->closeCursor();
?>
</tbody>
</table>
My database colleague built a couple of views and SP's for me so I can focus on html/php.
One is a query that shows a "total" row at the bottom of the table.
I want to:
Exclude this last row on my <tbody>
Show this single row in my <tfoot>
Why don't I just show everything in <tbody> since the totals row will always show at the bottom anyway? Because I want my <tfoot>'s <td>'s colspan to be different, for presentation purposes.
How can I do this using php alone?
I'm using basic PDO:
foreach($table as $row) {
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
This should work as long as the last row is not exactly the same as any other row.
$last_row = end($table);
echo '<tbody>';
foreach ($table as $row) {
if ($row === $last_row) continue;
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '</tbody>';
echo '<tfoot>
<tr>
<td>'.$last_row['Item'].'</td>
<td>'.$last_row['Amount'].'</td>
</tr>
</tfoot>';
If it's possible that the last row is not unique you can use the key instead:
$last_key = count($table) - 1;
echo '<tbody>';
foreach ($table as $key => $row) {
if ($key === $last_key) continue;
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '</tbody>';
echo '<tfoot>
<tr>
<td>'.$table[$last_key]['Item'].'</td>
<td>'.$table[$last_key]['Amount'].'</td>
</tr>
</tfoot>';
$total = 0;
foreach($table as $row) {
$total += $row['Amount'];
echo '<tr>
<td>'.$row['Item'].'</td>
<td>'.$row['Amount'].'</td>
</tr>';
}
echo '<tr>
<td>Total:</td>
<td>'.$total.'</td>
</tr>';
You don't need to use thead or tfoot if you do not intend to use them to style your content or in some cases SEO purposes.
hello friends i want to display two td tag in one tr.
this is is write into foreach loop.
this is my code.
$rri=0;
foreach ($related as $key => $value) {
if($rri % 2 == 0 ){
echo "<tr class='dsfdsf'>";
echo "<td >".$rri."</td>";
echo "</tr>";
}else{
echo "<td >".$rri."</td>";
}
$rri++;
}
this is my php code it return out put is under
<table>
<tr class='dsfdsf'>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr class='dsfdsf'>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr class='dsfdsf'>
<td>5</td>
</tr>
</table>
i want to output like this
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
</table>
foreachloop add new tr tag.
i checked odd or even this is working but unfortunate tr is added.
please help,
Thank you.
Hope this will help find your solution in a different but effective way:
<?php
$related = array(1,2,3,4,5);
$chunk = 2;
?>
<table>
<?php foreach (array_chunk($related, $chunk) as $row): ?>
<tr>
<?php foreach ($row as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Put the </tr> oudside the if and set it in else statement.
echo '<table>';
$rri=0;
foreach ($related as $key => $value) {
if($rri % 2 == 0 ){
echo '<tr class="dsfdsf">';
echo '<td>'.$rri.'</td>';
}else{
echo '<td>'.$rri.'</td>';
echo '</tr>';
}
$rri++;
}
echo'</table>';
This should work
$rri=0;
foreach ($related as $key => $value) {
if($rri % 2 == 0 ){
if($rri > 0){
echo "</tr>";
}
echo "<tr class='dsfdsf'>";
}
echo "<td>".$rri."</td>";
$rri++;
}
you can try this
$rri=1;
echo "<tr class='dsfdsf'>";
foreach ($related as $key => $value) {
echo "<td >".$rri."</td>";
if($rri % 2 == 0 ){
echo "</tr><tr>";
}
$rri++;
}
echo "</tr>";
<?php
$related=array('1','2','3','4');
$rri=0;
echo "<table border=1 width=500>";
foreach ($related as $key => $value) {
if($rri % 2 == 0 || $rri==0 ){
echo "<tr class='dsfdsf'>";
}
echo "<td >".$rri."</td>";
if($rri/2 == 0 && $rri!=0 ){
echo "</tr>" ;
}
$rri++;
} echo "</table>";?>
Try This
for ($i = 0; $i <= $rri; $i+=2) {
echo "<tr class='dsfdsf'>\n";
echo "<td>$i</td>\n";
echo "<td>$i+1</td?\n";
echo "</tr>\n";
}
I am trying to use for loops to create a table which dynamically returns something like this: Note how the td content have been arranged
<table>
<tr>
<td>1</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>10</td>
</tr>
</table>
among What I have tried so far is
$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
echo '<tr>';
for($k=0;$k<$col;$k++)
{
echo '<td>'.echo $x+=1.'</td>';
}
echo '</tr>';
}
In this case I get something different and which is not what I want.
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
</table>
Kindly someone help me map this.Thanks
<?php
# how high to count
$count = 10;
# how many rows in the table
$rows = 2;
# figure out how many columns
$columns = ceil($count/$rows);
# could also go the other way and declare there to be 5 columns and then
# divide to get the rows, but since you're counting down the columns,
# I thought this made more sense. Either way.
?><table><?php
for ($i=0; $i<$rows; ++$i) {
?><tr><?php
for ($j=0; $j<$columns; ++$j) {
# calculate which number to show in column $j of row $i. Each column adds
# $rows numbers to the total, while each row just adds one more. And we
# want to start at 1 instead of 0.
$n = $j * $rows + $i + 1;
?><td><?= $n ?></td><?php
}
?></tr><?php
}
?></table>
$total_count = 10;
$total_rows = 2;
$table = array();
//building table array
for($i=1;$i<=$total_count;$i++) {
$row = $i % $total_rows;
$row = $row == 0 ? $total_rows : $row;
$table[$row][] = $i;
}
//generate table based on array
echo "<table>";
for($row=1;$row<=$total_rows;$row++) {
echo "<tr>";
foreach($table[$row] as $cell) {
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
echo "</table>";
This isnt as complicated as people are making it seem
Start the inner loop at whatever row you're currently on and add 2 each time.
<?php
$rows=2;
$cols=10;
?>
<table>
<?php for($i=1;$i<=$rows;$i++): ?>
<tr>
<?php for($k=$i;$k<$cols;$k+=2): ?>
<td><?php echo $k ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
Id probably use range and foreach though
<?php
$rows=2;
$cols=10;
?>
<table>
<?php foreach( range( 1, $rows ) as $row ): ?>
<tr>
<?php foreach( range( $row, $cols, 2 ) as $col ): ?>
<td><?php echo $col ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This approach will split the data itself
function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
if (is_array($array) == true and !empty($array))
{
$rows = ceil(count($array)/$cols);
$array = array_chunk($array,$rows);
if (count($array) < $cols)
{
$array = array_pad($array,$cols,$pad);
}
foreach($array as $key => $value)
{
$array[$key] = array_pad($value,$rows,null);
}
foreach($array as $key => $value)
{
foreach($value as $sub_key => $sub_value)
{
$output[$sub_key][$key] = $sub_value;
}
}
return $output;
}
return $array;
}
$data = array(1,2,3,4,5,6,7,8,9,10,11);
echo '<table border="1">';
foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';