I have a one array in code-igniter controller
foreach($array as $row) {
echo $row['_source']['shape'];
echo "<br>";
echo $row['_source']['cut'];
echo "<br>";
echo $row['_source']['color'];
echo "<br>";
echo $row['_source']['clarity'];
echo "<br>";
echo $row['_source']['lab'];
echo "<br>";
echo $row['_source']['polish'];
echo "<br>";
echo $row['_source']['symmetry'];
echo "<br>";
echo $row['_source']['stone_id'];
echo "<br>";
echo $row['_source']['fluorescence'];
echo "<br>";
echo $row['_source']['cert_no'];
echo "<br>";
echo $row['_source']['location'];
echo "<br>";
}
which gives me two different bunch of values
In first bunch of values i got
round 3X D IF GIA VG G id01 FNT xy01 india
In second bunch of values i got
heart 2X f IF IGI VG G id01 FNT xy01 china
Now i want to display this all values in table in view part..
First send your data to view as follows:
//Retrive your data here and put in $result
$data['res']=$result;
$this->load->view('view_page', $data);
Then in your view page
<table>
<?php
foreach($res as $row) {
echo "<tr>";
echo "<td>".$row['_source']['shape']."</td>";
echo "<td>".$row['_source']['cut']."</td>";
echo "<td>".$row['_source']['color']."<td>";
echo "<td>".$row['_source']['clarity']."</td>";
echo "<td>".$row['_source']['lab']."</td>";
echo "<td>".$row['_source']['polish']."</td>";
echo "<td>".$row['_source']['symmetry']."</td>";
echo "<td>".$row['_source']['stone_id']."</td>";
echo "<td>".$row['_source']['fluorescence']."</td>";
echo "<td>".$row['_source']['cert_no']."</td>";
echo "<td>".$row['_source']['location']."</td>";
echo "</tr>";
}
?>
</table>
In controller :
$data['your_array'] = $array;
$this->load->view('view_page', $data);
View page :
<table>
<?php
if(is_array($values) && count($your_array) > 0) {
foreach($your_array as $row) {
?>
<tr>
<td><?php echo $row['_source']['shape']; ?></td>
<td><?php echo $row['_source']['cut']; ?></td>
<td><?php echo $row['_source']['color']; ?><td>
<td><?php echo $row['_source']['clarity']; ?></td>
<td><?php echo $row['_source']['lab']; ?></td>
<td><?php echo $row['_source']['polish']; ?></td>
<td><?php echo $row['_source']['symmetry']; ?></td>
<td><?php echo $row['_source']['stone_id']; ?></td>
<td><?php echo $row['_source']['fluorescence']; ?></td>
<td><?php echo $row['_source']['cert_no']; ?></td>
<td><?php echo $row['_source']['location']; ?></td>
</tr>
<?php
}
}
?>
</table>
Related
I got this piece of code that shows output from a database in a table. The last column shows the output on a button and can be clicked to go further.
I am looking for a way to style the button with css, but its not doing what i need at all.
<?php
while($row = mysqli_fetch_array($result)){
?>
<tr align="center">
<td><?php echo $row['klasse']; ?></td>
<td><?php echo $row['orde']; ?></td>
<td><?php echo $row['onderorde']; ?></td>
<td><?php echo $row['familie']; ?></td>
<td><?php echo $row['onderfamilie']; ?></td>
<td><?php echo $row['soort']; ?></td>
<td><?php echo $row['ondersoort']; ?></td>
<?php
$output = $row['ned_naam'];
echo "<td> <a href='info.php?value=". $output ."'><button>" . $output . "</button></a> </td>";
echo "</tr>";
}
?>
I tried:
<button class=\'btn\'>" . $output . "</button></a> </td>";
but that doesn't seem to work at all. What am i doing wrong?
There is no reason to use escape character \ on single quote since you wrapped your echo in double quotes
echo "<button class='btn'>" . $output . "</button>";
I built an application that outputs json data showing who represents you in congress, using php. I created a table to output the data however, it's not rendering, not sure where the error is?
<div class="data-table-wrapper">
<?php
$json_rep =
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip='.$_GET['zip'].'&output=json');
$rep_array = json_decode($json_rep);
var_dump($rep_array);
?>
<table class="data-table">
<thead>
<tr>
<td>Name</td>
<td>Party</td>
<td>State</td>
<td>District</td>
<td>Phone</td>
<td>Office</td>
<td>Link</td>
</tr>
<?php
foreach($rep_array->results->Name->Party as $key=>$item){
?>
<tr>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->party; ?></td>
<td><?php echo $item->state; ?></td>
<td><?php echo $item->district; ?></td>
<td><?php echo $item->phone; ?></td>
<td><?php echo $item->office; ?></td>
<td><?php echo $item->link; ?></td>
</tr>
<?
}
?>
</table>
</div>
</div>
should render key and value pair into the table?
Use $rep_array = json_decode($json_rep, true); to get an associative array back. Now all you need to do is loop through the array to render any of the data in your table, like so:
<?php
$json_rep = file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=10001&output=json');
$rep_array = json_decode($json_rep, true);
echo "<table>";
foreach ($rep_array['results'] as $key => $value) {
foreach ($value as $key1 => $value1) {
echo "<tr>";
echo "<td>";
echo $key1;
echo "</td>";
echo "<td>";
echo $value1;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
Been struggling with this for hours. Followed several similar examples I found online, none identical situation, and can't get it to pass the variable. I know it's basic, but just learning. Thanks in advance.
<?php
foreach($arr as $r) {
echo "<tr>";
echo "<td>".$r['TimeStamp']."</td>";
echo "<td>".$r['LocationName']."</td>";
**echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**
echo "<td>".$r['Price']."</td>";
echo "<td>".$r['Description']."</td>";
echo "</tr>";
}
?>
I added a variable $id to make the code clearer and easier to work with, but still can't pass the value.
<?php
foreach($arr as $r) {
$id = $r['Post_ID'];
echo "<tr>";
echo "<td>".$r['TimeStamp']."</td>";
echo "<td>".$r['LocationName']."</td>";
echo "<td><a href='details_get.php?id='.$id.'>".$r['Title']."</a></td>";
echo "<td>".$r['Price']."</td>";
echo "<td>".$r['Description']."</td>";
echo "</tr>";
}
?>
Finally got it to work, but I had to separate my php foreach loop and html as found in this post works.
<?php foreach($arr as $r) : ?>
<tr>
<td><?php echo $r['TimeStamp']; ?></td>
<td><?php echo $r['LocationName']; ?></td>
<td><?php echo $r['Title']; ?></td>
<td><?php echo $r['Price']; ?></td>
<td><?php echo $r['Description']; ?></td>
</tr>
<?php endforeach; ?>
**echo "<td><a href='details_get.php?id='$r['Post_ID']>".$r['Title']."</a></td>";**
this row should look like
echo <td>".$r['Title']."</td>";
You are missing a dot in the $r['Post_ID'] I think.
If you share more information may be I can give you others advises
I have 4 rows of data in the table. I managed to display all the data using PDO select query. I also have a search button for displaying specific data from my database. It's working but instead of filtering and displaying the specific data I have searched, it just added on the table.
Here is my code for search button:
if(isset($_POST['search_btn']))
{
$row = null;
if(empty($_POST['InvNumbTxt']))
{
echo '<h3>*Please input invoice number!</h3>';
}
else
{
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql1="SELECT * FROM invalid_invoice WHERE Invoice_Number='".$invtxt."'";
$stmt1=$dbh->prepare($sql1);
$stmt1->execute();
while( $row2 = $stmt1->fetch(PDO::FETCH_ASSOC) ) { ?>
<td><?php echo $row2['Issue_Type']; ?></td>
<td><?php echo $row2['Creation_Date']; ?></td>
<td><?php echo $row2['Site']; ?></td>
<td><?php echo $row2['Vendor_Name']; ?></td>
<td><?php echo $row2['Invoice_Date']; ?></td>
<td><?php echo $row2['Invoice_Number']; ?></td>
<td><?php echo $row2['Part_Number']; ?></td>
<td><?php echo $row2['PO']; ?></td>
<td><?php echo $row2['RR']; ?></td>
<td><?php echo $row2['Currency']; ?></td>
<td><?php echo $row2['Invoice_Amount']; ?></td>
<td><?php echo $row2['Issues']; ?></td>
<td><?php echo $row2['PersoninCharge']; ?></td>
<td><?php echo $row2['PIC_Comments']; ?></td>
<td><?php echo $row2['Status']; ?></td>
<td><?php echo $row2['ID']; ?></td>
<?php
}
?>
</tr>
<?php
$dbh = null;
}
}
?>
Here is the code for displaying the data on the table:
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql="SELECT * FROM invalid_invoice";
$stmt=$dbh->prepare($sql);
$stmt->execute();
echo "<form name='update' method='POST'><tr class=output2>";
while($row =$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<td>".$row['Issue_Type']."</td>";
echo "<td>".$row['Creation_Date']."</td>";
echo "<td>".$row['Site']."</td>";
echo "<td>".$row['Vendor_Name']."</td>";
echo "<td>".$row['Invoice_Date']."</td>";
echo "<td>".$row['Invoice_Number']."</td>";
echo "<td>".$row['Part_Number']."</td>";
echo "<td>".$row['PO']."</td>";
echo "<td>".$row['RR']."</td>";
echo "<td>".$row['Currency']."</td>";
echo "<td>".$row['Invoice_Amount']."</td>";
echo "<td>".$row['Issues']."</td>";
echo "<td><input type='text' name='pic' value='".$row['PersoninCharge']."'></td>";
echo "<td><input type='text' name='comt' value='".$row['PIC_Comments']."'></td>";
echo "<td><input type='text' name='stat' value='".$row['Status']."'></td>";
echo "<td><input type='submit' name='save_btn' value='♦ SAVE ♦' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</form>";
$dbh=null;
<tr class=output2>
<?php
Try adding a check to display all records only when serach_btn is not set. Something like:
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql="SELECT * FROM invalid_invoice";
$stmt=$dbh->prepare($sql);
$stmt->execute();
echo "<form name='update' method='POST'><tr class=output2>";
if(!isset($_POST['search_btn'])) {
while($row =$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<td>".$row['Issue_Type']."</td>";
echo "<td>".$row['Creation_Date']."</td>";
echo "<td>".$row['Site']."</td>";
echo "<td>".$row['Vendor_Name']."</td>";
echo "<td>".$row['Invoice_Date']."</td>";
echo "<td>".$row['Invoice_Number']."</td>";
echo "<td>".$row['Part_Number']."</td>";
echo "<td>".$row['PO']."</td>";
echo "<td>".$row['RR']."</td>";
echo "<td>".$row['Currency']."</td>";
echo "<td>".$row['Invoice_Amount']."</td>";
echo "<td>".$row['Issues']."</td>";
echo "<td><input type='text' name='pic' value='".$row['PersoninCharge']."'></td>";
echo "<td><input type='text' name='comt' value='".$row['PIC_Comments']."'></td>";
echo "<td><input type='text' name='stat' value='".$row['Status']."'></td>";
echo "<td><input type='submit' name='save_btn' value='♦ SAVE ♦' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='".$row['ID']."'/></td>";
echo "</tr>";
}
}
echo "</form>";
$dbh=null;
<tr class=output2>
I am trying to figure out how to link items in a row of a table while keeping the row in place, I have the following code:
echo "<br><br><table border=0 cellpadding=3>";
echo "<td><b>Player Name</b></td>";
echo "<td><b>Position</b></td>";
echo "<td><b>Height</b></td>";
echo "<td><b>Weight</b></td>";
echo "<td><b>Birthdate</b></td>";
echo "<td><b>NHL Rights</b></td>";
echo "<td><b>CNGHL Team</b></td>";
echo "<td><b>Current Team</b></td>";
echo "<td><b>Current League</b></td>";
while($row = mysql_fetch_array($oteaminfo))
{
echo "<tr>";
echo "<td>".$row['FullName']."</td> ";
echo "<td>".$row['Position']."</td> ";
echo "<td>".$row['Height']."</td> ";
echo "<td>".$row['Weight']."</td> ";
echo "<td>".$row['DOB']."</td> ";
echo "<td>".$row['Team']."</td> ";
echo "<td>".$row['CNGHLRights']."</td> ";
echo "<td>".$row['InternationalTeam']."</td> ";
echo "<td>".$row['InternationLeague']."</td> ";
echo "</tr>";
}
echo "</table>";
I have tried using
echo "<a href=\"cnghlplayers.php? PlayerID=".$row['PlayerID']."\">".$row['FullName']."<br>";
In Place of the
echo "<td>".$row['FullName']."</td> ";
In the table but It puts the links into a new row above my current table. Any help would be greatly appreciated, I tried searching for this topic but I couldn't find any information that was helpful.
Thanks!
Just wrap the <td> element around:
echo '<td>'.$row['FullName'].'</td>';
Wrap your link with TD:
echo "<td>".$row['FullName']."</td>";
Test this, there were some issues with your table markup. This should be a bit easier to read as well.
?> <!-- this line ends your php code so that you can format the HTML sanely -->
<br />
<br />
<table border=0 cellpadding=3>
<tr>
<td><b>Player Name</b></td>
<td><b>Position</b></td>
<td><b>Height</b></td>
<td><b>Weight</b></td>
<td><b>Birthdate</b></td>
<td><b>NHL Rights</b></td>
<td><b>CNGHL Team</b></td>
<td><b>Current Team</b></td>
<td><b>Current League</b></td>
</tr>
<?php while($row = mysql_fetch_array($oteaminfo)) { ?>
<tr>
<td><?php echo "".$row['FullName'].""; ?></td>
<td><?php echo $row['Position']; ?></td>
<td><?php echo $row['Height']; ?></td>
<td><?php echo $row['Weight']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Team']; ?></td>
<td><?php echo $row['CNGHLRights']; ?></td>
<td><?php echo $row['InternationalTeam']; ?></td>
<td><?php echo $row['InternationLeague']; ?></td>
</tr>
<?php } ?>
</table>
<?php //continues your php code below