I have a php script that runs glob on a directory, and it returns all the images it finds in the directory, with an ad from google before all the images.
I would like it if I could use glob to load 10 of the images, then insert the javascript from googles ad services, and continue loading the images. So an ad every 10 images. Every attempt by me so far has failed spectacularly, and any help would be greatly appreciated!
Below is my PHP code
<?php
$manualwidth = $_GET['manwidth'];
$manualdir = $_GET['mandir'];
$manualmodel = $_GET['manurl'];
$manualurl = $manualdir . '/' . $manualmodel . '/';
$files = glob($manualurl .'{*.jpg,*.gif}', GLOB_BRACE);
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" width="'.$manualwidth.'"><br>'." ";
}
?>
Echo the js every 10th iteration
<?php
$count = count($files);
for ($i=0; $i<$count; $i++)
{
if($i % 10 === 0) {
echo "google ads here";
}
$num = $files[$i];
echo '<img src="'.$num.'" width="'.$manualwidth.'"><br>'." ";
}
?>
How about putting the ad code into an html file in the directory:
ad.html
<script type="text/javascript>
//Code to run
</script>
//other ad stuff here
And then just include that file after every 10 images:
index.php
<?php
for($i = 1; $i <= count($files); $i++)
{
//Echo image code here
if($i%10 == 0)
include("ad.html");
}
Related
I want to include a link after the picture, to copy that picture into an other catalog.
The code scans a catalog for pictures and displays them on a page, newest on top.
$folder = $cam.'/grabs/';
$filetype = '*.*';
$files = glob($folder.$filetype);
if ($sort == 'Nye') {
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
}
$count = count($files);
echo '</font><table>';
echo "<font color='white'> $count bilder";
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo ' - Save' ;
echo '</td></tr>';
}
echo '</table>';
?>
My problem is that i do not know how to get the filname instead of $i in movefile='.$i.
I have tried to put in $files but that gives me an error message.
Learned a lot today.
The glob( will take every picture and give it a number $i
The original .$i. Returns a line number. (As stated in one of the comments, thanks for the hint.)
So, to get the filename, you have to add $files before the $i to get the right one.
The answer was:
.$files[$i].
echo ' - Save' ;
I'm trying to build a site that has a photo gallery and rather than build a database CMS I'm trying it with the use of PHP and folders. At the moment I have a script to get all of the images in a folder and display them on a page, however as there are going to be probably in excess of 100 photo's I'd like to use pagination to spllit this over several pages but I have no idea how to do this.
Here is the script I'm currently running:
<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';
$filename = HOW DO I GET THE NAME WITHOUT FILE TYPE
$files = glob($folder.$filetype);
foreach ($files as $file)
{
echo '
<div class="galleryCellHolder">
<div class="galleryCell">
<a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
</div>
</div>
';
}
?>
Q1 - How do I extract the file name without the file extension?
Q2 - How do I paginate this for say 24 images per page?
For paging you must calculate the total items to page , capture the parameter of the current page and iterate over the respective range.
<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$total = count($files);
$per_page = 6;
$last_page = (int)($total / $per_page);
if(isset($_GET["page"]) && ($_GET["page"] <=$last_page) && ($_GET["page"] > 0) ){
$page = $_GET["page"];
$offset = ($per_page + 1)*($page - 1);
}else{
echo "Page out of range showing results for page one";
$page=1;
$offset=0;
}
$max = $offset + $per_page;
if($max>$total){
$max = $total;
}
You can use the function pathinfo to get the file name without extension.
//print_r($files);
echo "Processsing page : $page offset: $offset max: $max total: $total last_page: $last_page";
show_pagination($page, $last_page);
for($i = $offset; $i< $max; $i++){
$file = $files[$i];
$path_parts = pathinfo($file);
$filename = $path_parts['filename'];
echo '
<div class="galleryCellHolder">
<div class="galleryCell">
<a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
</div>
</div>
';
}
show_pagination($page, $last_page);
Using the following function you can create the navigation links
function show_pagination($current_page, $last_page){
echo '<div>';
if( $current_page > 1 ){
echo ' <<Previous ';
}
if( $current_page < $last_page ){
echo ' Next>> ';
}
echo '</div>';
}
?>
I am using the ProgressBar class from the following link for displaying a progress bar for image downloads..
http://pastebin.com/KSxjC01r
I am using the following code..
echo 'Starting Image Download...<br />';
$p = new ProgressBar();
echo '<div style="width: 300px;">';
$p->render();
echo '</div>';
//progress bar
for ($i = 0; $i < ($size = 100); $i++) {
$p->setProgressBarProgress($i*100/$size);
usleep(1000000*0.01);
}
fetch_image("$item", "../cbimages/$img ");
echo "Downloaded $img <br />";
}
echo "Finished downloading images....";
Everything is working fine..But for each download it is displaying only 99.0% in progress bar. After completion too it displays as 99.0% . What is wrong the code above. Where am i going wrong. Help requested..
Update:
Resolved the issue by changing the following line in the class file:
From:
if ($percentDone == 100) {
print('document.getElementById("'.$this->pbid.'").style.display = "none";');
}
To:
if ($percentDone == 100) {
print('document.getElementById("'.$this->pbid.'").style.width = "'.$percentDone.'%";');
Your for loop currently only counts to 99 because you used < instead of <=.
Changing it to this should work:
<?php
for ($i = 0; $i <= ($size = 100); $i++) {
$p->setProgressBarProgress($i * 100 / $size);
usleep(1000000 * 0.01);
}
Your loop isn't quite right
you have this:
for ($i = 0; $i < ($size = 100); $i++) {
The rule is that i < 100 so, when i == 100 the loop is not run. Meaning it is never passed to setProgressBarProgress()
This will work:
for ($i = 0; $i <= ($size = 100); $i++) {
The loop will still run when i == 100
Working on implementing image descriptions to a php run gallery and can't seem to figure out how to call each text file for each individual image. I would need to place a div in the code for the image and then call the include.
//total number of images
$total = 77;
//max number of thumbnails per page
$max = 9;
//what image do we want to start from?
$startcount = $_GET["start"];
//if there is not a defined starting image, we start with the first
if(empty($startcount))
{
$startcount = 0;
}
//start off the loop at 1
$loop = 1;
//start the loop
while($loop <= $max)
{
//for the picture labels
$num = $startcount + $loop;
if($num > $total)
{
$num = $num - 1;
break;
}
// Add class="last" to every third list item
if(is_int($num / 3))
{
$last = ' class="last"';
}
else
{
$last = "";
}
//the code for the image
echo '
<li'.$last.'><img src="images/portfolio/thumbs/pic-'.$num.'-thumb.jpg" width="256" height="138" alt="Thumbnail of image '.$num.'" /><div>'.$num.'</div></li>';
I see that I can call the text files by number using '.$num.' (I have 77 individual text files with a phrase in each) but how do I tell it to call the files?
Assuming the description files are named something like "description_$num.txt", you can simply use readfile.
echo "<li${last}><a href='images/portfolio/pic-${num}.jpg' rel='milkbox[gall1]'>
<img src='images/portfolio/thumbs/pic-${num}-thumb.jpg' width='256'
height='138' alt='Thumbnail of image ${num}'/>
</a><div>";
readfile("description_${num}.txt");
echo '</div></li>';
Note that you don't "call" files in PHP, you either include them (which interprets them as scripts) or read them (which dumps them to the output).
For each file you need to do something like that:
<?php
$f=fopen($file,'r');
$data='';
while(!feof($f))
$data.=fread($f,$size);
fclose($f);
// do whatever you want with the file content.
?>
I made use of file_get_contents, and split the output a bit so you can more easily modify it:
<?php
$total = 77;
$max = 9;
$startcount = $_GET["start"];
$loop = 1;
if(empty($startcount)) $startcount = 0;
while($loop <= $max)
{
$num = $startcount + $loop;
if($num > $total)
{
$num--;
break;
}
$last = ($num % 3 == 0 ? ' class="last"' : '');
$output = "<li $last>";
$output .= "<a href=\"images/portfolio/pic-$num.jpg\" rel=\"milkbox[gall1]\">";
$output .= "<img src=\"images/portfolio/thumbs/pic-$num-thumb.jpg\" width=\"256\" height=\"138\" alt=\"Thumbnail of image $num\">";
$output .= "</a>";
$output .= "<div>";
$output .= file_get_contents("textfile-" . $num . ".txt"); // assuming this is where you want the phrase
$output .= "</div>";
$output .= "</li>";
echo $output;
}
?>
I am trying add a descriptive tag under each image but most of the code is in PHP and i am not to familiar with how to program it without screwing up the whole structure. Here is the website: http://suncoastdeck.com/index.php?page=portfolio&start=0
Here is the code for the portfolio page:
<div class="content-box">
<?
//total number of images
$total = 77;
//max number of thumbnails per page
$max = 9;
//what image do we want to start from?
$startcount = $_GET["start"];
//if there is not a defined starting image, we start with the first
if(empty($startcount))
{
$startcount = 0;
}
//start off the loop at 1
$loop = 1;
//start the loop
while($loop <= $max)
{
//for the picture labels
$num = $startcount + $loop;
if($num > $total)
{
$num = $num - 1;
break;
}
// Add class="last" to every third list item
if(is_int($num / 3))
{
$last = ' class="last"';
}
else
{
$last = "";
}
//the code for the image
echo '
<li'.$last.'><img src="images/portfolio/thumbs/pic-'.$num.'-thumb.jpg" width="256" height="138" alt="Thumbnail of image '.$num.'" /><div>'.$num.'</div></li>';
//add 1 to the loop
$loop++;
}
echo '</ul>';
//Calculate the number of pages
$total_pages = $total / $max;
//clean it up
if(!is_int($total_pages))
{
$total_pages = floor($total_pages) + 1;
}
//start the page count at 1
$ploop = 1;
echo '<hr /><div id="portfolio-wrap"><div id="pages">Page: ';
while($ploop <= $total_pages)
{
$offset = ($ploop * $max) - $max;
if($startcount == $offset)
{
echo '<span>'.$ploop.'</span>';
}
else
{
echo ''.$ploop.'';
}
$ploop++;
}
echo '</div>';
echo '<div id="portfolio-foot-left"><p>Displaying Images <strong>'.($startcount + 1).' - '.$num.'</strong> of <strong>'.$total.'</strong></p></div></div>';
?>
Pretty much what i want is to make section drop down a bit more where i can add in additional info about the pic. Any suggestions?
You need a database or else you can store your descriptions in a text file next to the image. What have you tried so far?
Basically you need to create a form that accepts the image name and caption, take the caption and write it to a file or database. Then when displaying the caption, you just add a <div> that reads from file with file_get_contents(). Why don't you copy the script to another directory and start experimenting? People probably won't write the whole thing for you ;)