Codeigniter: adding label to foreach echo - php

This are my view code:
<?php
foreach($books as $books) {
echo $books->book_id;
echo $books->book_name;
echo $books->description;
echo $books->author;
echo $books->publisher;
echo $books->pages;
echo $books->publication_date;
echo $books->price;
echo $books->status;
echo $books->quantity;
echo $books->genres;
echo $books->user_rating;
echo $books->reviews;
}
?>
the output is
However i want the output to be like Book ID:1, Book Name:qweqweqweqweqwe, price 123.00 and so on, in a table form am i able to do it?

I hope this will help you..
<table>
<th>
<td>Book Id</td>
<td>Book Name</td>
<td>Description</td>
<td>Author</td>
<td>Publisher</td>
<td>Pages</td>
<td>Publication Date</td>
<td>Price</td>
<td>status</td>
<td>Quantity</td>
<td>Genres</td>
<td>User Rating</td>
<td>Reviews</td>
</th>
<?php foreach($books as $book) { ?>
<tr>
<td><?php echo $book->book_id ?></td>
<td><?php echo $book->book_name ?></td>
<td><?php echo $book->description ?></td>
<td><?php echo $book->author ?></td>
<td><?php echo $book->publisher ?></td>
<td><?php echo $book->pages ?></td>
<td><?php echo $book->publication_date ?></td>
<td><?php echo $book->price ?></td>
<td><?php echo $book->status ?></td>
<td><?php echo $book->quantity ?></td>
<td><?php echo $book->genres ?></td>
<td><?php echo $book->user_rating ?></td>
<td><?php echo $book->reviews ?></td>
</tr>
<?php } ?>
</table>

concat your string with php function.
echo "Book ID:".$books->book_id.", Book Name: ".$books->book_name;
Like this way.

Use like this:
echo "Book ID : $books->book_id";
double quotes will automatically identify variables and execute them.
you can also try:-
echo 'Book ID:'. $books->book_id;
Please choose which one easily readable for you.

Related

infusionsoft dsfind().Divide data into 3 chuncks in different pages

data i am fetching from infusion using tag id and dsfind() function. and that data i have to divide into 3 pages. (data used be unique)
<?php
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("infusionid"))
{
$returnFields = array('Id','Email');
$contacts = $app->dsFind('Contact',14,0,'Groups',7406,$returnFields);
echo '<pre>', print_r($contacts), '</pre>';
}
?>
I want to print this series
<tr>
<td><?php echo $contacts[0]['Id'] ?></td>
<td><?php echo $contacts[0]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[4]['Id'] ?></td>
<td><?php echo $contacts[4]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[7]['Id'] ?></td>
<td><?php echo $contacts[7]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[11]['Id'] ?></td>
<td><?php echo $contacts[11]['Email']; ?></td>
</tr>
You can use foreach() function e.g:
echo "<table>";
foreach ( $contacts as $var) {
echo "<tr><td>ID: ", $var['Id']."</td>";
echo "<td>Email: ", $var['Email']."</td></tr>";
}
echo "</table>";

Loop php values from an echo in HTML

I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.

Change row color of a table based on mysqli value using materializecss

I'm using Materialize css on my project, i have a table that shows mysql field called "status" and in this table i want change the color of the row if i change the "status" like "1=blue, 2=red..." Someone here knows how i can make a function to do this? Thank you.
table extample:
table class="striped bordered responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Cliente</th>
<th>Objeto</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr>
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
</tbody>
One simple way would be:
<?php
$colorMap = [
1 => 'blue',
2 => 'red',
// add more
];
while ($row_os = mysqli_fetch_assoc($result_user)) { ?>
<tr style="background:<?php echo $colorMap[$row_os['status']] ?>">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
Of course you could also add a class depending on status the same way and do the styling in CSS.
Simple as Doing this on the row , only if the status must contain a value of either 1 or 2
<?php
while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr class="<?php echo $row_os["status"]==1?'blue':'red'?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
or if status varies (then follow #colburton answers ) or this
<?php
while($row_os = mysqli_fetch_assoc($result_user)){
$color="";
switch($row_os["status"]){
case 1:
$color="blue";
break;
case 2:
$color="red";
break;
//and so on
}
?>
<tr class="<?php echo $color;?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>

Display multiple arrays in a html table

I'm trying to display multiple arrays into a table so that each value is on a separate line within the table.
This is my current setup:
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><? echo $res['partnumber']; ?></td>
<td><? echo $res['partdescription']; ?></td>
<td><? echo $res['partprice']; ?></td>
<td><? echo $res['partquantity']; ?></td>
<?php
}
?>
Which displays the following:
Screenshot
instead i need it to display each value on a separate line
I tried the following but it duplicates its value over and over again.
<? foreach ($res as $row) : ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<? endforeach;
}
?>
First of all, your code is vulnerable to SQL injection, make sure to secure $id (ex. $id = (int)$_GET['id'];) before putting it into your query string.
then try:
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><? echo $res['partnumber']; ?></td>
</tr><tr>
<td><? echo $res['partdescription']; ?></td>
</tr><tr>
<td><? echo $res['partprice']; ?></td>
</tr><tr>
<td><? echo $res['partquantity']; ?></td>
</tr>
<?php
}
?>
If you need new line for each column must simply add new tr for each td
Try this:
<? foreach ($res as $row) : ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
</tr>
<tr>
<td><? echo $row['partdescription']; ?></td>
</tr>
<tr>
<td><? echo $row['partprice']; ?></td>
</tr>
<tr>
<td><? echo $row['partquantity']; ?></td>
</tr>
<? endforeach; ?>
Notice the change in the starting curly braces, and that i removed the endforeach you used, and the table tags...
Now you can make use of this in your for each loop.
<table>
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
?>
</table>
I think this should do the trick
This is what i'm getting with your code:
(i'll fix the security issue after i get this working, thank you)
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
?>
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
}
?>
$arr1 = ['name'=>'jon', 'age'=>30, 'address'=>'dilly'];
$arr2 = ['name'=>'gita', 'age'=>20, 'address'=>'goha'];
$arr3 = ['name'=>'sita', 'age'=>20, 'address'=>'pune'];
$agroup = array([$arr1, $arr2, $arr3]);
?>
<table>
<?php foreach($agroup as list($a, $b, $c)){ ?>
<tr>
<td><?php echo $a['name']; ?></td>
<td><?php echo $b['name']; ?></td>
<td><?php echo $c['name']; ?></td>
</tr>
<tr>
<td><?php echo $a['age']; ?></td>
<td><?php echo $b['age']; ?></td>
<td><?php echo $c['age']; ?></td>
</tr>
<tr>
<td><?php echo $a['address']; ?></td>
<td><?php echo $b['address']; ?></td>
<td><?php echo $c['address']; ?></td>
</tr>
<?php } ?>
</table>

Failed to export to .xlsx format

I tried to export my table to .xlsx format. But it is failed to do that. So far I did this:
header('Content-type: application/octet-stream');
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //should be a correct mimetype
header('Content-Disposition:attachment; filename="contoh-1.xlsx"');
Below is the error:
Here is how I generate the data.
Controller:
public function kategori_excel(){
$paramKategori = $this->input->get('CategoryCode');
$get_url_kategori = $this->ws_url->GetUrl('CategoryRetrieve?CategoryCode='.$paramKategori);
$get_json_kategori = json_decode(file_get_contents($get_url_kategori), true);
$data['itemKategoriToExcel'] = $get_json_kategori['Kategoris'];
$this->load->view('kategori_excel', $data);
}
And here is the view:
<table border='1' width="100%">
<tr>
<th>Kode</th>
<th>Nama</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
<th>Sumber Definisi</th>
<th>Tanggal</th>
<th>Tipe Tanggal</th>
<th>Edisi</th>
<th>Role</th>
<th>Other Citation Detail</th>
<th>Nama Katalog</th>
<th>Nama Organisasi</th>
</tr>
<?php foreach($itemKategoriToExcel as $item): ?>
<tr>
<td><?php echo $item['KodeKategori'] ?></td>
<td><?php echo $item['Nama'] ?></td>
<td><?php echo $item['Definisi'] ?></td>
<td><?php echo $item['UnsurAbstrak'] ?></td>
<td><?php echo $item['Alias'] ?></td>
<td><?php echo $item['SumberDefinisi'] ?></td>
<td><?php echo $item['Tanggal'] ?></td>
<td><?php echo $item['TipeTanggal'] ?></td>
<td><?php echo $item['Edisi'] ?></td>
<td><?php echo $item['Role'] ?></td>
<td><?php echo $item['OtherCitationDetail'] ?></td>
<td><?php echo $item['NamaKatalog'] ?></td>
<td><?php echo $item['NamaOrganisasi'] ?></td>
</tr>
<?php endforeach; ?>
</table>
How can I fix this thing(s)?

Categories