How to add links into my table with SQL and PHP - 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?

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.

Image and words with classes won't display (PHP CSS HTML)

Got problem displaying the below picture and words, what am I doing wrong?
echo "<img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /><br />";
echo "<h4 class="text-info"><?php echo $row["name"]; ?></h4>";
AS you are working in php so you have no need to echo <?php echo $row["image"]; ?> in side an echo and you have to consider about single quotes and double quotes
Do like this
echo "<img src='images/".$row["image"]."' class='img-responsive' /><br />";
echo "<h4 class='text-info'>".$row["name"]."</h4>";
At first you need to use php tag for translating echo tag after do like this..
<?php
echo '<img src="images/echo $row["image"];" class="img-responsive" /><br/>';
echo '<h4 class="text-info"> echo $row["name"]; </h4>';
?>
try this.
echo "<img src='images/'". $row['image']." class='img-responsive' /><br />";
echo "<h4 class='text-info'>".$row['name']."</h4>";

Selecting data from sql to create a thumbnail

I'm creating a thumbnail with a title, image and caption on it. I'm trying to select data from my table to show it into my homepage. Can someone help me to create a normal thumbnail in my php that contains the detail from my sql. I tried to search and can't find how to create a thumbnail using php and not html.
$sql = "SELECT * FROM news";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['caption'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
In short, is there a way to create a php file from this?
<div class="col-md-4">
<div class="thumbnail">
<img alt="Memory" img src="../../images/a2.jpg">
<div class="caption">
<h3><b>
Title
</b></h3>
<p>
Caption Caption Caption Caption Caption
</p>
<p align="right">
<a class="btn btn-primary" href="news2.html">Read More</a>
</p>
</div>
</div>
</div>
Do you want to replace your table structure with that template structure? You'll need to adjust some of the data to fill the hyperlink (I don't know how you want to build that).
while($row=mysqli_fetch_array($result)){
echo "<div class=\"col-md-4\">";
echo "<div class=\"thumbnail\">";
echo "<img alt=\"Memory\" src=\"../../images/{$row["image"]}\">";
echo "<div class=\"caption\">";
echo "<h3>{$row["title"]}</h3>";
echo "<p>{$row["caption"]}</p>";
echo "<p align=\"right\">";
echo "<a class=\"btn btn-primary\" href=\"news2.html\">Read More</a>";
echo "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
}

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.

Alt attribute as a variable in 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>";

Categories