Alt attribute as a variable in php - php

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>";

Related

How to put text below photo

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.

PHP foreach loop with array table

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>

How to add links into my table with SQL and PHP

My code works good so far, but I want to make each table cell clickable where the real image opens by clicking on it.
How do I do that?
echo "<table>";
$res = "SELECT * FROM table1";
$result = $mysqli->query($res);
while($myRow = $result->fetch_array())
{
echo "<tr>";
echo "<td>";?> <img src=" <?php echo $myRow["image"]; ?>" height="100" width="100"><?php echo "</td>";
echo "</tr>";
}
echo "</table>"
Just wrap the small image in <a> tags
<a href="http://path_to_images/<?=$myRow["image"]?>">
<img src="<?php echo $myRow["image"]; ?>" height="100" width="100">
</a>
echo "<td>";?>
"<a href=" <?=$myRow["image"]; ?>">
<img src=" <?php echo $myRow["image"]; ?>" height="100" width="100" \>"<?php echo "
<a/>
</td>";
Just put <a href=""><a> outside the img?

Show picture from database don´t work

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.

multiple hidden fields

This is my first post, so I hope I'm doing it correctly.
This code displays several rows, and for each one there is a button that I would like
to redirect to the next form to edit the current line ID register.
I'm using variable hidden input names that are row-specific:
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."></td>";
echo "</tr>";
$count++;
}
?>
So, I would like to pass the hidden name value for a given row to the next form I'm working with.
There must be a really simple solution, but I'm really stucked. Thanks for your time.
You should use javascript/jQuery to do this. This can also be done in php way but then you need to use forms.
<?php
while($row = mysql_fetch_array($result))
{
$ID_variable[$count] = "ID".$row['ID'];
echo "<tr>";
echo "<td><input onClick=\"nextForm(".$ID_variable[$count].",".$row['ID'].")\" type=\"submit\" name=\"edit\" value=".$row['ID']."></td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td><input type=\"hidden\" name=".$ID_variable[$count]." value=".$row['ID']."> </td>";
echo "</tr>";
$count++;
}
?>
<script language="javascript">
function nextForm(name,value) {
document.location = document.location + "?name="+name+"&value="+value;
}
</script>
I have the following code that I use to show data from DB and edit/delete:
<table>
<?php foreach ($row as $key => $value) { ?>
<tr>
<td><?php echo $value['id']; ?></td>
<td><?php echo $value['title']; ?></td>
<td><?php echo substr($value['text'], 0, 150); ?></td>
<td>
<img alt="" src="../libs/imgs/icons/edit.png" />
<img alt="" src="../libs/imgs/icons/edit.png" />
</td>
</tr>
<?php } ?>
</table>
I hope to be helpfull.

Categories