Create new line for each seperated element in field (database) - php

Here's my code :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2'].'" >
</div>
In that "img2" row in the database, it's image path. But I need to detect if there's more than one image path seperated by a comma in there.
So if there's 2 images in the field (ex.: img/image1.jpg, img/image2.jpg), well the HTML would look like :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2(first value)'].'" >
<img src="'.$row['img2(second value)'].'" >
</div>
But if there's only 1 value, it stays like my first code part.
Anyone? Thanks a lot

use the explode function and then a foreach loop
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
?>
<img src="<?php echo $image; ?>" >
<?php } ?>
</div>

Related

Change part of URL string for images with PHP

In my MySQL database, the image links are saved in the format like http://www.old.com/image1.jpg. But I had to change the domains of the images and the new links appear like images.new.com/image1.jpg. I have been changing the images links with jQuery with the following function:
$(document).ready(function() {
$('img').each(function() {
var src = $(this).attr('src');;
$(this).attr('src', src.replace('www.old', 'images.new'));
});
});
But I am wondering is there any way to change part of the URL strings with PHP. I am getting the image URLs with the following PHP function.
<?php
$imageQuery = $mysqli->query("SELECT imageURL FROM images WHERE album = 'UK' ORDER BY date ASC");
$images = $imageQuery->fetch_all(MYSQLI_ASSOC);
$images = array_chunk($images, 2);
?>
<div class="row">
<div class="col-4" id="box1">
<?php foreach (array_column($images, 0) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
<div class="col-4" id="box2">
<?php foreach (array_column($images, 1) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
</div>
With PHP how can I echo the new links for the images directly here in the img src? <img class="img-fluid" src="<?= $image['imageURL']; \\modified link here ?>">
PHP has it's own method for replacing strings, str_replace. The equivalent to your jQuery in PHP is:
str_replace('www.old', 'images.new', $image['imageURL'])
A better idea would be to update the values in your database.
An even better idea would be to not store the duplicate root URLs anywhere, and stitch them together in the application if you need absolute URLs, for some reason.
You could alternatively convert the text in the sql select statement:
SELECT REPLACE(imageURL, 'www.old', 'images.new') AS imageURL FROM ...
By using AS imageURL none of the rest of the code would need to change.

bootstrap image row out of place

[SOLVED]
I'm have written a horizontal row of 4 images 500x300 using placeholders, this works perfectly fine, but when I try to replace those images with images from a folder (using php) of the same dimensions, the first 2 image containes are knocked out of place while the last 2 are where they belong.. I've attached a screenshot and the code below is what I'm using to get the row of images.
<div class="row" style="margin-top:1px">
<?php
$directory = 'images';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
?>
<div class="col-sm-3 col-xs-6" >
<a href="#" >
<img class="img-responsive portfolio-item" src="<?php echo "images/$file"; ?>" alt="">
</a>
</div>
<?php
}
?>
</div>
this is what i get in the browser
Using glob instead of an array fixed the problem. PHP is including the . and double dot of the directory structure and therefore creating 2 empty images.

php issue in using explode

I have this code :
http://jsfiddle.net/S4rD9/
(i know the preview is messed up, it's just to show you the PHP code part I have)
Basicly, this is connected to my databse, and it takes the informations inside the text fields of each columns.
As you can see in the code, elements are fetched like this :
$row['img2']
Means it displays the information in the text field of the "img2" column.
The "img2" column has image path in it.
The part of the code that I'm having trouble with looks like this :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2'].'" >
</div>
And I need it to use the "explode" function (I think that's the one I need to use), to get each values in that img2 column textbox in the database, seperated by a comma.
Right now in the database, the text field of the first row (for example) of the img2 column is :
img/img-araignee-big.jpg, img/img-bug-big.jpg
But it only gets the "img/img-araignee-big.jpg" part.
Someone sent me this :
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
?>
<img src="<?php echo $image; ?>" >
<?php } ?>
</div>
He said it should work, but I cannot get this to work correctly.
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
print '<img src="' . $image . '">';
} ?>
</div>

get two paths of images from database and loop them to display in slider

Im little bit stuck on a logic.scenario is i have a slider where two images are shown at a time taking 50%-50% part of screen,here is the simple html of it,
<div class="zeus-slider zeus-default" >
<div class="zeus-block">
<div class="zeus-slide s-50"> <img src="slider/slider1.jpg" data-effect="slideRight" alt="" /> </div>
<div class="zeus-slide s-50"> <img src="slider/slider2.jpg" data-effect="slideRight" alt="" /> </div>
</div>
</div>
here every block contains two images i have shown only one.now i have trying to make it dynamic, but i didnt get any idea how it will work, so i created database with two tables, each for individual image in a single block.Both table have fields like simg_id(A_I),img_name,simg_path.
here its my php on slider page.
<div class="zeus-block">
<?php
$slider_slt=getResultSet('select * from slider_img_master1 ORDER BY simg_id');
if(mysql_num_rows($slider_slt))
{
while($slider_data=mysql_fetch_assoc($slider_slt))
{
?>
<div class="zeus-slide s-50"> <img src="slider/first/<?php echo $slider_data['simg_path']?>" data-effect="slideRight" alt="" /> </div>
<?php }
} ?>
<?php
$slider_slt2=getResultSet('select * from slider_img_master2 ORDER BY simg_id');
if(mysql_num_rows($slider_slt2))
{
while($slider_data2=mysql_fetch_assoc($slider_slt2))
{
?>
<div class="zeus-slide s-50"> <img src="slider/second/<?php echo $slider_data2['simg_path']?>" data-effect="slideRight" alt="" /> </div>
<?php }
} ?>
</div>
now the problem is when i try to fetch path of image in slider, images are not changing one by one on both half of screen.instead it showing like both images from from both table top, another two images from both tables below 1st both, and so on , so full page is covered with images.
I know this idea of creating two tables for getting two images at once is silly,but i could not think any better.if any one can suggest any better way, it would be so helpful.
Update:getResultSet is function for mysql_query.
if anybody interested i found an answer for above question.
<div id="slide1" >
<div class="zeus-slider zeus-default" >
<?php
$slider_str="select *from slider_img_master1 where simg_status='Active'";
$i=1;
$result=mysql_query($slider_str);
if(mysql_num_rows($result)>0)
{
echo '<div class="zeus-block">';
while($row=mysql_fetch_assoc($result))
{
if($i%2==1 && $i!=1)
{
echo '<div class="zeus-block">';
}
?>
<div class="zeus-slide s-50"> <img src="slider/<?php echo $row['simg_path'];?>" data-effect="slideRight" alt="" /> </div>
<?php
if($i%2==0)
{
echo '</div>';
}
$i++;
}
} ?>
</div>
<div class="clear"> </div>
<div class="next-block"> </div>
<div class="prev-block"> </div>
</div>

Condition put on images

I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>
There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.

Categories