Skip to main content
Inspiring
June 6, 2007
해결됨

Changing order of results to fit images into available space

  • June 6, 2007
  • 3 답변들
  • 654 조회
Hi all,

How would I go about outputting images stored in a database in a grid when images will be in various sizes and will be added in any order? All images will either be 100 x 50 or 300 x 50 and the display area will be 400 wide. What I need is for a way of CF to recognise that the if 2 images in a row are 300 wide, followed by 2 images at 100 wide, it should slot one of the 100 wide images after each of the 300 wide images.

So if the actual order of images stored was:
300x50
300x50
100x50
100x50
100x50
300x50

it should display them in an order to fill in the gaps left by the larger images, e.g.
300x50
100x50
300x50
100x50
100x50
300x50

Is this possible with CF? If not, how else is it achievable?

Many thanks in advance.

Paul
    이 주제는 답변이 닫혔습니다.
    최고의 답변: Outside5 Ltd
    In case anyone needs this in future, the final working code is attached.

    Paul

    3 답변

    Inspiring
    June 6, 2007
    Since you can only accomodate 4x100 OR 1x300+1x100, you'll need a way to alternate the 100 and 300px grahics. If you set the 300px graphics to an "odd" incrementation scheme and the 100 px graphics to an even incremental scheme, you'll get something like this:

    Assuming 2 300px graphics and 6 100px graphics:

    WIDTH, IMAGEORDER
    ====================
    300px, 1 <--- First 300px graphic (odd)
    100px, 2 <--- First 100px graphic (even)
    100px, 4 <--- Second 100px graphic (even)
    100px, 6
    300px, 3 <!--- Second 300px graphic (odd)
    100px, 8
    100px, 10
    100px, 12

    Now when you order the data, you get something like this:

    WIDTH, IMAGEORDER
    ====================
    300px, 1
    100px, 2 <!--- 400 px (First Row)
    300px, 3
    100px, 4 <--- 400 px (Second Row)
    100px, 6
    100px, 8
    100px, 10
    100px, 12 <!--- 400 px (Third Row)

    Once the data is ordered, you just have to keep a running tally of the pixel width for all the graphics in a row - as soon as you get to 400, create a new row.

    <table>
    <tr>
    <td>
    <cfset iRowWidth = 0>
    <cfoutput query="myOrderedQuery">
    <!--- Make New Row --->
    <cfif iRowWidth gte 400>
    </td></tr><tr><td>
    <cfset iRowWidth =0>
    </cfif>

    <!--- output image --->
    <img src="#myImage#" width="#myImageWidth#">
    <cfset iRowWidth = iRowWidth + myImageWidth>
    </cfoutput>
    </td>
    </tr>
    </table>
    Outside5 Ltd작성자
    Inspiring
    October 11, 2007
    Back to this old chestnut.

    I have built a function to deal with this, but have hit a bit of a brick wall.

    It has been decided that images will fill either 1 or 2 of the 4 available columns on the page. This means that the only images that need to be moved are panoramic ones (galleryPanomic EQ 1) if they are one of every fourth records.

    My query gets all the images for the page, then loops through the results to reorder them. I have used <cfif getGallery.galleryPanorama EQ 1 and not getGallery.currentRow MOD 4> to decide firstly if the image is panoramic and secondly if it is one of every fourth record.

    I am falling down at what to implement once this decision has been reached. If the image should be moved to the next wider space - say we are on the 3rd result (currentRow EQ 3) and there are 4 panoramic images that follow - would I use queryAddColumn? How would I handle the fact that 4 panoramic images follow? We know that the 4 panoramas would fill 2 rows of 4 columns, but how would I then set the next smaller image to go back to fill in the space left at the end of the first row?

    Many thanks in advance.

    Paul

    Outside5 Ltd작성자답변
    Inspiring
    October 12, 2007
    In case anyone needs this in future, the final working code is attached.

    Paul

    Inspiring
    June 6, 2007
    If you know your images are going to be only 300 and 100 px wide, maybe something like this?

    1) Add a column to your query called "imageorder" (QueryAddColumn)
    2) Loop over your query and assign an incremented value to myQuery.imageorder based on the width of the image (300 px - odds, 100px evens) - Take a look at QuerySetCell() and myQuery.currentRow
    3) Use Query of Query to order by imageorder.

    Then its just a matter of looping through your query. You'd just have to keep an eye on the width of each row and place the correct HTML code when a new row needs to be created.

    Outside5 Ltd작성자
    Inspiring
    June 6, 2007
    Michael,

    Many thanks for your reply. I *think* I *kind of* get what you are saying. Most of the images will be 100x50 with the occasional 300x50 thrown in. Would I still need to apply the 300 px odds, 100px evens logic you mentioned if this is the case?

    Thanks again,
    Paul
    June 6, 2007
    I'm not sure you can do this with native CF7 and below. The CF8 however has image reading capabilities that can give you all sorts of information.

    I guess then with the new features you could construct some if else logic to do the formatting you require.
    Outside5 Ltd작성자
    Inspiring
    June 6, 2007
    Cheers Kapitaine,

    If I were to store the image dimension in the db along with the location of the image I could construct some sort of logic to a similar effect, my probelm is syntax wise, how would I instruct it to place the smaller image out of the flow of the recordset?

    Any ideas anyone?

    TIA,
    Paul