php issue in using explode - php

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>

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.

Problem displaying images saved as blob images

am having an issue displaying images stored as blob in my database, as the images doesn't display rather is shows crazy characters and symbols, i tried to change the tags for the php but with no avail. Help and here is my code: Note that i want all types of images to be saved as blob (jpg, jpeg, gif, svg, png) - thanks in advance
<?php
include 'include/connect.php';
$sql = "SELECT * FROM room_details ORDER BY id ASC LIMIT 2, 1;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,'.$row['image'].'">'; ?>
<div class="mask">
<div class="main">
<h5><?= $row['room_type']; ?></h5>
<div class="price"><?= $row['room_name']; ?><span>a night</span></div>
</div>
Read More
</div>
</div>
} ?>
Since the blob url is decoded with base64, you need to encode it first. You can use either JavaScript or PHP to do this.
JavaScript:
With JavaScript, you can just use the atob() function to encode your base64 url like this:
<script>
var x = <?php echo $row['image'] ?>
document.getELementById("code").innerHTML = atob(x);
</script>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' ?><span id="code"></span><?php echo '">'; ?>
<!-- DIV CONTENT HERE -->
</div>
However, as stated in this other SO thread, it would be a better approach if you use PHP itself or other JavaScript approaches (mentioned in that same thread) to retrieve and encode the base64 url as the above approach is not secure.
PHP:
With PHP, you can either encode the base64 url before outputting it into your HTML like this:
<?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' . base64_encode(.$row['image']).'">'; ?>
Or you can create a new php file, encode the PHP in that file and then echo the encoded base64 url to your HTML like this:
<!-- PHP file -->
<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = base64_encode(getImageFromDatabase($id)); // your code to fetch the image
header('Content-Type: image/jpeg');
echo $image;
?>
<!-- HTML -->
<img src="image.php?id=<?php echo $image_id; ?>" />
Check out the accepted answer in this other SO thread for a more in-depth explanation of the above two PHP approaches.

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.

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

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>

Displaying BLOB files in PHP script

This is my PHP script which displays a radio station's schedule:
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="$image" width=115 height=60>
<div class="time">$airtime</div>
<div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
</div>
The values with the dollar sign represent the field names in my database, which is radiopresenters.
How would I get this to work as a PHP script, and display the values from the database?
All values in the fields are stored in TEXT format, apart from the image field which is stored in BLOB format.
Airtime is stored in a separate database entitled as radioschedule which has all 4 fields ib, and I intend to link these together via some relational means.
What's the best way to get it to display as the above, especially the BLOB part?
What you want to do is set up an show_image.php script, Example
show_image.php
<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;
if($id)
{
if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
{
$data = mysql_fetch_assoc($res);
header("Content-type: image/jpg"); //Send the content Type here.
print $data['blob_data'];
exit;
}
}
?>
Then within your html files you would do the follwing.
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>
<div class="time">$airtime</div>
<div class="show">
<h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
That's roughly how its done, another pointer is when your selecting your data via your main page, you should not select the blob data as it will slow your application down, and show_image.php may require more work as its for example purposes only
Peace.
You could write the image stored as a blob in the database out to a file. You could then use a url as your image source.
Assuming $presenter doesn't have any file/url reserved characters in it, and $image is stored as an accurate binary jpg (or png or gif etc.) , you could do:
<?php
if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
fwrite($fh, $image) ;
fclose($fh) ;
}
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">
I would suggest that you work out some way of caching the file, so it doesn't have to be written out for every page load though.
Are you asking how to put those values into your file?
As for the text ones you could just use
<?=$airtime?>
or if your server setup doesn't support short tags just use
<?php echo $airtime?>
I would just do this inline with your php.
Example:
<div class="time"><?=$airtime?></div>
As for the BLOB, I would advise not doing this and just storing the image on your server and store the image file's name in the db. BUT if you are intent on doing this I think the only way you can do this is by having a separate script that returns an image file. Something like this:
<?php
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>
Then in your other file you would do something like:
<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>

Categories