Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

loop if image exists?


Go to End


10 Posts   4072 Views

Avatar
nicknick

Community Member, 15 Posts

18 February 2013 at 1:32pm

ok, so i have my lightbox working now but i would like to allow for different numbers of images

so can my bikepage.ss do a check to see how many of my images are present (i.e how many of my 10 upload fields actually have an image in) then loop the required number of times to add the following code
<div class="image"><a title="$Model" rel= "lightbox-$model" href="$photo.URL"> <img src="$photo.SetWidth(104).URL" width="75" height="75" /></a></div>

i would also need to increment the $photo. by 1 each time
i.e
count photo's with content in
set $photo = $photo
<div class="image"><a title="$Model" rel= "lightbox-$model" href="$photo.URL"> <img src="$photo.SetWidth(104).URL" width="75" height="75" /></a></div>
$photo = $photo+1
loop
or
for each $photo as $photoid
<div class="image"><a title="$Model" rel= "lightbox-$model" href="$photoid.URL"> <img src="$photoid.SetWidth(104).URL" width="75" height="75" /></a></div>

any suggestions gratefully received
thanks
nick

Avatar
nicknick

Community Member, 15 Posts

18 February 2013 at 10:13pm

I thought this would work but doesn't

<% if $photo.exists() %>
<div class="image"><a title="$Model" rel= "lightbox-$model" href="$photo.URL"> <img src="$photo.SetWidth(104).URL" width="75" height="75" /></a></div> 
<% end_if %>
<% if $photo1.exists() %>
<div class="image"><a title="$Model" rel= "lightbox-$model" href="$photo1URL"> <img src="$photo1.SetWidth(104).URL" width="75" height="75" /></a></div> 
<% end_if %>

the above only shows the first image, its as if the end if stops all execution of images. however the method does work if i only had one image, any improvements?

Avatar
kinglozzer

Community Member, 187 Posts

18 February 2013 at 10:53pm

Hi nicknick,

It sounds like you're doing:

$has_one = array(
'Image1'=>'Image',
'Image2'=>'Image'
... 

.. etc. You should instead be using:

$many_many = array(
'Photos' => 'Image'
);

You can then use one UploadField to add as many images as you like. You can set a max amount of images like this:

$uploadField = new UploadField('Photos', 'Photos');
$uploadField->setConfig('allowedMaxFileNumber', 10);

You can then, in your template, do:

<% if Photo %><% control Photo %>

<% end_control %><% end_if %>

If you need the position in the loop (usually $i, when you do $i++ in standard PHP) you can use $Pos.

Hope this helps :)

Avatar
nicknick

Community Member, 15 Posts

18 February 2013 at 11:00pm

Ahh ha, this looks to be the solution im looking for, i had lost internet at home and was trying to add the new image fields from memory, when i used the has many i must have had the sintax wrong as the images did not add to the db properly, i.e i could keep adding images in the admin area but paths were wrong etc.
i will re write later using your suggestion and let you know.
Thanks
Nick

Avatar
kinglozzer

Community Member, 187 Posts

19 February 2013 at 6:13am

Hi Nick,

You can't use $has_many for Images. This is because when using a $has_many, you need to define a $has_one on the other end - which you can't do without editing a built in class (bad idea). So for Images and Files, always use $has_one or $many_many. That's probably why they weren't saving properly.

Avatar
nicknick

Community Member, 15 Posts

19 February 2013 at 8:11am

Edited: 19/02/2013 9:28am

hmm, looks like ive got started on the wrong foot, please review the below code for my BikePage.php and let me know what i have done wrong, this is the original that produces $image - $image10

<?php
class BikePage extends Page {
    static $db = array(
	'Make' => 'Text',
				 'Model' => 'Text',
				 'Eng' => 'Text',
				 'MaxSpeed' => 'Text',
				 'Colours' => 'Text',
				 'Wheels' => 'Text',
				 'Length' => 'Text',
				 'Width' => 'Text',
				 'Height' => 'Text',
				 
    );
    static $has_one = array(
        'Photo' => 'Image',
		 'Photo1' => 'Image',
		  'Photo2' => 'Image',
		   'Photo3' => 'Image',
		    'Photo4' => 'Image',
			 'Photo5' => 'Image',
			  'Photo6' => 'Image',
			   'Photo7' => 'Image',
			    'Photo8' => 'Image',
				 'Photo9' => 'Image',
				  'Photo10' => 'Image',
				 
				 
    );
     
    public function getCMSFields() {
        $fields = parent::getCMSFields();
         
        $fields->addFieldToTab("Root.Images", new UploadField('Photo'));
       $fields->addFieldToTab("Root.Images", new UploadField('Photo1'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo2'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo3'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo4'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo5'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo6'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo7'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo8'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo9'));
	   $fields->addFieldToTab("Root.Images", new UploadField('Photo10'));
	   $fields->addFieldToTab("Root.Info", new TextField('Make'));
	   	   $fields->addFieldToTab("Root.Info", new TextField('Model'));
		    $fields->addFieldToTab('Root.Info', new TextField('Eng'));
			$fields->addFieldToTab('Root.Info', new TextField('MaxSpeed'));
			$fields->addFieldToTab('Root.Info', new TextField('Colours'));
			$fields->addFieldToTab('Root.Info', new TextField('Wheels'));
			$fields->addFieldToTab('Root.Info', new TextField('Length'));
			$fields->addFieldToTab('Root.Info', new TextField('Height'));
			$fields->addFieldToTab('Root.Info', new TextField('Width'));
			 $config = GridFieldConfig_RelationEditor::create();
		  $BikeTypeField = new GridField(
            'BikeType',
            'BikeType',
            $this->BikeType(),
            GridFieldConfig_RelationEditor::create());
			$fields->addFieldToTab('Root.BikeType', $BikeTypeField);			
        return $fields;
    }
	static $many_many = array(
        'BikeType' => 'BikeType'
    );
}
 
class BikePage_Controller extends Page_Controller {
}

Thanks
Nick
p.s when i try to change the code for the above i get errors regarding redeclare and unexpect t string when i run dev/build

Avatar
kinglozzer

Community Member, 187 Posts

20 February 2013 at 1:28am

Hi Nick,

Try this:

<?php 
class BikePage extends Page { 
static $db = array( 
   'Make' => 'Text', 
             'Model' => 'Text', 
             'Eng' => 'Text', 
             'MaxSpeed' => 'Text', 
             'Colours' => 'Text', 
             'Wheels' => 'Text', 
             'Length' => 'Text', 
             'Width' => 'Text', 
             'Height' => 'Text', 
             
); 
static $many_many = array(
    'Photos' => 'Image'    
);

public function getCMSFields() { 
$fields = parent::getCMSFields();

$uploadField = new UploadField('Photos', 'Photos);
$uploadField->setConfig('allowedMaxFileNumber', 10);
$fields->addFieldToTab("Root.Images", $uploadField); 

    $fields->addFieldToTab("Root.Info", new TextField('Make')); 
        $fields->addFieldToTab("Root.Info", new TextField('Model')); 
       $fields->addFieldToTab('Root.Info', new TextField('Eng')); 
         $fields->addFieldToTab('Root.Info', new TextField('MaxSpeed')); 
         $fields->addFieldToTab('Root.Info', new TextField('Colours')); 
         $fields->addFieldToTab('Root.Info', new TextField('Wheels')); 
         $fields->addFieldToTab('Root.Info', new TextField('Length')); 
         $fields->addFieldToTab('Root.Info', new TextField('Height')); 
         $fields->addFieldToTab('Root.Info', new TextField('Width')); 
          $config = GridFieldConfig_RelationEditor::create(); 
       $BikeTypeField = new GridField( 
'BikeType', 
'BikeType', 
$this->BikeType(), 
GridFieldConfig_RelationEditor::create()); 
         $fields->addFieldToTab('Root.BikeType', $BikeTypeField);          
return $fields; 
} 
   static $many_many = array( 
'BikeType' => 'BikeType' 
); 
}

class BikePage_Controller extends Page_Controller { 
}

Avatar
nicknick

Community Member, 15 Posts

20 February 2013 at 9:34am

Edited: 20/02/2013 9:45am

Thanks King, ive got it working now but had to do several tweaks, interestingly i found i could not use the $photos inside the control area. i solved it using the following that also $Up's to pull the naming and grouping of images.

			<% loop BikeType %>
                                $Name<% if Last !=1 %>,<% end_if %>
                            <% end_loop %>
    </article>
        $Form
 

<% if Photos %>       <div class="gallery">
<div class="images"><% control Photos %>
<div class="image"><a title="$Up.Make $Up.Model" rel= "lightbox-$Up.model" href="$link"> <img src="$link" width="75" height="75" /></a></div>

<% end_control %></div>
</div><% end_if %>
			
</div>
<% include SideBar %>

It would be helpful to be able to use $Photos.Url and $Photos.Setwidth() within the control but i cant seem to get it to work

thanks again for your help
Nick

Go to Top