I have a number of images in my db and they all correspond to the same project ID.
I want them to be displayed next to each other in the browser.
But the following code is output only the latest image but not all of them:
//get all the images in this project and add them to the content variable for output
if (!empty($FormProjectID)) {
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
if (mysqli_num_rows($DBQuery3) < 1) {
$content = '
<p>This project is empty. Upload some files to get started.</p>
';
} else {
while($row = mysqli_fetch_array($DBQuery3)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$content = '
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
';
}
}
How do I get all the image so that in the end the html looks like this:
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
Later on in my html page I have:
<?php echo $content; ?>
change $content = ' to $content .= '
PHP String Operators
Note: set the $content variable to empty string or null before your while loop
What you're looking for is string concatenation (see string operators)
to fix your problem you'll need to first initialise an empty string, do this before your while loop
} else {
$content = '';
while($row = mysqli_fetch_array($DBQuery3)) {
and then use the concatenation operator . to append each image
$content .= '
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
';
Related
I have an image that is stored in the database in binary form. I have rescued this image (with a select) and would like to insert it as a div's background image. I can only enter it as src of an img.
How to Store in Database:
<?php
$foto = $_FILES['image']['name'];
if($this->__get('image') != "")
{
$binary = file_get_contents($this->__get('foto'));
$query = "UPDATE usuarios SET image = :image, name = :name" ;
$query .= "WHERE id = :id";
$stmt = $this->db->prepare($query);
$stmt->bindValue(':image', $binary);
$stmt->bindValue(':name', $this->__get('name'));
$stmt->bindValue(':id', $this->__get('id'));
$stmt->execute();
}
?>
Part I enter as src of an img. (
it's working)
<?php
function dataURI($bin)
{
return 'data: image/gif;base64,'.base64_encode( $bin );
}
?>
<div class="row mt-2">
<div class="col-lg-12 text-center" id="div-foto-usuario">
<?php
if(!empty($this->view->info_usuario['image']))
{
$image = dataURI($this->view->info_usuario['image']);
echo "<img id='img-image-user' class='mr-2 border border-secondary' src='$image' style='border-radius:50%;width:120px; height:120px;'>";
}
else
{
echo "<img id='img-image-user' class='mr-2 border border-secondary' src='/img/user.jpg' style='border-radius:50%;width:120px; height:120px;'>";
}
?>
</div>
</div>
I would like to insert in the div background-image below: (The problem)
<div class="row">
<div class="col-lg-12" style="background-image: url(<?php echo $foto;?>);">
</div>
</div>
I tried to echo the variable that contains the image inside the url (), but it didn't work out.
Generated Page HTML Code:Fiddle
I am brazilian. I don't speak english. I used the google translate.
Use
<div id="postimave" style="background-image: url('<?php echo $image; ?>')">Some text
</div>
I have two include files:
1.draw_images.php
<?
function practice_area_img($img_path)
{
$items = "";
$files = glob($img_path . "/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
$items .= <<<HTML
<div class="item">
<img src="$num" class="img-responsive" alt="">
</div>
HTML;
}
return $items;
}
?>
The above code snippet goes through a folder and draws all the images therein into stylized div tags.
Please note here that I get the div blog-slider drawn, but the images fail. The image path is of this kind:
/opt/lampp/htdocs/my_project/images/my_pic.jpg
2.definitions.php
<?php
$rev_args['images_path'] = dirname(__FILE__) . '/images';
?>
I call them as follows:
<?php
include(dirname(__FILE__) . '/definitions.php');
include(dirname(__FILE__) . '/draw_images.php');
<div id="blog-slider" class="owl-carousel owl-theme">
<?= practice_area_img($rev_args['images_path']); ?>
</div>
How can I go about getting the image drawn?
UPDATE
Please nothe that <?= $num; ?> prints a something like /opt/lampp/htdocs/my_project/images/my_pic.jpg
so:
<img src="$num" class="img-responsive" alt="">
is actually :
<img src="/opt/lampp/htdocs/my_project/images/my_pic.jpg" class="img-responsive" alt="">
behind the scenes.
Without seeing the source of the generated html, the only thing I can see, is that the image paths are probably wrong.
You display the image using:
<img src="$num" class="img-responsive" alt="">
However, you look in the images/ directory of the script path to get them, so instead of just echoing the filenames, you should probably use something like:
<img src="images/$num" class="img-responsive" alt="">
Edit: Based on your comment, you would need something like:
$num = basename($files[$i]);
...
<img src="images/$num" class="img-responsive" alt="">
It is a absolute path problem in your img tag
<?
function practice_area_img($img_path)
{
$items = "";
$files = glob($img_path . "/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
// remove absolute path
$relPath = str_replace(dirname(__FILE__)."/", "", $num);
$items .= <<<HTML
<div class="item">
<img src="$relPath" class="img-responsive" alt="">
</div>
HTML;
}
return $items;
}
?>
This will produce:
<img src="images/my_pic.jpg" class="img-responsive" alt="">
which it will search the image under your server you can put in front of it
http(s)://localhost (servername) if you want to.
I have this php array. Where in my database i have a field called body, and in that field there is some html code. Like this:
<h1>title</h1><img src="http://farm5.staticflickr.com/4075/4788694752_d03557765b_z.jpg" alt=""/>
Here is my php code:
<?php
$q = "SELECT * FROM journals ORDER BY timestamp DESC";
$r = mysqli_query($dbc, $q);
while($journal_list = mysqli_fetch_assoc($r)) { ?>
<div class="col-md-4">
<a class="list-group-item" href="journal.php?id=<?php echo $journal_list['id']; ? >">
<h4 class="list-group-item-heading"><?php echo $journal_list['body']; ยด?></h4>
</a>
</div>
<?php } ?>
In the h4 im calling the body field in the database. But i only want the img in that field??
Try phpquery:
$src = phpQuery::newDocumentHTML($journal_list['body'])->find('img')->attr('src');
This is a little hack that you can use
$img = explode('img',$journal['body']);
$final_img = '<img '.$img[1];
Now echo image as
<h4 class="list-group-item-heading"><?php echo $final_img; ?></h4>
I have written a page that displays images in a list style with a jquery slider added to it. However, what i need help with is for the jquery slider to recognizes it as a list of images so that the slider can work. Any help would be much appreciated.
Below is my code:
<div id="slideshow">
<ul class="slides">
<?php
$target = "admin/photogallery/";
$getImages = mysql_query("SELECT * FROM photogallery");
if(!$getImages) die("Cannot execute query. " . mysql_error());
$target = "admin/photogallery/";
while($row = mysql_fetch_array($getImages)){
if (file_exists($target)) {
print '"<li><img src="http://localhost/crystalvirgins/admin/photogallery/'.$row_rsphotogallery['photo'].'" border="0" width="702" alt="'.$row_rsphotogallery['description'].'" /></li>"
<div class="clear_3"></div>';
$i++;
}
}?>
</ul>
<span class="arrow previous"></span>
<span class="arrow next"></span>
</div>
just try this code
<ul>
<?php
$directory = "data/uploads/homegallery/";
if (glob($directory . "*") != false)
{
$filecount = count(glob($directory . "*"));
}
else
{
}
$files_index = glob("data/uploads/"."home"."gallery/*.*");
for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];
?>
<li><img src="<?php echo $num2;?>" width="650" height="276" alt="" /> <?</li>
}?>
</ul>
Please include the jQuery Plugin and code being used. Also what does the generated HTML source look like.
I am using the Facebook Graph API to get the users locale information (updated code):
[more code]
<div id="scoreboard-overview">
<ul>';
$num_rows = mysql_num_rows($resulttt);
$i = 0;
while($row = mysql_fetch_array($resulttt)) {
$i = $i + 1;
$fb_data = json_decode(file_get_contents('http://graph.facebook.com/'. $row['fbID']));
$fb_locale_str = $fb_data->locale;
$fb_name_str = $fb_data->first_name;
$fb_country_str = strtolower(substr($fb_locale_str, -2));
$flag_uri = '/flags/unknown.gif'; //display a general flag if unknown.
if (!empty($fb_country_str) && in_array($fb_country_str, $valid_flags) )
$flag_uri = '/flags/' . $fb_country_str . '.gif';
echo '<li>
<div class="container">
<div class="black">
<img src="https://graph.facebook.com/'. $row['fbID'] .'/picture" alt="" />
</div>
<div class="grey">
'.$i.' '.$fb_name_str.'';
printf ('<div class="test"><img src="%s" /></div>', $flag_uri);
echo ' </div>
<div class="holder">
<div class="blue">
<p>0<br />'. $row['Time'] .'</p>
</div>
<div class="red">
<img src="http://ep2.nl/images/star.gif" alt="" />
</div>
<div class="yellow">
<p>'. $row['Score'] .'</p>
</div>
</div>
</div>
</li>';
}
echo '</ul>
</div>
[more code]
$row['fbID'] is the users and/or friends Facebook ID (used in a array)
Well this gives a result like: en_US where en is the users language and US is the users country. The country is where I am after.
I have a couple of flag images that look like this: us.png. These flags are named using the ISO3166-1 alpha-2 country codes where appropriate. So I want to trim this en_US to us and put .png behind it so I can show the flag image. But how to do this?
Also a general flag if the locale returns nothing like unknown.png would be nice.
Many thanks,
Maurice
Not too hard to do.
Try this:
$valid_flags = array ('us', 'ca', 'mx', '...') //You'll need to populate this one
$fb_data = json_decode(file_get_contents('http://graph.facebook.com/'. $row['fbID']);
$fb_locale_str = $fb_data->location;
$fb_country_str = strtolower(substr($fb_locale_str, -2));
$flag_uri = '/flags/unknown.png'; //display a general flag if unknown.
if (!empty($fb_country_str) && in_array($fb_country_str, $valid_flags) )
$flag_uri = '/flags/' . $fb_country_str . '.png';
printf ('<div class="grey"><img src="%s" /></div>', $flag_uri);
As a note, you shouldn't be calling file_get_contents inside of an echo statement.