Jump to content

PHP variable within OnClick within PHP echo?


Josh417

Recommended Posts

Hi!

 

So I'm trying to build a lightweight image gallery.

 

I'm trying to show all pictures from a specific directory and have it so when a user clicks on the picture it opens a new window with the high res picture.

 

 

This is my code:

 


<?php
 
   $files = glob("images/*.*");
 
   for ($i=1; $i<count($files); $i++)
 
  {
 
  $image = $files[$i];
  
  
  echo '<div class="img"><a href="#" onClick="window.open(\'.$image .\', \'width=600\', \'height=400\', \'scrollbars=no\', \'menubar=no\');"><img width="160" height="140" src="'.$image .'"></a></div>';
 
  
  ;
   }

 

Problem I'm having is probably VERY easy to fix, but I can't figure it out.

 

When it opens the new window instead of linking to the picture I'm trying to open, it has
www.root.com/.$image%20
as the path...

 

What am I missing here? Why won't it give the actual path to the image? On the main gallery.php page the images show up fine, and if I put the
.$image .
code in the href="" tag it works as well.

 

 

Any tips would be appreciated!

Link to comment
Share on other sites

You don't actually break out of the single quotes in the PHP string to have $image concatenated.  Also you probably need to add images/ to the location:
echo '<div class="img"><a href="#" onClick="window.open(\'images/'.$image.'\', \'width=600\', \'height=400\', \'scrollbars=no\', \'menubar=no\');"><img width="160" height="140" src="'.$image.'"></a></div>';

 

Link to comment
Share on other sites

That's awesome. I knew it was something small like that.

 

Follow up question.

This is what I've got now, but when I try to set the attributes of the window, only the width and height seem to work/change anything. It's not

 

<?php
 
   $files = glob("images/*.*");
 
   for ($i=1; $i<count($files); $i++)
 
  {
 
  $image = $files[$i];
  
  
  echo '<div class="img"><a href="#" onClick="window.open(\''.$image.'\', \'Photo\', \'width=600,height=350,resizable=no,scrollbars=no,location=no,menubar=yes\');"><img width="160" height="140" src="'.$image.'"></a></div>';
  
   }
 
 ?> 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.