Jump to content

display images


Argonust

Recommended Posts

Hi

I have a script for displaying images see below

<?php
 include "header.php";
 require 'gallery/Gallery.php';
 ?>

 <?php
 $models=file("models.txt", FILE_USE_INCLUDE_PATH);


if(isset($_POST['model'])){
   $model=($_POST['model']);
  }

$gallery = new Gallery();
$gallery->setPath('gallery/images/'.$model);

 $images = $gallery->getImages(array('jpg'));
?> 
 
<div class="container">
     <?php if($images): ?>
    <div class=gallery cf >
      <?php foreach($images as $image):?> 
      <div class="gallery-item">
        <a href="<?php echo $image['full'];?>"><img src="<?php echo $image['thumb']; ?>"> 
      
      </div>
    <?php endforeach; ?>
   </div>
   <?php else: ?>
      There are no images
   <?php endif; ?> 
   
 </div> 


<div class="form">
 <form action="index.php" method="POST">
  <select name="model" class="select_box" data-placeholder="Select Model" style= width:120px;/>
     <option></option>
     <?php foreach($models as $mod):?>
      <option><?php echo $mod ?></option>
     <?php endforeach;?>
  </select>
  <input type="submit" Value="Select" value="Select">
  </form> 
 </div>
 
 <script type="text/javascript">
  $(".select_box").chosen();
</script>

The above code works , i get no errors this  images array  is populated at this line  "$images = $gallery->getImages(array('jpg'));" but they do no show in the container class.

When I run this script all the css shows borders and the like. bet there are no images didplayed.

However it does work if I replace the code in the container class with this

$images = "gallery/images/".$model;
    $files = scandir($images);
    unset($files[0], $files[1]);
  
    $rows = array_chunk($files, 6);
 	
	foreach ($rows as $row){
	    foreach ($row as $image){
             echo '<td><a href="gallery/images/', $model,'/', $image,'"><img src="gallery/images/',$model,'/thumbs/',$image,'"/></a></td>';
           }
       }

Is it possible I have a conflict with the jQuery. I am using the plugin called "chosen"

The $models is a text file that populates the drop down box

Thank you, your help is very much appreciated

Link to comment
Share on other sites

2 minutes ago, Argonust said:

The above code works , i get no errors this  images array  is populated at this line  "$images = $gallery->getImages(array('jpg'));" but they do no show in the container class.

Then apparently it's not populated. Look at the Gallery code. And make sure you're calling the method correctly.

Link to comment
Share on other sites

2 hours ago, Argonust said:

Would'nt I get errors if I was calling the method incorrectley?

Maybe, maybe not. Depends on what you were doing and how their code was written.

Do a View Source of the page to see if those gallery-items were created. If not then there's something weird. If so then there is a different problem related to the gallery.

Link to comment
Share on other sites

Is error checking turned on?

What if there is no POST['model'] element?  You aren't handling that.  And why don't you check for it before bothering to read in that file?

 

PS - I really dislike your style of writing code.  This going in and out of php mode is tiresome.  Absolutely awful.

Link to comment
Share on other sites

1 hour ago, ginerjm said:

PS - I really dislike your style of writing code.  This going in and out of php mode is tiresome.  Absolutely awful.

What if it was more like

<?php

include "header.php";
require 'gallery/Gallery.php';

$models=file("models.txt", FILE_USE_INCLUDE_PATH);

if(isset($_POST['model'])){
	$model=($_POST['model']);
}

$gallery = new Gallery();
$gallery->setPath('gallery/images/'.$model);

$images = $gallery->getImages(array('jpg'));

?> 
 
<div class="container">
<?php if($images): ?>
	<div class="gallery cf">
<?php	foreach($images as $image): ?>
		<div class="gallery-item">
			<a href="<?=$image['full']?>"><img src="<?=$image['thumb']?>"> 
		</div>
<?php	endforeach; ?>
	</div>
<?php else: ?>
	There are no images
<?php endif; ?> 
</div> 


<div class="form">
	<form action="index.php" method="POST">
		<select name="model" class="select_box" data-placeholder="Select Model" style="width:120px;">
			<option></option>
<?php foreach($models as $mod): ?>
			<option><?=$mod?></option>
<?php endforeach; ?>
		</select>
		<input type="submit" Value="Select" value="Select">
	</form> 
</div>
 
<script type="text/javascript">
$(".select_box").chosen();
</script>

Cleaned up, proper indentation, <?php always at the beginning of the line and PHP-relative indentation right after, and short echo tags. That's how I do it and I think it's readable - at least as readable as any template system, and benefits from syntax highlighting unlike echoing strings does.

Link to comment
Share on other sites

I use heredocs for large chunks of non-php code.  I rarely use a php tag in the midst of my scripts.  For short pieces of html (perhaps in loops that build things) I use an echo statement rather than exit and enter php.  I never output html/js code directly until I have begun outputting the entire html document.

Link to comment
Share on other sites

15 minutes ago, ginerjm said:

I use heredocs for large chunks of non-php code.  I rarely use a php tag in the midst of my scripts.  For short pieces of html (perhaps in loops that build things) I use an echo statement rather than exit and enter php.  I never output html/js code directly until I have begun outputting the entire html document.

Thing I hate about that is lack of highlighting. For my own code I'm very big on IDE support: everything can be autocompleted because of docblocks (@vars and @params and @returns), classes and functions are searchable as symbols (very rarely embedded in strings), minimal to no use of magic behavior that's not easily documentable (__get/set/call are very specific in what they support). I've had to work with Laravel lately and I hate it.

Putting HTML into strings runs against those principles because IDEs very rarely support it, and when they do it's extremely limited (most I've seen is recognizing regexes with slash delimiters), so I hate building and outputting it that way for anything more than the trivial case. If, on the other hand, PHP were to support something like XHP ?, that would totally change the situation.

Link to comment
Share on other sites

When I mix HTML and PHP I tend to do as requinix did, except I keep the <?php tag at the same indentation as the HTML around it rather than beginning of the line.  I find it to be easily readable and understandable.  Most of the time these are essentially template files so like 90% html, 10% php.

 

@Argonust, you have some HTML errors in your original post which may or may not be causing you issues.  Missing quotes around your class names on the gallery div.   Missing a closing </a> on each image.

As suggested, check the view-source output to see if your HTML is being built but with no values, or if the HTML is missing all together.   If it's built but with no values, then in your foreach loop do var_dump($image) to see what the variable is for each item.

 

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.