I am trying to make it so that each time a user session is loaded a random image is displayed from the directory. Like an advert.
At the moment the image changes each page refresh, this doesn't really help me because as a user goes from page to page the constant refresh of images becomes annoying.
Here's what I have so far?
Please can anyone point out the piece of code I need to do what I need to do.
<?php
$path_to_images = "../PTB1/data/adverts/"; // path for images
$default_img = "test.png"; // default image, when error on page
function getRandomImage($path, $img) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $path . $img;
}
function getImagesList($path) {
$ctr = 0;
if(!isset($_SESSION['id']));
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// formati slik, ki jih prepozna
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}
?>
<div class=\"advert-box\" id=\"mod-advert\">
<img src="<?php echo getRandomImage($path_to_images, $default_img) ?>" height="190" width="180"alt="">
</div>
Did you try creating a session variable?
<?php
session_start();
// check if a image is already present
// no need to create again
if(!isset($_session['image'])){
$_session['image'] = getRandomImage($path_to_images, $default_img);
}
....
....
<div class=\"advert-box\" id=\"mod-advert\">
<img src="<?php echo $_session['image'] ?>" height="190" width="180"alt="">
</div>
first step, do the image selection when user LOGIN:
session_start();
if(!isset($_SESSION['image'])){
$_SESSION['image'] = getRandomImage($path_to_images, $default_img);
}
next, on every page, you just have to look at the $_SESSION['image'] variable:
<img src="<?php echo $_SESSION['image']; ?>" height="190" width="180"alt="">
Related
Problem:
So when I login, a user can go to the browse.php to add images to the favorites.php, and the photos will appear on that page.
When I log out and log back in, the images are gone (not saved).
Question:
How do I save it after I logout so on next login I get to see the favorite'd images from that particular user using cookies or other methods (can't use database because I'm not allowed to)?
My favorites page
<?php
session_start();
require_once 'favoritesfunction.php';
if (isset($_GET["fileName"])) {
$fileName = $_GET["fileName"];
addToFavorites($fileName);
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
$title = "Travel Photos - View Favorites";
?>
<link rel="stylesheet" href="viewfavorites.css" />
</head>
<body>
<main class="container">
<h1 class="favheader">Favorites</h1>
<div class="content">
<?php
if (isset($_SESSION['favorites']) && isset($_SESSION['email'])) {
$favorites = $_SESSION['favorites'];
if (count($favorites) > 0) {
?> <ul class="ul-favorite"> <?php
foreach ($favorites as $f) {
echo '<img class="displayPic" src="https://storage.googleapis.com/assignment1_web2/square150/' . $f . '">';
}
?>
</ul>
<p id="removeall"><button class="button"><span><a target="" href="services/removefavorites.php?remove=all">Remove All</a></span></button></p>
<?php
}
} else {
?>
<div class="nofavorites">
<p>No favorites found.</p>
</div>
<?php
}
?>
</div>
</main>
</body>
<script type="text/javascript" src="main.js"></script>
</html>
My add to favorites function php
<?php
function addToFavorites($fileName)
{
//Checks if the session favorites exists
if (isset($_SESSION['favorites'])) {
$favorites = $_SESSION['favorites'];
} else {
$favorites = array();
$_SESSION['favorites'] = $favorites;
}
// add item to favourites
$item = $fileName;
$match = false;
//Loop below checks for duplicates
$i = 0;
while ($i < count($favorites)) {
if ($favorites[$i] == $item) {
$favorites[$i] = $item;
$match = true;
}
$i++;
}
//if match equals false, that means its not in the favorites list of the user
//so it is added to the user's favorites array
if ($match == false) {
$favorites[] = $item;
}
$_SESSION['favorites'] = $favorites;
$_SESSION['favorites'] = $favorites;
}
My approach:
I tried doing something like setting $name = $_SESSION['email'] and $value="<img src=...>" then setcookie($name, $value) and then if(isset($_COOKIE[$value])) echo $value
I know this is totally wrong, I tried looking everywhere online to try to solve this, if I could get some help that would be great thanks!
I am trying to display the images of my Houses from the folder but it shows the error like this
http://localhost/jageerx/assets/images/House/assets/images/House28278954_957081961107785_391331158313648576_n.jpg
404 (Not Found)
My code is below
<?php
$PropertyType = "";
$image = "";
if($data != null)
{
foreach($data as $key=>$value)
{
$PropertyType = $value['PropertyType'];
$image=$value['HouseImage1'];
}
}
?>
the front-end code is this
<img src="<?php echo base_url('assets/images/House/'. $image);?>" alt="tab1" class="img img-responsive">
folders hierarchy is mentioned below
this is the model function
public function SIngleHouseADD($houseID)
{
$this->db->select('*');
$this->db->from('housedetail');
$this->db->where('HouseID', $houseID);
//$qry = $qry->result_array();
$query=$this->db->get();
$resultArray = $query->result_array();
return $resultArray;
//return $qry;
}
this one is the controller
public function SingleProperty()
{
//$HouseID = $_GET['id'];
$this->load->model('SingleAddModel');
$plots = $this->SingleAddModel->SIngleHouseADD($_GET['id']);
$data = array();
$data["data"] = $plots;
$this->load->view('SinglePropertyDetail_view', $data);
}
In base_url() you are passing assets/images/House/ the same path in $image, so it is duplicating path, I'll suggest you to first echo the results then use them in html. And for your Image you can use:
<img src="<?php echo base_url().$image;?>" alt="tab1" class="img img-responsive">
OR
<img src="assets/images/House/<?php echo $image;?>" alt="tab1" class="img img-responsive">
Hope this will help you :
Since your $image variable already contains assets/images/House so should not be use in base_url() again
Should be like this :
foreach($data as $key=>$value) {
$PropertyType = $value['PropertyType'];
$image=$value['HouseImage1'];
?>
<img src="<?php echo base_url().$image;?>" alt="tab1" class="img img-responsive">
<?php }?>
I don't know if this works best for you but when I add images or css in codeigniter I usually add them in the same level as my index.php so it goes like this!
See screenshot.
screenshot
I got a script that gets images from a folder and puts them inside a lightgallery. All is working fine, except when I add more than 7 images (or any new images for that matter). There are 7 images in the folder when I add 8.jpg it shows nothing, not in the gallery and not in the source code. What could be the cause of this?
My script:
<div id="lightgallery">
<a href="<? echo $imagesmall; ?>">
<img class="fullnieuwsimg" src="<? echo $imagesmall; ?>">
</a>
<?
function scan_dir($dir) {
$ignored = array('.', '..', '.svn', '.htaccess','index.html');
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}
ksort($files,SORT_NATURAL);
$files = array_keys($files);
return ($files) ? $files : false;
}
if(is_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') != FALSE){
foreach(scan_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') as $entry) {
$gallery .= '
<a href="/_intern/SNM/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'">
<img class="galleryimgs" title="'.$contentcr[0]['alias'].'" alt="'.$contentcr[0]['alias'].'" src="/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'" />
</a>';
}
}else{
echo '';
}
echo $gallery;
?>
</div>
The pictures are in the folder like this (barebone joomla cms) :
All work, except 8.jpg or any other new images I add.
I want to make a product gallery like 5 products, each has their own background-image attribute
I use loop to insert the product image, so I want to make it each loop will have different background-image
I'm thinking of using IF statement like so
<?php
$bg = 0;
$bg1 = "url('img1.jpg')";
$bg2 = "url('img2.jpg')";
$bg3 = "url('img3.jpg')";
$bg4 = "url('img4.jpg')";
$bg5 = "url('img5.jpg')";
if ($bg = 0){
echo " <div style='background-image :$bg1 ;'>" ;
$bg = 1;
} else if ($bg= 1) {
echo " <div style='background-image :$bg2 ;'>" ;
$bg = 2;
} else if ($bg= 2 ) {
echo " <div style='background-image :$bg3 ;'>" ;
$bg = 3;
} else if ($bg= 3 ) {
echo " <div style='background-image :$bg4 ;'>" ;
$bg = 4;
} else if ($bg= 4 ) {
echo " <div style='background-image :$bg5 ;'>" ;
$bg = 0;
}
echo " </div> " ;
code for product images
?>
above is the simplified code I wrote, it doesn't work.
if anyone has a different but much simpler solution it will be appreciated
note : the image files are in the same directory with this php file
thank you
Would you be open to using img tags? This, in my opinion, would be a better solution:
Code:
<?php
$images=Array(
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg"
);
//
print "\n<br> Code: \n<pre>\n".RenderThoseImages($images)."\n</pre>";
//
function RenderThoseImages($images)
{
//
$s="";
//
foreach($images as $image){
$s.="\n<div><img src=\"{$image}\"></div>";
}
return $s;
}
?>
Outputs:
<br> Code:
<pre>
<div><img src="img1.jpg"></div>
<div><img src="img2.jpg"></div>
<div><img src="img3.jpg"></div>
<div><img src="img4.jpg"></div>
<div><img src="img5.jpg"></div>
</pre>
The main reason being that when you use the background-image CSS, you're also responsible for grabbing the image dimensions in PHP and rendering height/width into the div CSS as well, or possibly creating some javascript to fix it after loading, creating unneeded headache.
Here is a snippet of my code
<?php
$me = $facebook->api('/me/friends');
foreach( $me['data'] as $frns ) {
?>
<img src="https://graph.facebook.com/"<?php echo $frns['id'] ?>"/picture"
title="<?php echo $frns['name'] ?>"/>
<?php
}
I want to merge all of the images using something like
$im = mergeImages( array( image1, image2, etc ) );
How can I limit the merge to only the first 196 or a random number of images?
Change your code to this. You won't have to make another series of API calls to get the urls for the pictures, and your script won't have to deal with the 301 redirects Facebook typically throws for an image.
$me = $facebook->api('/me/friends?fields=name,picture');
echo "<br />Total friends".sizeof($me['data'])."<br />";
echo "<br /> Friends collage<br /><br />";
$frns_images = array();
$i = 1;
foreach($me['data'] as $frns)
{
if ($i >= 100) break;
$img = $frns['picture']['data']['url']; //Double check this.
if ('jpg' === substr($img, -3)) {
printf ('<img src="%s" title="%s" />', $img, $frns['name']);
$frns_images[] = $img;
$i++;
}
}
$im = mergeImages($frns_images);