SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to dynamically build 2 column layout from text/image files.

    I have a bunch of pages that have text, and sometimes graphics. All of the text/images are in a single cell table. I need to find a way to create a 2 column layout from these files.

    I figure it's going to happen like this:

    read the file into string
    somehow figure out the total 'length' or the text and images
    split it into to even strings
    put into template with 2 columns.

    If anyone has done anything like this, or can help me at all, I'd really appreciate it. I'm trying now, but it's taking longer than I thought!

    Thanks,

    Matt

  2. #2
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is something that might get you started.

    1) Create a table with 2 columns.
    2) Count the total number of records and divide by 2
    3) Fill in column 1 then Fill in column 2


    Here is a sample breakdown :

    Code:
    <?php
    
    $s = "<table width=100%><tr><td>[DATA1]</td><td>[DATA2]</td></tr></table>";
    
    $totalrows   = 120;
    $cols          = 2;
    $maxperrow = ceil($totalrows / $cols);
    
    
    for($i=1; $i <= $totalrows; $i++){
    
       if($i <= $maxperrow) $data1 .= $i . "<br>";     
       if($i > $maxperrow)  $data2  .= $i . "<br>";
    
    }
    
    
    $s = str_replace('[DATA1]',$data1,$s);
    $s = str_replace('[DATA2]',$data2,$s);
    
    echo($s);
    
    ?>

    Quote Originally Posted by mwmitchell
    I have a bunch of pages that have text, and sometimes graphics. All of the text/images are in a single cell table. I need to find a way to create a 2 column layout from these files.

    I figure it's going to happen like this:

    read the file into string
    somehow figure out the total 'length' or the text and images
    split it into to even strings
    put into template with 2 columns.

    If anyone has done anything like this, or can help me at all, I'd really appreciate it. I'm trying now, but it's taking longer than I thought!

    Thanks,

    Matt

  3. #3
    SitePoint Enthusiast lacerus's Avatar
    Join Date
    Aug 2003
    Location
    Hamburg
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To me it sounds like mwmitchell has one big TEXT field in the DB and wants to split that and not a list of entries. If that is the case, you're idea sounds good to me. I would probably count the words (explode(), then count()) or maybe count the spaces (' ') and split according to that. Just figure out which is faster and/or shorter in code. Also you should strip out html tags before you count...

    I would ignore the 'length' of images at first and maybe only add x number of imaginary words for each pixel in height when I really have to.

  4. #4
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Buenos Aires, Argentina
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I understand this question, mwmitchell has several HTML files composed of text or images and text, all in a single table cell. And he needs to convert them to new HTML files with two columns.

    If I am right, it would be better having some sample of this HTML files here to figure it out: You will have to read the file, parse it's content, then create a new HTML.

    Mitchel: post a link or attach on of your files.

    Quote Originally Posted by mwmitchell
    I have a bunch of pages that have text, and sometimes graphics. All of the text/images are in a single cell table. I need to find a way to create a 2 column layout from these files.

    I figure it's going to happen like this:

    read the file into string
    somehow figure out the total 'length' or the text and images
    split it into to even strings
    put into template with 2 columns.

    If anyone has done anything like this, or can help me at all, I'd really appreciate it. I'm trying now, but it's taking longer than I thought!

    Thanks,

    Matt

  5. #5
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _Leo_
    As far as I understand this question, mwmitchell has several HTML files composed of text or images and text, all in a single table cell. And he needs to convert them to new HTML files with two columns.
    Yes you are exactly right. That's what I meant. Sorry for any confusion. I don't have the files ready yet. Because I'm still getting prepared for the job. The files will be more than likely composed of 1 table, and one cell/column/row. The contents will be text, images and even more tables to hold images/captions. So, I'm kind of imagining that I'll have to:

    get the contents from inside of the main table
    get the total count of paragraphs and images
    seperate the content into two halves

    I want the last paragraph on the left side to be complete. And the right side is used for only when there are more than 2 paragraphs. I guess I'll treat an image like a paragraph. I mean I see the paragraphs and images as 'blocks' and want to treat them as individuals.

    Does that make sense? Will I have to 'measure' the heighth of the image, or am I getting to complicated with that?

    Thanks!

    Matt

  6. #6
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Buenos Aires, Argentina
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right.

    Although it is complex, PHP is the right tool for the job. I mean, you will need to parse a lot, you will have to use Regular expresions for that.

    The first step, getting the content from inside the main table, should be easy. Of course, only if the input file format is allways the same. Let's say:

    <html>
    ...
    <body>

    <table>
    <tr><td>
    [content]
    </td></tr>
    </table>

    </body>
    </html>

    For this example, locating the start and end of content is as simple as locating "<tr><td>" and "</td></tr>" lines. Which is easy if you read the whole file into an array variable. Knowing the index value for both lines, all array items between them is the content you need to reformat.

    The second step, I think, should be measuring the height (relative height) of the whole content. Because you want to split it into two pices of the same height, which will be Browser-client dependent unless the content uses CSS (Style Sheets). If you have Styles used, you will know the height of a single line of text, and you can compare it with the size of an image. But if you don't have Style tags for font definition, you don't know it. In fact, even with CSS you can't be really sure what will the final user client (Explorer) do with the size of your text.

    Therefore, if you know the height of the font, you can calculate (total lines * font height) + img_height[1] + img_height[2] + ... + img_height[n] as the total height, then divide it by 2.

    Since text and images are mixed, you will have to know the order of them (text img img img text text img text), you can't just split in the middle. It won't work.

    Then, look for the better place to split the content.

    Take in count:

    * The error introduced by the images will depend on the total height of the content. I mean, images of 80 px of height won't bother you in a file of 4000 px

    * The total number of lines of a paragraph will increase as you decrease its width. This will happend if you put the content of a table with one column in other table of 2 columns but the same width. With half the space, a paragraph may go double in lines.

    I hope it helps. If you have questions regarding some specific task, I will answer if I can. Without more information, I can't go more specific myself.

    Quote Originally Posted by mwmitchell
    Yes you are exactly right. That's what I meant. Sorry for any confusion. I don't have the files ready yet. Because I'm still getting prepared for the job. The files will be more than likely composed of 1 table, and one cell/column/row. The contents will be text, images and even more tables to hold images/captions. So, I'm kind of imagining that I'll have to:

    get the contents from inside of the main table
    get the total count of paragraphs and images
    seperate the content into two halves

    I want the last paragraph on the left side to be complete. And the right side is used for only when there are more than 2 paragraphs. I guess I'll treat an image like a paragraph. I mean I see the paragraphs and images as 'blocks' and want to treat them as individuals.

    Does that make sense? Will I have to 'measure' the heighth of the image, or am I getting to complicated with that?

    Thanks!

    Matt


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •