I have a result and a json , where $response['json'] contain table header columns and table data comes for the $response['results']. i need to check if the header contain column name as "NN ID" then respective table column data should be in a anchor Data tag.
PHP code:
$json = $response['JSON'];
$result = $response['RESULT'];
echo '<strong>Search Results</strong>
<h4 class="bg-default"><strong>Total Records ('.count($result).')</strong></h4>';
if (count($result) > 0) {
echo '<table class="table table-striped table-hover table-bordered">
<thead class="bg-primary">
<tr>';
foreach (array_values($json) as $column) {
echo '<th>'.$column.'</th>';
}
echo '</tr>
</thead>
<tbody>';
foreach ($result as $row) {
echo '<tr>';
foreach (array_keys($json) as $field) {
echo '<td>'.$row[$field].'</td>';
}
echo '</tr>';
}
echo '</tbody>
</table>';
}
Need to add href for
<td>
<a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[field].'\')">
'.$row[field].'
</a>
</td>
Current View:
Need View as a link for NN ID Column data
A simple if statement is all you need:
foreach ($result as $row) {
echo '<tr>';
foreach ( array_keys($json) as $field) {
if ( $field == 'NN ID' ) {
echo '<td><a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[$field].'\')">
'.$row[$field].'
</a></td>';
} else {
echo '<td>'.$row[$field].'</td>';
}
}
echo '</tr>';
}
Related
issue getting in datatable while i m using loop array
Column are equal thead and tbody still getting issue.
i m faching data from command and using array i want output in datatable.output is working but datable shorting and search option is not working.
<div class="m-t-25">
<table id="data-table" class="table dataTable">
<thead>
<tr>
<?php
foreach ($output[0] as $clave=>$fila) {
echo "<th>".$fila."</th>";
}
?>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$output = array_slice($output, 2);
foreach ($output as $fila) {
echo "<tr>";
foreach ($fila as $elemento) {
echo "<td>".$elemento."</td>";
}
echo '<td><select class="act">';
echo '<option value="watch" selected="">graph</option>';
echo '</select></td>';
echo "</tr>";
}
?>
</tbody>
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'm using CodeIgniter and I'm trying to populate a <table> and just getting the same row 4 times.
My model:
$sql2=
"SELECT *
FROM puntos
WHERE checklist_idchecklist='$idchecklist'";
$qry2=$this->db->query($sql2);
$puntos=$qry2->row();
return $puntos
This query returns 2 arrays of 4 attributes each, i tested it doing the SQL query on phpmyadmin.
My controller:
function verpuntos() {
$this->load->model('checklistm');
$puntos=$this->checklistm->obtenerpuntos();
$this->load->view('plantilla/headerguardia');
$this->load->view('checklist/lista', $puntos);
$this->load->view('plantilla/footer');
}
My view:
$punto=array(
'descripcion' => $descripcion,
'lugar' => $lugar,
'tipo' => $tipo,
'urgencia' => $urgencia);
<table class="table">
<thead>
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php foreach($punto as $row) {
echo '<tr>';
echo '<td>'.$descripcion.'</td>';
echo '<td>'.$lugar.'</td>';
echo '<td>'.$tipo.'</td>';
echo '<td>'.$urgencia.'</td>';
echo '</tr>';
}
?>
<tbody>
</table>
And this is what I'm getting, the same row 4 times:
In codeigniter row() fetch the first row in object format.$puntos=$qry2->row(); This statement fetch only first row of your fetched data in object fromat. SO no need to use foreach loop. Just try like this..
echo '<tr>';
echo '<td>'.$puntos->descripcion.'</td>';
echo '<td>'.$puntos->lugar.'</td>';
echo '<td>'.$puntos->tipo.'</td>';
echo '<td>'.$puntos->urgencia.'</td>';
echo '</tr>';
For more see here https://www.codeigniter.com/userguide3/database/results.html
<table class="table">
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php
echo '<tr>';
foreach($punto as $row)
{
echo '<td>'.$row.'</td>';
}
echo '</tr>';
?>
</tbody>
<table>
try like this
echo '<tr>';
echo '<td>'.$punto['item_1_name'].'</td>';
echo '<td>'.$punto['item_2_name']..'</td>';
echo '<td>'.$punto['item_3_name']..'</td>';
echo '<td>'.$punto['item_4_name']..'</td>';
echo '</tr>';
item_1_name = index name of array returned
HTML Table getting mixed up when having echo in while loop. Bellow attached the output image for that. How can i get normal table with loop result? Please note i am getting table data by searching with manual date.
<?php
if (isset($_POST['submit_date'])) {
if (empty($_POST['m_date'])) {
echo '<div class="alert alert-danger">Error: Select date then search</div>';
}else{
$m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>
<tr>
';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<td>'.$cat_name.'</td><td>'.$price.'</td>';
}
echo '</tr></table>';
}
}
?>
the output i am getting from this code
It looks like you'll need to include table rows (<tr>) inside your loop, as well. Your current code outputs all data on the same row, which undoubtedly breaks the table.
echo '<table style="width:100%">
<tr>
<th>Category Name</th>
<th>Price</th>
</tr>';
while ($row=mysqli_fetch_array($q)) {
$cat_name = $row['cat_name'];
$price = $row['price'];
echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td></tr>';
}
echo '</table>';
Replace your cede with this
<?php if (isset($_POST['submit_date'])) { if (empty($_POST['m_date'])) { echo '<div
class="alert alert-danger">Error: Select date then search</div>'; }else{ $m_date = $_POST['m_date'];
$q = mysqli_query($conn, "SELECT * FROM bazar_dor WHERE m_date='$m_date'");
echo '<table style="width:100%"> <tr>
<th>Category Name</th> <th>Price</th> </tr> ';
while ($row=mysqli_fetch_array($q)) { $cat_name = $row['cat_name'];
$price = $row['price']; echo '<tr><td>'.$cat_name.'</td><td>'.$price.'</td><\tr>';
} echo '</table>';
} } ?>
I have this code that works fine in other pages that I made but doesn't work properly on my summary page.
<?php
//AAFES-date1
$sqlAAFES1 = "SELECT * FROM aafes WHERE dueDate ='$date1'";
$qAAFES1 = $pdo->prepare($sqlAAFES1);
$qAAFES1->execute(array($date1));
$dataAAFES1 = $qAAFES1->fetch(PDO::FETCH_ASSOC);
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
echo '<tr>';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
echo '</table>';
};
};
?>
as you can see the 2nd row doesn't align with the first row.
while on the other page, I used the same foreach code, but alignment is perfect. So I wanna ask what seems to be the problem with this one.
Don't close foreach loop after table. Put <tr></tr> inside foreach loop.
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<tr>';
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
};
echo '</table>';
};