Jump to:

23376 Posts in 18214 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » loop if image exists?

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

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

Page: 1 2
Go to End
Author Topic: 1032 Views
  • nicknick
    Avatar
    Community Member
    15 Posts

    loop if image exists? Link to this post

    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

  • nicknick
    Avatar
    Community Member
    15 Posts

    Re: loop if image exists? Link to this post

    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?

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: loop if image exists? Link to this post

    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

  • nicknick
    Avatar
    Community Member
    15 Posts

    Re: loop if image exists? Link to this post

    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

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: loop if image exists? Link to this post

    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.

  • nicknick
    Avatar
    Community Member
    15 Posts

    Re: loop if image exists? Link to this post

    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

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: loop if image exists? Link to this post

    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 {
    }

  • nicknick
    Avatar
    Community Member
    15 Posts

    Re: loop if image exists? Link to this post

    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

    1032 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.