Create dynamic table with api data - php

How do I make my table dynamically list all the values it pulls from the api, atm it only pulls 1 record and puts it in the table, the rest of the data gets put outside it
Probably something very small I'm missing here, but can't put my finger on it
Thanks for any help, very appreciated.
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
</table>
<?php
}
}
}
?>

Didnt close foreach in right place. use this code instead
<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
$fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
if ($fetch->status != 'OK') {
echo sprintf('Error: %s', $fetch->message);
} else {
//example how to access statistics variable
$inactiveAccounts = $fetch->statistics->inactiveAccounts;
echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
<tr>
<th>Account name</th>
<th>Level</th>
</tr>
<?php
//example how to display all accounts username
foreach ($fetch->accounts as $account) {
?>
<tr>
<td><?php
echo $account->username;
?></td>
<td><?php
echo $account->currentLevel;
?></td>
</tr>
<?php
}
?>
</table>
<?php
}
}
?>

Related

unlink image using function in php

Please help me how can i unlink a image when i delete a product.I dont know where is my error in my code .Thank everyone so much.I tried different way to resolve this problem.I teached myself learn to code on youtube, and then I tried to create another model.Please help me solve this problem.
Html Page
<h3>List Product</h3>
<table id="user">
<thead>
<tr>
<th>Serial</th>
<th>Product Name</th>
<th>Image</th>
<th>Price</th>
<th>Category</th>
<th>Type</th>
<th>Action</th>
</tr>
</thead>
<?php
if(!empty($_GET['msg'])){
$msg = unserialize(urldecode($_GET['msg']));
foreach ($msg as $key => $value){
echo '<span style="color:blue;font-weight:bold">'.$value.'</span>';
}
}
?>
<tbody>
<?php
$i = 0;
foreach($listproduct as $key => $listpro){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $listpro['productName']; ?></td>
<td><img src="<?php echo BASE_URL ?>/teamplate/image/<?php echo $listpro['image']; ?>" width="60" height="60"></td>
<td><?php echo $listpro['price']; ?></td>
<td><?php echo $listpro['catName'] ?></td>
<td>
<?php
if($listpro['type']==0){
echo 'Feathered';
}else{
echo 'Non-Feathered';
}
?>
</td>
<td>Delete
|| Edit
</td>
</tr>
<?php
}
?>
</tbody>
</table>
Function in Product page
public function delPro($id = NULL){
$tablePro = 'tbl_product';
$this->data = array();
$ProductModel = $this->load->model('ProductModel');
$result = $ProductModel->delProductById($tablePro, $id);
if($result == 1){
$this->data['productbyid'] = $ProductModel->updateProductById($tablePro, $id);
$path_unlink= BASE_URL."/teamplate/image/";
foreach($this->data['productbyid'] as $this->key => $this->value){
$image_pro = $this->value['image'];
unlink($path_unlink.$image_pro);
}
$mdata['msg'] = "Product Deleted Successfully...";
}else{
$mdata['msg'] = "Product Not Deleted...";
}
$url = BASE_URL."/Product/ListProduct?msg=".urlencode(serialize($mdata));
header("Location:$url");
}
ProductModel Page
public function updateProductById($tablePro, $id){
$query = "SELECT * from $tablePro where productId = '$id'";
return $this->db->select($query);
}

How Can I Pass Result of Foreach from Controller to View CodeIgniter

just like my title, how can I pass foreach result from controller to view?
So far I did this:
--Controller.php
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data['kodeSubKategori']=$kodeSubKategori;
$data['namaSubKategori']=$namaSubKategori;
$data['definisiSubKategori']=$definisiSubKategori;
$data['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data['aliasSubKategori']=$aliasSubKategori;
}
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
Then, in my view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<tr>
<?php
foreach((array)$kodeSubKategori as $itemKodeSubKategori)
echo '<td>'.$itemKodeSubKategori.'</td>';
?>
<?php
foreach((array)$namaSubKategori as $itemNamaSubKategori)
echo '<td>'.$itemNamaSubKategori.'</td>';
?>
<?php
foreach((array)$definisiSubKategori as $itemDefinisiSubKategori)
echo '<td>'.$itemDefinisiSubKategori.'</td>';
?>
<?php
foreach((array)$unsurAbstrakSubKategori as $itemUnsurAbstrakSubKategori)
echo '<td>'.$itemUnsurAbstrakSubKategori.'</td>';
?>
<?php
foreach((array)$aliasSubKategori as $itemAliasSubKategori)
echo '<td>'.$itemAliasSubKategori.'</td>';
?>
</tr>
</table>
It successfully show ONLY THE LAST ONE data. I want to get all data from the result from foreach. How can I do that?
I suggest you to pass all $get_json_subkategori['SubKategoris'] array in data array to your view:
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemSubKategori'] = $get_json_subkategori['SubKategoris'];
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
And in your view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<?php foreach($itemSubKategori as $rowSubKategori): ?>
<tr>
<td><?php echo $rowSubKategori['kodeSubKategori'] ?></td>
<td><?php echo $rowSubKategori['namaSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['unsurAbstrakSubKategori'] ?></td>
<td><?php echo $rowSubKategori['aliasSubKategori'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Because you data array override each time that's y you get only last value every time.
You need to create your array with auto incremented value
$i=0;
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data[$i]['kodeSubKategori']=$kodeSubKategori;
$data[$i]['namaSubKategori']=$namaSubKategori;
$data[$i]['definisiSubKategori']=$definisiSubKategori;
$data[$i]['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data[$i]['aliasSubKategori']=$aliasSubKategori;
$i++;
}
Pass data in one array. and you can catch all in view at one foreach llop
Use this
In Controller
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemKodeSubKategori'] = $get_json_subkategori;
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
In View
<tr>
<?php
foreach($kodeSubKategori as $itemKodeSubKategori)
{
echo '<td>'.$itemKodeSubKategori['KodeSubKategori'].'</td>';
echo '<td>'.$itemKodeSubKategori['Nama'].'</td>';
echo '<td>'.$itemKodeSubKategori['Definisi'].'</td>';
echo '<td>'.$itemKodeSubKategori['UnsurAbstrak'].'</td>';
echo '<td>'.$itemKodeSubKategori['Alias'].'</td>';
}
?>
</tr>

php code to break page after 6th row

I try to display 2 table side by side with php code....
but i need to display only 6 table on single page ......and rest on another page......
so plz can any one help me to break page or break loop after 6th iteration..... 7th table display on another page like wise...
plz see my image attached below....i am facing problem on print preview...
page breaks my table during print...like below image...
I attached croped imaged here...
my page actuaally display 8 tables on single page...but i need is only 6 on one page.
below is my code..
<?php if (is_array($data)) { foreach($data as $row) { ?>
<table border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td colspan="4">Dainik Bhaskar Nagpur</td>
</tr>
<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>
<tr>
<td>Receipt Date</td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td>Coupon Date</td>
<td><?php echo htmlspecialchars($row['coupondate']); ?></td>
</tr>
<tr>
<td>Copy Start Date</td>
<td><?php echo htmlspecialchars($row['startingdate']); ?></td>
<td>HawkerName</td>
<td><?php echo htmlspecialchars($row['hawkername']); ?></td>
</tr>
<tr>
<td>SubagentName</td>
<td><?php echo htmlspecialchars($row['subagentname']); ?></td>
<td>CenterName</td>
<td><?php echo htmlspecialchars($row['ward']); ?></td>
</tr>
<tr>
<td>customer</td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td>Address</td>
<td><?php echo htmlspecialchars($row['society']); ?></td>
</tr>
</thead>
</table>
<?php } }?>
Try using CSS:
<style>
#media print {
.pageBreak {
page-break-after: always;
}
}
</style>
And at every 6 table, add a pageBreak:
<?php
$lineCounter = 0;
if (is_array($data)) {
foreach($data as $row) {
$lineCounter++;
?>
<!-- output a table... -->
<?php
if($lineCounter % 6 == 0) {
echo '<span class="pageBreak"></span>' . PHP_EOL;
}
}
}
?>
<?php
// get total records
TotalNoOfRecords = count($data);
$Count = 1;
foreach($data as $row) {
// write here your content
// break page after 6 rows
if($Count % 6 == 0 && $Count != $TotalNoOfRecords)
{
echo "<p style='page-break-after:always'></p>";
}
$Count++;
}
?>
try this is code or visit link
<?php
$q = "SELECT * FROM your_table ";
$myq = mysqli_query($link, $q);
$fixtures ='';
$i=0;
while($row=mysqli_fetch_assoc($myq)) {
$r[]=$row;
}
foreach ($r as $val) {
$i++;
?>
<!-- your value from database -->
<table>
<tr>
<td><?php echo $val['your_column']; ?></td>
</tr>
</table>
<!-- your value from database -->
<?php
if($i % 6==0){
echo '<div style="page-break-after: always;">[------ break ------]</div>' . PHP_EOL;
$i=0;
}
}
?>

Display a tables value when a record is not empty

I have an activity table that contain the list of activity student had joined before. So if the student is new student, there will have been no activity for that student.
<table align="center" width="1000" border="1" >
<h3>Activity List</h3>
</br>
<tr align="center" style="font-weight:bold" >
<td>ID</td>
<td>Activity</td>
<td>Sem</td>
<td>Session</td>
<td>Achievement</td>
<td>Level</td>
</tr>
<?php do { ?>
<tr align="center">
<td><?php echo $row_Recordset1['student_id']; ?></td>
<td><?php echo $row_Recordset1['activity']; ?></td>
<td><?php echo $row_Recordset1['sem']; ?></td>
<td><?php echo $row_Recordset1['session']; ?></td>
<td><?php echo $row_Recordset1['achievement']; ?></td>
<td><?php echo $row_Recordset1['level']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
How can I make this table only show on screen if it is not empty. Btw, im using session to display the exist record.
The best way to do this would be to get your array via mysql(i)_fetch_array() of the query you have build and then to chec to see if the query has rows like:
$qry = "SELECT `this` FROM `table`"
while ($result = mysql_fetch_array($qry)) {
if (mysql_num_rows($result) > 0) {
echo "We have rows!";
}
else {
echo "Looks like we haven't got anything here!";
}
}
I hope this helps.
Might also help to look here: PHP mysql_num_rows method.
<?php if(!empty($activity))
{
your msg ..
}
else
{
}
?>
where empty() will check a given variable is empty or not
Well that's what the if statement is used for:
if(!count($activities)) {
echo "This student has no activities yet.";
} else {
//display activities
....
}

How do I display a query in PHP 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

Categories