Simple PHP Variable Woes

Soldato
Joined
31 Oct 2005
Posts
8,846
Location
Leeds
After taking advice from IIRC DJ Jestar about using the glob function I have ended up with this

PHP:
		<?php  
  
  foreach glob("'$d_no'.JPG") as $file) {
    
	
    echo '<td>' . "<a href='$file' target=_blank><img  src='$file'  alt='$file'  width='220' height='220'></a>" . '</td>'; 
	}   
echo '</table>'; 

  
?>

Now the above works when I use
PHP:
(glob("'7.JPG") as $file) {
as there is a file called 7.JPG

However I'm trying to use the variable $d_no, which when echoed on that page displays fine, however if I used the first piece of php code, globing the variable $d_no, I'm met with

"Warning: Invalid argument supplied for foreach() in /home/irgscouk/public_html/userpage.php on line 61"

I'm sure it's just the way I'm declaring my variable

Any ideas?
 
Be sure to know exactly what is being sent to glob..
PHP:
$d_no = 'foo';
echo "'$d_no'.JPG";
this will output:
Code:
'foo'.JPG

I'm not sure you want that?
 
Think you're after:

Code:
foreach glob($d_no . ".JPG") as $file) {

and see Jestar's explanation about mixing quotes.
 
basically in the same directory as this php script I have a few images

1.JPG
2.JPG etc etc

I want the relevant picture to be displayed upon the choice of the variable

So when id = 1 display 1.JPG

As I said, if I MANUALLY say, display Just 2.JPG, its fine and happy

If I say, dynamically display that variable.JPG, it's not happy

Jestar, I'm using the variable d_no on other things on that page, and they are coming out fine

The variable is definately The number asked
 
If you only want a single file, whose name you already know, then why are you looping over glob? :confused:

Surely this does exactly the same thing:
PHP:
<?php

$file = $d_no . '.JPG';
echo '<td><a href="' . $file . '" target="_blank"><img  src="' . $file . '"  alt="' . $file . '"  width="220" height="220" /></a></td>'; 
echo '</table>';

?>

Also, just to be pedantic: TD elements should be contained within TR elements in HTML tables.
 
Last edited:
no no, I was looking for a file where filename = variable

my point was it wasn't working as I was declaring my variable wrong

The other point was that the script was working due to the fact that if I MANUALLY declared a file, like 2.jpg it worked so I knew it was how I was declaring the variable, hence the thread:D

Anyhoo it's all sorted now
 
Back
Top Bottom