Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

83 users online



foreach problems

foreach problems

Currently viewing this thread: 1 (0 members and 1 guests)


white_queen

white_queen

Status: Offline!

foreach problems

I am very new to PHP, so don't laugh at me if this problem is really easily solved....:confused:
Here is the code I have:
if ($action=="delete"){
$pics=directory("../gallery","JPG,jpg,JPEG,jpeg");
$pics=namefilter($pics,"tn_");
$pets=directory("../petfiles","pet");
// display list of pets with download links
foreach ($pets as $pet){
echo "<a href=\"javascript:loadOne();\">download</a>&#160;";
echo $pet;
echo "<br />\n";
}
foreach ($pics as $pic){
echo "";
}

}
/*
Download pet file
*/
echo "<SCRIPT LANGUAGE=\"JavaScript\"><!--
function loadOne() {
window.location.href = '../petfiles/$pet';
setTimeout(\"window.location.href='index.php?delfile=$pic&action=delete&display=Delete+images'\",5000);
setTimeout(\"window.location.href='index.php?delfile=$pet&action=delete&display=Delete+images'\",5000);
}</SCRIPT>";

The idea is that when the user clicks the loadOne link for a pet, they get the pet file downloaded, then it is removed from the list. The removal works, it's just when you click a pet to download, it always provides the same pet file, even with more than one in the directory.

Can anyone help?

~Milli

cannotbeused2

cannotbeused2

Neverside Newbie
Status: Offline!
PHP:

<?php

if ($action=="delete"){
$pics=directory("../gallery","JPG,jpg,JPEG,jpeg");
$pics=namefilter($pics,"tn_");
$pets=directory("../petfiles","pet");
// display list of pets with download links
foreach ($pets as $pet){
echo 
"<a href=\"javascript:loadOne();\">download</a> ";
echo 
$pet;
echo 
"<br />\n";
}
foreach (
$pics as $pic){
echo 
"";
}

}
/*
Download pet file
*/ 
echo "<SCRIPT LANGUAGE=\"JavaScript\"><!--
function loadOne() {
window.location.href = '../petfiles/$pet';
setTimeout(\"window.location.href='index.php?delfile=$pic&action=delete&display=Delete+images'\",5000);
setTimeout(\"window.location.href='index.php?delfile=$pet&action=delete&display=Delete+images'\",5000);
}</SCRIPT>"
;

?>

*making it readable*

___________________

evolvedbb

midsummerstorm

midsummerstorm

28, Male & single, what else do you need to know?
Status: Offline!

Hmmm... I guess the only file you're able to download is the last one in the dir, right? Smile

As far as I understand it, you want to create a list of links for the user to choose which pet to download. But you're always echoing the same href: <a href=\"java script:loadOne();\">download</a>. Then, how is the loadOne function supposed to know which file has been chosen?

Moreover, this link is not correctly formed, and should be <a href="#" onClick="loadOne();">download</a>. Now, I'm not a javascript guru, but what about passing the selected pet as an argument? Something like:

PHP:

<?php

foreach ($pets as $pet){
echo 
"<a href=\"#\" onClick=\"loadOne('$pet');\">download</a> ";
echo 
$pet;
echo 
"<br />\n";

// and the loadOne function
echo "<SCRIPT LANGUAGE=\"JavaScript\"><!--
function loadOne(pet_file) {
window.location.href = '../petfiles/'+pet_file;
setTimeout(\"window.location.href='index.php?delfile=$pic&action=delete&display=Delete+images'\",5000);
setTimeout(\"window.location.href='index.php?delfile=$pet&action=delete&display=Delete+images'\",5000);
}</SCRIPT>"
;

?>

I'm not really sure about those setTimeout functions and why you use them, but I'm pretty sure this could be done better with just PHP and removing all javascript in the page.

___________________

-~-~-~oO··Oo~-~-~-~-
I wish I was a messenger and all the news was good
I wish I was the full moon shining off your camaros hood
...

Last edited by midsummerstorm, May 1st, 2003 11:26 PM (Edited 1 times)

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.018 seconds.