Echoing fixed number of images on a single row - php

How are these images being shown. Are they displaying it by calling the php echo on the set html table or do they just echo it using while loop. The data are taken from the database. and how do they make the 4th image a new row after 3 images are displayed. Beginner here. Thanks.

The Basic Principle is Keep track of the total number of pictures you add with a variable, say $reconi, and if the modulus (Remainder of Division by 3) is 0, then add a line break (In the example below I am showing Images pulled from a Directory, I already had the script handy, but you shall easily adapt it for doing from database as well). If not clear, feel free to get back.
$directory = "D:/ItemPics/";
$files = glob($directory . "*");
$reconi=1;
foreach($files as $file){
if($reconi % 3==0){
// Add picture
// Add a line break
}else{
// Add picture;
}
$reconi++
}

Related

Show small number of images in foldergalery

Im having trouble finding some piece of code for showing me the small number of how many images is in a folder in my website. Im using foldergallery.
I need to show the number above my folders, so one would know how many images are in a folder.
Can someone please point me there or write some kind of PHP code for this.
Thank you
R
You can use this:
$directory = "/path/to/directory/";
if(is_dir($directory)) {
$images = glob($directory . "{*.jpg,*.JPG,*.jpeg,*.png,*.PNG,*.gif,*.GIF}");
$numberOfImages = count($images);
}
After that, you just print the $numberOfImages where you want.

Pull Images & Thumbnails from a Folder Through PHP

I'm trying to generate images on my website by pulling a thumbnail from one folder and the actual image from another. I have no idea what I am doing. This is what I have so far:
<?php
$thumbdirname = "images/thumbs/";
$imgdirname = "images/";
$mainimages = glob($imgdirname."*.{jpg,png,gif}", GLOB_BRACE);
$imagethumbs = glob($thumbdirname."*.{jpg,png,gif}", GLOB_BRACE);
foreach($imagethumbs as $image) {
echo '<a class="imageLink" href="$mainimages" data-lightbox="logo" data-title="Vivid Logo"><img src="'.$image.'"</a> ';
}
?>
I know that it will not work with just "$mainimages" for href, but I haven't been able to figure out what to put there. As is, it will pull up the right thumbnail, but not link to the full associated image.
I tried to put another foreach statement in, but I get four results from my two pictures (and their thumbnails). Which makes sense sense it is showing one of each combination:
(img1,thumb1),(img1,thumb2),(img2,thumb1),(img2,thumb2)
How should I change this to get it playing nicely?
You don't seem to be outputting from $mainimages so I'm going to ignore this for now.
You PHP code seems to be OK from initial glance. Only thing that is a bit iffy is that you are not closing you image tag.
If that does not help try counting the image array/object.
If that returns 0 try the below code.
// Find all files in that folder
$files = glob('images/gallery1/*');
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
Hope that helps

How to increment a single number in a text file with PHP

I am writing a VoteBox (like/dislike box) in html and I cannot find how to do this anywhere.
Basically, there will be a like and a dislike button. When one of them gets clicked, it will forward the user to a page. And in that, I want some PHP code that will increase the number in upvotes.txt/downvotes.txt
I have tried doing it with a database but the problem is I want anyone to be able to use this without any setup.
i also want the front page to display the number of upvotes and the number of downvotes
so its kinda like this (most of this isn't real code BTW im new to PHP):
//this is the code for upvote.html
$upvotes = get_data_from_TXT_file
$changedupvotes = $upvotes + 1
set_data_in_txt_file_to_$changedupvotes
Sorry if i havent explained this very well
Any help appreciated
This is skeleton of the code that you can use:
$file = 'file.txt'; // your file name
// error handling etc to make sure file exists & readable
$fdata = file_get_contents ( $file ); // read file data
// parse $fdata if needed and extract number
$fdata = intval($fdata) + 1;
file_put_contents($file, $fdata); // write it back to file
Reference:
http://www.php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.file-put-contents.php
You can use file() to read the file into an array, and then increment the upvotes, and then write the data back using file_put_contents():
if (file_exists('upvotes.txt')) {
$content = file('upvotes.txt'); // reading all lines into array
$upvotes = intval($content[0]) + 1; // getting first line
file_put_contents('upvotes.txt', $upvotes); // writing data
} else {
// handle the error appropriately
}

How do create a global visit counter that echoes an image for every visit?

Basically I want to create a global visit counter of a web page, then I want to display an image for every visit. So if 10 people have visited the page, 10 random images from the server will be echoed on that page. When the 11th visitor arrives on the page a new random image will be echoed and so on...
So I need help with two things really -
I have a very basic visit counter but it only works in sessions, I need it to work globally?
How do I echo images according to the number of visits?
Here is the basic code I have for a visit counter:
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1
echo "Page views: ".$_SESSION['views'];
I am still a massive beginner at php and any help would be hugely appreciated :)
Thanks guys.
if you chose not to store your data in the database then this code should work well for you, all you have to do is find images from 0 to 9, and create empty text file named counter.txt and place this code in your php file somewhere
$file = "counter.txt";
$count = file_get_contents($file);
$fh = fopen($file, 'w') or die("ccould not open file");
$content = (int)$count + 1;
fwrite($fh, $content);
fclose($fh);
$counter_array = str_split($content);
foreach($counter_array as $digit){
echo "<img src='" . $digit . ".gif' />";
}
SESSIONS are NOT for global storage.
They are only alive till the website is open.
You should use database to store the global variable.
Now, coming to the image display part. You can name of images with number specific to no of visits.
For example:
4.jpg will be the Image that is when to the fourth guy
Next, you can use a simple snippet like this
$visitCounter = "?"; //Get the count using a logic
echo "<img src="$visitCounter.jpg" />"; //Use it to create a image path
$_SESSION is the wrong use for this.
Back in the day, I would read and write to a text file on the server. Not the best method due to flock issues, but it will give you some experience reading and writing a file.

Web, SQL: display random images despite file name, load images from folder

I want a web page to display several random images on load. I've thought of several solutions but I would really like to be able to dump images into a folder without renaming them, and the web page will choose from those images in the folder and display them
I can imagine a PHP solution that will display random images from a folder, but it will need to look for certain names.
So my next step was to have an SQL database where every image got a key, and 10 keys would be chosen random by a query - the images associated with them would then be passed into an array that the document will load the elements of.
But now I guess I need to know how to automatically populate an SQL database by having it read a folder?
Insight appreciated, if I don't have to reinvent the wheel the better
Glob works like on the filesystem, e.g. supports wildcards
$files = glob('/path/to/files/*.jpg');
$yourRandomFile = array_rand($files);
This will return a random JPG file, based on it's extension.
This should do it :
function your_dir ($directory)
{
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
$results[] = $file;
//or sql query for each file
$sql = mysql_query("SELECT * FROM your_table WHERE your_file = '".$file."'");
if(mysql_num_rows !== 0){
//your query...
}
//
}
}
closedir($handler);
return $results;
}
$directory = '/path/to/your/directory';
your_dir($directory);
Would probably be better to select existing files from db first put them into an array and exclude them rather than checking for each one.
You can do as below.
1. read complete dir from where you want the page to load the image
2. store the image names in array
3. now generate a number using random function from 1 to size of the array
4. consider the generated number as key for the array and use that image name to put the image.

Categories