I want add text just below the picture but I don't know how to do it.
<?php
include "includes/connexio_web.php";
obrirConnexioBD();
$sql = "SELECT * FROM Llistat_vies";
$sth = $conn->query($sql);
while($row=mysqli_fetch_array($sth)) {
echo "<tr>";
echo "<td>"; ?>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200"><?php echo "</td>";
echo "<td>"; echo $row["text"]; echo "</td>";
echo "</tr>";
}
?>
Here your code updated.
<?php
include "includes/connexio_web.php";
obrirConnexioBD();
$sql = "SELECT * FROM Llistat_vies";
$sth = $conn->query($sql);
$table= "<table>";
while($row=mysqli_fetch_array($sth)){
$table= "<tr>
<td align='center'>
<img src=".$row["photo"] ." height='200' width='200'> <br>
$row["text"]
</td>
</tr>";
}
print $table . "</table>";
?>
try these new HTML5 tags:
<figure> and <figcaption>
Put your image and text inside these tags like this:
echo "<tr>";
echo "<td>"; ?>
<figure>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200">
<figcaption>
<?php echo $row["text"];
echo "</figcaption>";
echo "</figure>";
echo "</td>";
echo "</tr>";
I am pretending that your <table> and <tr> are outside of the loop.
<table>
<tr>
while($row=mysqli_fetch_array($sth)) {
echo "<td>";
echo "<table>";
echo "<tr>";
echo "<td>";
?>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200">
<?php
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo $row["text"];
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
}
</tr>
</table>
I put tables within the table an inside of a <td> so that they would be next to each other.
I don't know if this is the best way but it was similar to what you were doing and it worked for me. I hope this helps.
Related
I got the following table with a "view" button in a template.php. Im using bootstrap 3.3.7
<?php
if($num>0){
echo "<table class='table table-responsive table-sm table-hover table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th></th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>{$description}</td>";
echo "<td>";
echo "<a href='view_thing.php?id={$id}' class='btn-info btn-sm'>";
echo "<span class='glyphicon glyphicon-eye-open'></span> View";
echo "</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
It looks like:
table test 1
What i tried:
<th class="col-md-1"></th>
<th style="width: 10%"></th>
What it should do:
The last column should fit the button size.
Solution:
echo <th style=width: '10%'></th>
How do I change it into table form with three headings which are: Company name (ABC and XYZ), Branch (Kuching and Sibu for both companies respectively), and Staff name (for both companies according to the array). I'm a beginner. Please help as I'm stuck. Something's wrong with my code.
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table>
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
foreach($v_company as $v_company_name=>$v_company_info){
echo "<tr>";
echo "<td>";
echo "$v_company_name <br>";
echo "</td>";
echo "</tr>";
foreach($v_company_info as $v_branch=>$v_staffs){
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
foreach($v_staffs as $v_staff){
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>";
}
}
}
?>
</table>
</body>
</html>
You can use this code
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>
array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table>
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
$company = '';
$branch = '';
$staff = '';
foreach($v_company as $v_company_name=>$v_company_info){
foreach($v_company_info as $v_branch=>$v_staffs){
foreach($v_staffs as $v_staff){
echo "<tr>";
echo "<td>";
if($company == '' || $company != $v_company_name){
$company = $v_company_name;
echo "$v_company_name <br>";
}
echo "</td>";
echo "<td>";
if($branch != $v_branch){
$branch = $v_branch;
echo "$v_branch <br>";
}
echo "</td>";
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>
You can copy paste if you want. Happy coding
I updated your foreach loop echo value's try this:
<?php
foreach($v_company as $v_company_name=>$v_company_info){
$rowcount = 0;
foreach($v_company_info as $v_branch=>$v_staffs){
echo "<tr>";
echo "<td>";
echo ($rowcount == 0 )? $v_company_name : "";
echo "</td>";
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
echo "<td>";
foreach($v_staffs as $v_staff){
echo "$v_staff <br/>";
}
echo "</td>";
echo "</tr>";
$rowcount +=1;
}
}
?>
Is this your desired Output?
You can use this to get all the employees in separate rows
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table border="1" width="100%">
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
foreach($v_company as $v_company_name=>$v_company_info){
foreach($v_company_info as $v_branch=>$v_staffs){
foreach($v_staffs as $v_staff){
echo "<tr>";
echo "<td>";
echo "$v_company_name";
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>";
echo "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>
I wnt to show some pictures i have in a mysqli database, they are stored as blob.
But all i get is a A4 page with wierd text..
this is my code that i use too show(i connect to my database above this code):
echo "<table border=1>\n";
/*echo " <tr>\n";
echo " <th>Bild</th><th>Namn</th><th>Betygsättning</th>\n";
echo " </tr>\n";*/
while ($rad = mysqli_fetch_array($sql_result)) {
echo "<tr>";
echo "<td>";?> <img src="<?php echo $rad["Bild"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $rad["Namn"]; echo "</td>";
echo "</tr>";
}
echo "</table>\n";
If you really want to embed the image to the generated html you should write it as this here:
echo "<td>";?> <img src="data:image/jpeg;base64,<?php echo base64_encode($rad["Bild"]); ?>" height="100" width="100"> <?php echo "</td>";
Here again with a little more clean code:
echo "<table border=\"1\">\n";
while($rad = mysqli_fetch_array($sql_result)) {
echo " <tr>\n";
echo " <td>\n";
echo " <img src=\"data:image/jpeg;base64," . base64_encode($rad["Bild"]) . "\" height=\"100\" width=\"100\" />\n";
echo " </td>\n";
echo " <td>" . $rad["Namn"] . "</td>";
echo " </tr>\n";
}
echo "</table>\n";
It seem that you forgot also to close your img tag.
echo '<table border=1>';
/*echo " <tr>\n";
echo " <th>Bild</th><th>Namn</th><th>Betygsättning</th>\n";
echo " </tr>\n";*/
while ($rad = mysqli_fetch_array($sql_result)) {
echo '<tr>
<td><img src="'.$rad["Bild"].'" height="100" width="100"></td>
<td>'.$rad["Namn"].'</td>
</tr>';
}
echo '</table>';
That should do it. While you are in a block of php code you can concatonate on it like above.
Having trouble calling up table names row as a Alt attribute
$Linkimg = $row['Linkimg'];
$Name = $row['Name'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='60'>" ;
echo "<img src=\"{$row['Linkimg']}\" alt=\"{$row['Name']}\>" ."</td>";
what am I missing those darn brackets
It's safer to do this:
if($i===0) { ?>
<tr>
<?php } ?>
<td>
<img src="<?php echo $LinkImg; ?>" alt="<?php echo $Name; ?>"/>
</td>
Try this:
echo "<img src=\"{$row['Linkimg']}\" alt=\"{$row['Name']}\">" . "</td>";
I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP