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>
Related
I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp
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.
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 } ?>
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>
I'll like to know if there is a code I could use to prevent table rows from forming if there is no data in the custom fields.
For example this is my table:
<tr>
<td><?php echo $place_1; ?></td>
<td><?php echo $person_1; ?></td>
<td><?php echo $status_1; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_3; ?></td>
<td><?php echo $person_3; ?></td>
<td><?php echo $status_3; ?></td>
<td></td>
</tr>
How can I make it that "if $place_2 is empty, hide all the table rows of 2 and 3?"
Any help is appreciated!
You could simply use PHP to only output the row when it is not empty:
...
</tr>
<?php if ($place_2 != "") { ?>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<?php } ?>
<tr>
...
This approach wraps the tr element in an if block. There are other many ways to achieve the same result, which may show up in other answers. Based on your question, it looks like you want to suppress $place_3 as well when $place_2 is empty. I assume $place_3 would be empty in this case, so you could apply the same approach to that tr element, substituting $place_2 with $place_3.
You can use a WHILE loop also . This will work for any number of rows. Simply replace 4 with number of rows to be checked in While condition
<?php
$i=0;
while($i<4)
{
if($place_.$i == "")
break; // if $place_ variable is empty no further rows are printed.
else
{
?>
<tr>
<td><?php echo $place_.$i; ?></td>
<td><?php echo $person_.$i; ?></td>
<td><?php echo $status_.$i; ?></td>
<td></td>
</tr>
<?php
} // closing bracket of else
$i = $i + 1;
} //end of while loop
?>