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' ;
Related
I am running into a problem rendering a star rating and was hoping I can get some extra eyeballs on my problem. I have the normal rating working just fine with whole numbers but I am struggling to display the half star. For example I created a service that provides me with a rating 0-5 so I get a value like 2.5, 3 or 5 etc...
Before I go and create a switch case and create an svg for each variation I was hoping to get a little a pointer. Below is what I have currently, any tips would be greatly appreciated.
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
}
else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
Ideally I would like to add a condition at the end of the loop and check for the half and echo "";
There is probably an easier way to do it but this works, checks if $starRating is a float and then rounds it up and checks against $i to place the half star in the correct position.
<?php
$totalRating = 5;
$starRating = 2.5;
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
if(is_float($starRating) && (round($starRating) == $i)){
echo "<img src=\"/icons/star-half.svg\">";
}else{
echo "<img src=\"/icons/star-empty.svg\">";
}
}else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
You can verify if the value of $starRating is an integer, doing something like this (considering only half values):
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star_half.svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
If you want to show stars with a more precise value you can create images with the float values in the name, like "star-3.svg" (representing a .3 float value, and do something like this:
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star-" . floatval($starRating)) . ".svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
But in this case you need to take care to only receive float values with one number (2.2, 3.4, etc.).
I hope it helps...
I'm very new to PHP, but as a start, I've made a file deleter / lister, depending on time. If you access the page and the server finds files that are older than approx. 10 days, it will delete the file. Otherwise, it will display the file. What I'm trying to do, is sort the files that are displayed after time, so the newest come first. I've tried a variety of different approaches + searched around here, but not been able to find something that works for exactly this. I gave it a shot with directly applying
arsort($dir)
but since it's an object, it wouldn't let me. Can't really see it as a good way to sort in the for each for each item.
function fileDelete(){
$dir = new DirectoryIterator(dirname(__FILE__));
$now = time();
$j = 0;
foreach($dir as $fileinfo){
if($fileinfo->getExtension() == "png"){
if($now - filemtime($fileinfo) >= (60 * 60 * 24 * 10)){
unlink($fileinfo);
echo $fileinfo . " deleted " . "<br>";
$j++;
} else {
$data = getImageSize($fileinfo);
$width = $data[0];
$height = $data[1];
?>
<img src="<?php echo $fileinfo ?>" width="<?php echo ($width/2) ?>" height="<?php echo ($height/2) ?>">
<?php }
}
}
if($j == 0){
return "No files deleted";
} else {
return "<br>" . $j . " files deleted. <br>";
}
}
Here is my code. I also tried to array_push() it into another array and sort that, but for some reason, it didn't work. I hope someone can give me best practice on this and sorry for the duplicate if you find any. I didn't.
I actually fixed my own problem:
I split up the code into two foreach-loops. Made all the checks in the first one, deleted the file and then displayed the file in the next foreach loop.
To answer it fully: It was $fil[0] I was looking for.
function fileDelete(){
$dir = new DirectoryIterator(dirname(__FILE__));
$now = time();
$j = 0;
$filer = array();
foreach($dir as $fileinfo){
if($fileinfo->getExtension() == "png"){
if($now - filemtime($fileinfo) >= (60 * 60 * 24 * 10)){
unlink($fileinfo);
echo $fileinfo . " deleted " . "<br>";
$j++;
} else {
$filer[$fileinfo->getMTime()][] = $fileinfo->getFilename();
}
}
}
arsort($filer);
foreach($filer as $fil){
$data = getImageSize($fil[0]);
$width = $data[0];
$height = $data[1];
?>
<img src="<?php echo $fil[0] ?>" width="<?php echo ($width/2) ?>" height="<?php echo ($height/2) ?>">
<?php }
if($j == 0){
return "No files deleted";
} else {
return "<br>" . $j . " files deleted. <br>";
}
}
I am working on project which shows articles and this was done by article manager (a ready to use php script) but I have a problem, I want to show only four article titles and summaries from old list of article randomly which contains 10 article. Any idea how to achieve this process?
I have auto generated summary of article
<div class="a1">
<h3><a href={article_url}>{subject}</h3>
<p>{summary}<p></a>
</div>
When a new article is added the above code will generated and add into summary page. I want to add it to side of the main article page, where user can see only four article out of ten or more randomly.
<?php
$lines = file_get_contents('article_summary.html');
$input = array($lines);
$rand_keys = array_rand($input, 4);
echo $input[$rand_keys[0]] . "<br/>";
echo $input[$rand_keys[1]] . "<br/>";
echo $input[$rand_keys[2]] . "<br/>";
echo $input[$rand_keys[3]] . "<br/>";
?>
Thanks for your kindness.
Assuming I understood you correctly - a simple solution.
<?php
// Settings.
$articleSummariesLines = file('article_summary.html', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$showSummaries = 4;
// Check if given file is valid.
$validFile = ((count($articleSummariesLines) % 4 == 0) ? true : false);
if(!$validFile) {
die('Invalid file...');
}
// Count articles and check wether all those articles exist.
$countArticleSummaries = count($articleSummariesLines) / 4;
if($showSummaries > $countArticleSummaries) {
die('Can not display '. $showSummaries .' summaries. Only '. $countArticleSummaries .' available.');
}
// Generate random article indices.
$articleIndices = array();
while(true) {
if(count($articleIndices) < $showSummaries) {
$random = mt_rand(0, $countArticleSummaries - 1);
if(!in_array($random, $articleIndices)) {
$articleIndices[] = $random;
}
} else {
break;
}
}
// Display items.
for($i = 0; $i < $showSummaries; ++$i) {
$currentArticleId = $articleIndices[$i];
$content = '';
for($j = 0; $j < 4; ++$j) {
$content .= $articleSummariesLines[$currentArticleId * 4 + $j];
}
echo($content);
}
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");
}
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;
}
?>