Unable to display images from a comma separated string - php

I'm trying to display images from database but unable to show .
Please help me.
My code
$id = $_GET['reg_id'];
$sql13 = "select * from contacts where reg_id=" . $id;
$result13 = mysqli_query($conn, $sql13);
if (mysqli_num_rows($result13) > 0) {
while($documents = mysqli_fetch_array($result13))
{ ?>
<li class="make_text1" style="font-size:16px">
<span class="definition"><b>
<?php if(!empty($documents["name"])){ echo
$documents["name"]; }?><br><?php
if(!empty($documents["image"])){
$upload_dir = 'uploads/';
// echo "<img src='uploads/".$documents["image"]."' width='800' height='500'> ";
echo "<img src='uploads/".$documents["reg_id"]."/".$documents["image"]."' width='800' height='500'> ";
}

You need to loop over the values in $documents["image"]. Try this:
$images = explode(',', $documents["image"]);
foreach ($images as $image) {
echo "<img src='uploads/".$documents["reg_id"]."/$image' width='800' height='500'> ";
}
or if you only want do display one of them e.g. the first, something like this:
$images = explode(',', $documents["image"]);
echo "<img src='uploads/".$documents["reg_id"]."/".$images[0]."' width='800' height='500'> ";

Related

Have thumbnails of remaining videos showing underneath playing video

What I am trying to do is something like below:
The buttons at the side can be ignored. So the idea is that the big square is the main video playing and the little ones underneath will be the thumbnails of the remaining videos. The main video playing is randomly picked when the site is loaded and then the ones that are not playing, the thumbnails will be shown underneath. I am doing this like so:
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
echo $video;
array_push($items, $video);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $items[0];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
foreach($restVideos as $videoThumbnail) {
try {
$stmt = $dbconn->query("SELECT thumbnail, videoTitle FROM videos WHERE video = '$videoThumbnail'");
while($row = $stmt->fetch()){
$thumbnail = $row['thumbnail'];
array_push($thumbnails, $thumbnail);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
foreach($thumbnails as $nonPlaying) {
?>
<img src=<?php echo $nonPlaying ?> id="thumbnails" width="200" height="100">
<?php } ?>
what is happening at the moment is below:
Each image is just stacking up against each other how can I change it so this is not the case?
Also is there a more efficient way of doing this than what I am currently doing?
Edit:
$thumbnails = array();
$videoTitles = array();
$test = 700;
echo "<div>";
foreach($restVideos as $rVideo) {
$test = 200 + $test;
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100" style= "left: <?php echo $test?>px">
<?php
echo "</div>";
}
You not required to two query two times to db .
Use key value and store the entire video. While printing thumbnail based on the key (unique id ex. videoid). You can retrieve the content when you print the thumbnail list and can avoid querying again in a loop.
Ref to code snipp
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoMap[$row['videoid']] = $row;
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
if (count($items) == 0) echo $video;
array_push($items, $row['videoid']);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $videoMap[$items[0]]['video'];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
echo "<div>";
foreach($restVideos as $rVideo) {
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100">
<?php
}
echo "</div>";
}
?>
Use a div tag and print the image thumbnails.

Inserting fb access token to this code

I had this script from google it was working perfectly, the code was able to retrieve any facebook album from any page
suddenly it stopped working
I did a research on google I found that it needs an access token implementation in the code
but im not able to do it alone any help please
regards
$fb_page_id = "448955931844556";
$json_link = "http://graph.facebook.com/{$fb_page_id}/albums?fields=id,name,description,link,cover_photo,count";
$json = file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
$album_count = count($obj['data']);
for($x=0; $x<$album_count; $x++){
$id = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = $obj['data'][$x]['description'];
$link = $obj['data'][$x]['link'];
$cover_photo = $obj['data'][$x]['cover_photo'];
$count = $obj['data'][$x]['count'];
// if you want to exclude an album, just add the name on the if statement
if(
$name!="Profile Pictures" &&
$name!="Cover Photos" &&
$name!="Timeline Photos"
){
$show_pictures_link = "photos.php?album_id={$id}&album_name={$name}";
echo "<div class='col-md-4'>";
echo "<a href='{$show_pictures_link}'>";
echo "<img class='img-responsive' style='height:190px;width:190px;' src='https://graph.facebook.com/{$cover_photo}/picture' alt=''>";
echo "</a>";
echo "<h3>";
echo "<a href='{$show_pictures_link}'>{$name}</a>";
echo "</h3>";
$count_text="Photo";
if($count>1){ $count_text="Photos"; }
echo "<p>";
echo "<div style='color:#888;'>{$count} {$count_text} / <a href='{$link}' target='_blank'>View on Facebook</a></div>";
$description;
echo "</p>";
echo "</div>";
}
}
Add access_token=<your access token> to $json_link
for example:
$fb_access_token = "PUT YOUR ACCESS TOKEN HERE"
$json_link = "http://graph.facebook.com/{$fb_page_id}/albums?access_token={$fb_access_token}&fields=id,name,description,link,cover_photo,count";

How can I add an image header to my php mail form?

So I have a standardized mail form that has certain generated details that must accompany it including a header image.
I am trying to find out how to add the image to the text form for when I send it.
I have
$img_path = 'image12.jpg'; //declaration
while($row = mysql_fetch_array($result))
{
echo "<tr>";
$emailContent=str_replace("\\r\\n","",$row["generic_email_text"]);
echo 'Test';
echo '<td width="450"><textarea name="genericemail">'.$img_path .$emailContent.'</textarea></td>';
echo "</tr>";
}
echo "</tr>";
echo "</table><p>";
I tried adding img_path to it but it won't work. I'm taking over from someone elses code which I always find difficult. All I get with this is the name of the img ("image12")
And yes I havnt done PHP in a while
Here's the PHP for pulling the form info
$fullName = $firstName . " " . $lastName;
$imageFile = str_replace = '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />'; //Header here is just a place holder
$updatedEmailText = str_replace("%name%", fieldHtmlFormat($firstName), $updatedEmailText);
$updatedEmailText = str_replace("%firstname%", fieldHtmlFormat($firstName), $updatedEmailText);
$updatedEmailText = str_replace("%lastname%", fieldHtmlFormat($lastName), $updatedEmailText);
$updatedEmailText = str_replace("%duration%", fieldHtmlFormat($durationOfStay), $updatedEmailText);
$updatedEmailText = str_replace("%destination%", fieldHtmlFormat($arrivalCity), $updatedEmailText);
$updatedEmailText = str_replace("%travel%", fieldHtmlFormat($travel), $updatedEmailText);
return $updatedEmailText; .
edit:
changed code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
$emailContent=str_replace("\\r\\n","",$row["generic_email_text"]);
echo 'Test';
//LINE 50'<td width="450"><textarea name="genericemail"><imgsrc="'.$img_path.'"/>'.$emailContent.'</textarea></td>' }//LINE50
echo "</tr>";
echo "</table><p>";
You need to add <img> tag in order to show the photo.
Replace the line:
echo '<td width="450"><textarea name="genericemail">'.$img_path .$emailContent.'</textarea></td>';
With this line:
echo '<td width="450"><textarea name="genericemail"><img src="'.$img_path.'"/>'.$emailContent.'</textarea></td>';
And also make sure that you have the correct file/url path for $img_path.

Show Array of Images

I'm trying to display image in 5 by 3 table.
I'm able to display the images if all empty(blank.png).
Here is the code
<?PHP
$ds ='\image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
echo "<pre>"; print_r($imagefile);
$file = 'blank.png';
$d = $ds.$file;
echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
for($row=1;$row<=5;$row++){
echo "<tr>";
for($col=1;$col<=3;$col++){
// echo"<td height=60px>W$row</td>";
//if()
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
echo "</tr>";
}
echo "</table>";
?>
I want to display the images base on middle file name array $imagefile eg W1, W2 and if not in array, I will display the blank.png.
I was able to get the middle file name by this code, but I cannot display the images in correct row/col.
for($i=0;$i<count($imagefile); $i++) {
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
}
Can you try this,
Based on your code add these when you echo the image,
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
if($wd == *the increment either row or col*)
{
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
else
{
echo"<td height=60px>No image</td>" .PHP_EOL;
}
See if it works.
Here it is
<?php
$ds ='/image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
//echo "<pre>"; print_r($imagefile);
$default = 'blank.png';
?>
<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">
<?php
for($row=1;$row<=5;$row++){
?>
<tr>
<?php
for($col=1;$col<=3;$col++){
// construct the file name
$filename = 'EX_W' . $row . '_0' . $col . '.png';
// set the default image file
$imgPath = $ds . '/' . $default;
// in case the file name exists in your array with images,
// set the correct path to the image
if (in_array($filename, $imagefile)) {
$imgPath = $ds . '/' . $filename;
}
?>
<td height=60px>
<img border="1" height="120" width="120" src="<?php echo $imgPath; ?>"/>
</td>
<?php } ?>
</tr>
<?php
}
?>
</table>
As you can see I also prefer to "embed" the php code in the HTML. It makes no sense to me outputting HTML code through the PHP engine if it can be parsed as is ;-)

PHP Facebook FQL Multquery and dealing with foreach and variables

Alright, so I am trying to figure this out. Basically I have everything working well except for adding the cover photos to each of the DIVS. Basically I have 2 queries using FQL, and here is my code.
$query =
'{"query1":"SELECT aid, cover_pid, name, description FROM album WHERE owner=$fbowner",
"query2":"SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #query1)"}';
$fqlResult = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => $query));
$fql = $fqlResult[0]['fql_result_set'];
$covers = $fqlResult[1]['fql_result_set'];
$the_count = count($fql);
$i = 0;
foreach($fql as $value) {
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
Now that works fine, but what if I put this in the mix, the cover photos are all off unless I make $i = 1.
$i = 0;
foreach($fql as $value) {
if($value['name'] != 'Wall Photos'){
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
}
and if I stick another foreach in the mix, it grabs all the cover photos and sticks them into each div. So what is the easiest solution to just go in order and grab the cover photo associated with the album node?
Thanks in advance!
I tried this, too. You can't rely on the indexes being consistent between these two queries.
To make it work, first modify query2 to also return pid, then replace this line in your code:
echo "<img src='{$covers[$i]['src']}' border='1'>";
with this:
foreach($covers as $cover) {
if ($value['cover_pid'] === $cover['pid']) {
echo "<img src='{$cover['src']}' border='1'>";
break;
}
}

Categories