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>
Related
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>";
I would like the table to display all the values inside of it, but it is currently not displaying the values and only creating an empty table
[what the table is displayed as (image)][1]
$result = mysqli_query( $conn,'SELECT * FROM Pictures ');
$conn->close();
Html
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;"
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if( $result != null){
Result is not empty
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
}else{
echo "Something went wrong with the result";
}
?>
</tbody>
</table>
<?php //mysqli_close($conn); ?>
</body>
</html>
changed the out put to match the answer you gave but the output came out as picture 2 while my table is actually picture 3 any ideas
output:
table im trying to display
Display the data in this way:
<td><?php echo $row['id']; ?></td>
Try changing
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
to
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
or the shorthand variant
<td><?= $row['id']; ?></td>
<td><?= $row['hname']; ?></td>
<td><?= $row['himage']; ?></td>
<td><?= $row['hdesc']; ?></td>
And as Samuel pointed out in a comment, are you sure there is a need for the extra foreach considering you're already looping with the while?
Update: OP have you tried the following?
while( $row = mysqli_fetch_assoc( $result ) ){
//removed foreach()
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
</tr>
<?php
}
}else{
Update 2 OP wishes to have the image load instead of showing the raw URL.
To do this, we need to use an actual image tag and insert the url into the src.
I assume that this line is the image URL
<td><?php echo $row['himage']; ?></td>
So change it to
<td> <img src="<?php echo $row['himage']; ?>" > </td>
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to do a school project and the result only shows me 1 data from my MySQL table. Here is the code:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
}
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
</table>
Can anyone help tell me what did I am missing?
As the other commenters have said, you need to include the output block within the while loop, else you will only get the output from the last row.
Try this:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php } ?>
</table>
Really all the intermediate variables you're using are redundant, so this is equivalent and much shorter:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
Or more concise still, without the messy switching between html and php modes:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
echo "<table>";
while($row= mysql_fetch_array($run)){
echo "<tr>";
echo "<td>$row['Name']</td>";
echo "<td>$row['startdate']</td>";
echo "<td>$row['finaldate']</td> ";
echo "<td>$row['within']</td>";
echo "<td>$row['reason']</td>";
echo "<td>$row['remaining']</td>";
echo "<td>$row['status']</td>";
echo "</tr>";
}
echo "</table>";
If you want headers too, you can do as below:
<?php include("connect.php");?>
<table>
<tr>
<th>Name</th>
<th>Start Date</th>
<th>Final Date</th>
<th>Within</th>
<th>Reason</th>
<th>Remaining</th>
<th>Status</th>
</tr>
<?php
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query) or die(mysql_error());
?>
<?php
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
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.