#1
Posted 03 July 2012 - 01:55 AM
- Ryan
#2
Posted 03 July 2012 - 03:21 AM
To output the pictures, you would convert the field to an array and iterate through it.
$sequence = explode(",", $page->extra_field);
$images = $page->images;
foreach ($sequence as $i){
$image = $images->eq($i);
echo "<img src='{$image->url}' alt='{$image->description}'>";
}too complicated?
#3
Posted 03 July 2012 - 04:11 AM
@somartist | modules created | support me, flattr my work flattr.com
#5
Posted 03 July 2012 - 04:48 AM
$img = $page->images->first(); $page->images->remove($img); $page->images->add($img); $page->save();
and the first image is at last position
@somartist | modules created | support me, flattr my work flattr.com
#6
Posted 03 July 2012 - 11:57 AM
$cnt = count($page->images);
foreach($page->images as $image) {
$image->sort = $cnt;
$cnt--;
}
// now tell it to sort by the field 'sort'
$page->images->sort('sort');
// you may not need to do this, but putting it here just in case
$page->trackChange('images');
$page->save();
Avoid calling $page->images->remove() just because that will queue an actual file to be removed when the page is saved.
Another way you might accomplish reversing the order:
$myImages = new Pageimages($page);
foreach($page->images as $image) {
$myImages->prepend($image);
}
$page->images = $myImages;
$page->save();
#7
Posted 03 July 2012 - 12:33 PM
I don't get why removing and readding would be a problem. Seems to work well.
Edit: Just wondering, but why does echo $image->sort not output anything?
@somartist | modules created | support me, flattr my work flattr.com
#8
Posted 04 July 2012 - 03:16 AM
On a side note: I am a bit confused as to the 'trackChange' method (why it exists) as I did see it in the inputField module as follows:
if($changed) {
$this->value->sort('sort');
$this->trackChange('value');
}
#9
Posted 04 July 2012 - 03:53 AM
@somartist | modules created | support me, flattr my work flattr.com
#10
Posted 05 July 2012 - 10:35 AM
I don't get why removing and readding would be a problem. Seems to work well.
If it works for you it should be okay. But I think that behind the scenes, it's actually physically deleting a file at some point. Do your filenames change at all when you do this? (i.e. like get a number appended to the end of them)
Edit: Just wondering, but why does echo $image->sort not output anything?
The $image->sort was for our use as a temporary variable. Not actually used by the core.
On a side note: I am a bit confused as to the 'trackChange' method (why it exists) as I did see it in the inputField module as follows:
That is just a way to tell the $page that something about the value changed. The $page may not know that the value changed because it's the same object instance (of Pagefiles) that the $page started with. A $page doesn't know all the inner workings of fields it deals with. As far as it can tell, it's still dealing with the same value.
Some types notify the page of the change automatically, and it may be the case here too, but I wasn't positive without testing it. So we were notifying the page just to be safe.
In my second example, it wasn't even a consideration to do a trackChange() because we actually set a new value to the $page (which it can definitely see).
#11
Posted 04 November 2012 - 10:46 AM
The only difference from the above is that I'm not using an array for image.
Like Soma I figured that for my new uploaded image file to overwrite the existing one I would have to delete it before adding the new one.
I have tried remove, unset, delete but can't seem to figure out how to do this?
If it's impossible I could always use an array of images and call out the most recent I suppose.
#12
Posted 05 November 2012 - 01:08 PM
$oldFile = 'myimage.jpg'; $newFile = '/some/path/or/url/myimage2.jpg'; $oldItem = $page->images->get($oldFile); $page->images->add($newFile); $newItem = $page->images->last(); // get the item just added $page->images->insertAfter($newItem, $oldItem); $page->images->delete($oldItem); $page->save();
#14
Posted 08 March 2013 - 09:51 AM
Hello all,
I'm trying to get my head around sorting user images as well.
Although, I don't really need to sort them as much as allow the user to choose which image is first.
The scenario would be something like:
1. Present user with a grid of thumbnails showing their existing images. (got this part).
2. User selects an image to "use as profile image".
3. Selected image is now set to first in the images array
4. $user->save;
Everything I think of seems far too complicated, I'm hoping there is a fairly straightforward way to do this via the API.
A nudge in the right direction would be much appreciated.
#15
Posted 08 March 2013 - 06:12 PM
Would it be nicer/easier to have profile image as a separate image field?
#16
Posted 08 March 2013 - 07:13 PM
So I would have a profile_image field and a user_images field, and move images between the 2?
Actually, I would just be assigning an image from user_images to profile_image, no need to remove it from user_images.
That might be the way to go.
#17
Posted 08 March 2013 - 07:30 PM
#18
Posted 08 March 2013 - 07:55 PM
Nice call. I do see a need to figure out custom sorting eventually (Soma gave me some pointers today on IRC) but in this case your solution is a lot easier.
Also tagged with one or more of these keywords: api, images
Community Support →
Getting Started →
Output images with limitStarted by k07n, 14 May 2013 |
|
|
||
Community Support →
General Support →
Problems with WireUploadStarted by 3fingers, 04 May 2013 |
|
|
||
Off Topic →
Pub →
Interesting Lightbox OptionStarted by MatthewSchenker, 26 Apr 2013 |
|
|
||
Community Support →
API & Templates →
Using while loop with PW APIStarted by kongondo, 25 Apr 2013 |
|
|
||
Community Support →
General Support →
Select field values not getting edited in front-end formStarted by onjegolders, 23 Apr 2013 |
|
|
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













