How to display name under photo in a foreach loop - php

Edit: I'm using php to accomplish this.
I'm making a page that draws information from a database (name, photo path) and I want it to show the picture and then the name of who it is underneath.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> '.$fullname.' ';
}
As of now, the name is displayed next to the photo and just looks bad. How would I go about making the name appear below the photo?

While a little more code than simply adding a line break, you could wrap your image in a <figure> and add a <figcaption> after your <img>.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<figure><img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><figcaption>'.$fullname.'</figcaption></figure>';
}
To display multiple images per line, add the following to your CSS:
figure {
display: inline-block;
}
Check it out in this jsfiddle.

Try this: Add <br> to create a line break
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> <br>'.$fullname.' ';
}

There are different options for this depending on your needs. A simple solution is to add a line break after the image:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><br /'.$fullname.' ';
If you want more control on styling, you can wrap the name in a div or span and assign a class to style it further with CSS:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><div class="imagename">'.$fullname.'</div>';
Of course you could also wrap the entire image and text in a div, with or without the full name in its own div within there for further styling flexibility.

Related

Lightbox not displaying images when using MySQL and PHPMyAdmin

I have encountered a small problem when trying to make my website more advanced. To put it simple:
when I wanted to display images in a table using Lightbox and PHP everyting worked well:
<tr>
<td class="w-25">
<a href="telewizory/telewizor1.jpeg" data-toggle="lightbox" data-lightbox="telewizor1" class="col-sm-4" >
<img src="telewizory/telewizor1.jpeg" class="img-fluid img-thumbnail" alt="Sheep">
</td>
<td>Philips 70PUS6704/12</td>
<td>3999</td>
</tr>
The first column presented the image, the second - name and the last one - price.
how it works
However, when I wanted to add MySQL and created a database in PHPMyAdmin something went wrong. I can still read the name and the price well, but images don't show, I can only see the alternative text - Sheep in this example.
New code:
<?php
$kat_id = isset($_GET['kat_id']) ? (int)$_GET['kat_id'] : 1;
$sql = 'SELECT `img`, `nazwa` , `cena`
FROM `produkty`
WHERE `kategoria_id` = ' . $kat_id .
' ORDER BY `nazwa`';
$wynik = mysqli_query($polaczenie, $sql);
if (mysqli_num_rows($wynik) > 0) {
while ($produkt = #mysqli_fetch_array($wynik)) {
echo
'<tr>
<td class="w-25">
<a href="telewizory/' . $produkt['img'] . ' data-toggle="lightbox" data-lightbox="' . substr($produkt['img'], 0, strpos($produkt['img'], ".")) . '" class="col-sm-4">
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
<td>' . $produkt['nazwa'] . '</td>'
. '<td>' . $produkt['cena'] . '</td>
</tr>'
. PHP_EOL;
}
} else {
echo 'wyników 0';
}
mysqli_close($polaczenie);
?>
how it doesn't work
I have no idea what may be the reason that this code doesn't work. I would be very grateful for any help :)
You should check that the $produkt['img'] here
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
is replaced with the expected value from the DB.

Problem with displaying photo from database on page

I have problem with displaying photo from database on the page. I made a path in database column image_src = "../GameForest/gamephoto/gta5.jpg". And path is correct I checked it several times.
//This is a class that displaying all the data from the database
<?php
class Game extends Dbh {
public function gameDiv() {
$id = $_GET['id'];
$stmt = $this->connect()->query("SELECT g.game_id, g.game_name, g.image_src, g.genre_id, g.developer_id, g.release_date, g.platfrom_id, g.game_price, g.game_description, g.processor, g.graphic, g.ram\n" . "FROM game AS g\n" . "LEFT JOIN genre AS z\n" . "ON g.genre_id = z.id WHERE game_id = '$id'");
while ($row = $stmt->fetch()) {
echo "<div class='gameName'><h2>" . $row['game_name'] . "</h2></div>";
echo "<div class='buying'><p>" . $row['game_price'] . "€</p><a href='bought.php'><button>Buy Game</button></a></div>";
//This next echo is for displaying photo from database:
echo "<div class='gamePhoto'><img>" . $row['image_src'] . "</img></div>";
echo "<div class='gameGenre'><b>Genre: </b><p>" . $row['genre_id'] . "</p></div>";
echo "<div class='gameDeveloper'><b>Created by: </b><p>" . $row['developer_id'] . "</p></div>";
echo "<div class='gamePlatform'><b>Platform: </b><p>" . $row['platfrom_id'] . "</p></div>";
echo "<div class='gameRdate'><b>Release date: </b><p>" . $row['release_date'] . "</p></div>";
echo "<div class='gameDescription'><b>Description: </b><p>" . $row['game_description'] . "</p></div>";
echo "<div class='sysRequirements'><p>Recommended System Requirements:</p><b>Processor:</b><p>" . $row['processor'] . "</p>" . " Heading <b>Graphic:</b><p>" . $row['graphic'] . "</p>" . " <b>RAM:</b><p>" . $row['ram'] . "</p>";
}
}
}
**//This is instance for previous class:**
<?php
#istance for printing information about a Game
$game = new Game;
echo $game->gameDiv();
?>
**//This is CSS code of that photo:**
.gamePhoto {
margin: 10px 0 20px 10%;
width: 200px;
height: 400px;
float: left;
}
.gamePhoto img {
width: 500px;
height: 600px;
}
?>
I expect that there is a picture from database but I get only a gray frame where the image should actually be below that it writes "../GameForest/gamephoto/gta5.jpg" (the path I wrote in the base).
The rest of the database data are displayed normally it's just a problem with images.
On the other page (and other class) the same picture from the same database is normally displayed and I have no problem.
img is inline block,use it like <img src="" />
Change this
<img>" . $row['image_src'] . "</img>
to this
<img src=" . $row['image_src'] . ">

How to concatenate sql, html and php with this code

Basically it is an element which shows an image from the path on database. But I just can't make it work.
<img class="img-circle profile_img" id="blah" src=" <?php $query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--cover" id="blah" src="'.$row2['img_local'].'" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src=1.png;">';
} else {
echo 'Show a default image.'; // <img src="path_to_default_image" alt="Default_image"/>
}
} ?>" alt="Avatar" title="DEFINIDA" >
I have managed to work, the final code:
<?php
$query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--profile" src="'.$row2['img_local'].'" title="Clique para abrir seu perfil">';
} else {
echo '<img class="image--profile" src="images/user.png">';
}
}
?>

foreach loop not displaying desired result

Hi guys and thank you for your time. My question is regarding.
I am trying to loop over images in my folder along with a post in the database with the end result looking like this:
Post 1 Image 1
Post 2 Image 2
Post 3 Image 3
At the moment i get this result:
Post 1 Image 1
Post 1 Image 2
Post 1 Image 1
Post 2 Image 1
Post 2 Image 2
Post 2 Image 3
I do not want this result.
Below is my code:
$post_info = get_posts();
foreach ($post_info as $info){
$photos = glob('design/img/*');
foreach($photos as $photo) {
echo " <a href='feed.php?pid=".$info['post_id']." ' >
<div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2>
<h3 style='color: black'> ".$info['body']." </h3> </div> </a>";
echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";
}
}
Thanks for your time.
Try this out (minus potential language specifics since I didn't actually run to check this code).. It's basically a regular for loop instead of a foreach.
$post_info = get_posts();
$photos = glob('design/img/*');
if (count($post_info) === count($photos)) { // According to your requirement, the counts would be the same
$count = count($post_info);
for ($i = 0; $i < $count; $i++) {
$info = $post_info[$i];
$photo = $photos[$i];
echo " <a href='feed.php?pid=".$info['post_id']." ' > <div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2>
<h3 style='color: black'> ".$info['body']." </h3> </div> </a>";
echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";
}
}
Hope that helps :)
Getting image details from get_posts() and removing inner foreach loop may fix your problem.
Note: replace $info['something_like_post_image'] with your image field.
$post_info = get_posts();
foreach ($post_info as $info) {
//$photos = glob('design/img/*');
//foreach ($photos as $photo) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $info['something_like_post_image'] . "' width='285px' height='200px' style='border: 5px solid black'>";
//}
}
UPDATE
/*
* If your images have any naming convention like
* imageFileName = "image_{POST_ID}.jpg"
* then you can use below code (NO DATABASE ENTRY REQUIRED)
* (ie, For post #1 image file would be "image_1.jpg";
* and for post #2 image file would be "image_2.jpg")
*/
$post_info = get_posts();
foreach ($post_info as $info) {
//filename = image_1.jpg or image_2.jpg or...
$photoFileName = 'design/img/' . 'image_' . $info['post_id'] . '.jpg';
if (file_exists($photoFileName)) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}
}
NOTE: You should have to keep a relation with each post against your unique image; otherwise you will not be able to get that unique image with your post, while listing.
Checkout below options to handle this situation.
You can keep image name in database (for each post, you can get your image name directly from database)
Use a naming convention for your images (for post #1 use unique image name (say image_1, for post #2 image_2 etc)
UPDATE - 2
If you are looking for a cycle-through images (without any condition), use below code
/*
* If you are looking for a solution that cycles each images
* along with each post, try this one
*/
$post_info = get_posts();
$photos = glob('design/img/*');
$numPhotos = count($photos) + 1;
//assuming your post# starts with 1
$imageId = 1;
foreach ($post_info as $info) {
//cycling
if ($imageId % $numPhotos === 0) {
$imageId = 1;
}
$photoFileName = 'design/img/' . 'image_' . $imageId++ . '.jpg';
//no need of this checking, since you are cycling
//if (!file_exists($photoFileName)) {
// $photoFileName = 'path/to/default/image.jpg';
//}
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}

Image not showing

I have used this code for accessing image from database to web page. But after debugging there is no image in image tag.
This code prints the correct path....but fails to return correct image in image tag...
echo $_SESSION['user_image'];
How do I get the image in image tag?
plz help me.
while($row = mysql_fetch_assoc($result))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['user_level'] = $row['user_level'];
//$_SESSION['user_image'] = $row['image'];
$image=$row['image'];
$user_image= trim('C:/xampp/htdocs/source/img/' . $image);
//$user_image = imagecreatefromstring($user_image);
$_SESSION['user_image']=$user_image;
echo $_SESSION['user_image'];
}
echo "<img src='".$row['image']."'>";
echo 'Welcome, <img src= "'.$_SESSION['user_image'].'" alt="" width="50"
height="40" /> ' . $_SESSION['user_name'] . '. <br /><a href="index.php">Proceed to
the forum overview</a>.';
You are using the local path in the server.
'C:/xampp/htdocs/source/img/' . $image
Should be
'/source/img/' . $image

Categories