Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Printing HTML with PHP...

Options
  • 07-04-2009 10:08pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi,

    I'm just wondering whats the best way of printing HTML when it has to be passed through PHP?

    At the moment i'm just using echo with single quotes and letting inserting breaks in the editor to make it more readible.

    When I view the source from a browser the HTML looks weird.

    Also, when doing this you don't get to see anything in design view or get code suggestions in Dreamweaver..

    Here's a quick snippet of my work..
    extract($row);
    echo '<td>';
    echo 	'<img src="images/album_image.gif" class="album_thumbnail"/>';
    echo '</td> ';
    echo '<td>';
    echo 	'Name: '. $name .'<br />';
    echo 	'Description: <br />'. $description .'<br />';
    echo 	'<a href="view_album.php?albumId='. $album_id .'">View</a>';
    echo '</td>';
    
    


    How would you guys go about printing this?

    In some pages I am doing two things e.g. add a photo form and then on the same page I display a confirmation message with no from.. This means I can't just use straight html and then open up a <?php tag when I need to insert dynamic data.. or does it?

    Thanks..


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Just do one big multi-line echo and create line breaks in the resulting html source with "\r\n"

    So you could have...

    [php]
    <?php
    $nl = "\r\n";

    echo 'some text here
    is '.$value.' and some other
    stuff'.$nl;
    ?>
    [/php]

    or you could write it like
    [php]
    some text here is <?php echo $value ?> and some other stuff
    [/php]

    Or look up heredoc syntax or use include or require if you've got tons of html.

    The way you're doing it is alright though, you could fix the output html by doing:
    [php]
    $nl = "\r\n";
    echo '<td>'.$nl;
    echo '<img src="images/album_image.gif" class="album_thumbnail"/>'.$nl;
    echo '</td> '.$nl;
    echo '<td>'.$nl;
    echo 'Name: '. $name .'<br />'.$nl;
    echo 'Description: <br />'. $description .'<br />'.$nl;
    echo '<a href="view_album.php?albumId='. $album_id .'">View</a>'.$nl;
    echo '</td>'.$nl;
    [/php]
    Or if you want to go nuts on formatting you could add tabs with '\t'


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Cool, at least I wasn't too far off!

    Whats the deal with the "print <<<" statement, I saw it somwhere one but can't find it anymore?

    IIRC it prints text from multiple lines..??


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Check this out... link
    Although I don't find myself using it much anymore for some reason.
    Between multi-line echo statements and concatenating lines...

    You could also try building your output inside a single variable...

    $mystring = 'line 1';
    $mystring .= 'line 2';
    $mystring .= 'line 3';
    $mystring .= 'line 4';

    Then it's handy enough to output where ever you need it, without creating a big mess.
    echo $mystring;


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    depending on your code, you could have an If else statement and close off php tags
    [php]
    <?php
    if(whatever)
    {


    ?>

    <!-- HTML CODE -->

    <?php

    }

    else{}

    ?>[/php]


  • Registered Users Posts: 180 ✭✭marcphisto


    techguy wrote: »
    Cool, at least I wasn't too far off!

    Whats the deal with the "print <<<" statement, I saw it somwhere one but can't find it anymore?

    IIRC it prints text from multiple lines..??

    This is basically how the <<< works

    [PHP]
    $output =<<< eod
    <td>
    <img src="images/album_image.gif" class="album_thumbnail"/>
    </td>
    <td>
    Name: $name <br />
    Description: <br /> $description <br />
    <a href="view_album.php?albumID=$album_id">View</a>
    </td>
    eod;

    echo $output
    [/PHP]

    You can also run this through a while loop to spit out rows of data

    eg

    [PHP]
    while($row = mysql_fetch_array($res)) {
    $output .=<<< eod
    your output
    eod;
    }
    [/PHP]

    The closing eod; must be flush to the left hand side of your editor and must not contain any white space after the ; other wise you'll get errors


  • Advertisement
  • Registered Users Posts: 2,119 ✭✭✭p


    Why not simply do this? Should solve most of your needs. This is what any PHP dev I've ever worked with did.
    [HTML]
    <td>
    <img src="images/album_image.gif" class="album_thumbnail"/>
    </td>
    <td>
    Name: <?=$name?> <br />
    Description: <br /> <?=$description?> <br />
    <a href="view_album.php?albumID=<?=$album_id?>">View</a>
    </td>[/HTML]

    If you need to do any more advanced PHP processing do it via an include or at the top of the document as suggested. Doing it that way makes it really easy to work with HTML templates produced by others, and also for them to change them if necessary.


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    Printing HTML via PHP is silly for many reasons; you should follow p's advice above.


  • Registered Users Posts: 1,181 ✭✭✭ronkmonster


    p wrote: »
    Why not simply do this? Should solve most of your needs. This is what any PHP dev I've ever worked with did.
    [html]
    <td>
    <img src="images/album_image.gif" class="album_thumbnail"/>
    </td>
    <td>
    Name: <?=$name?> <br />
    Description: <br /> <?=$description?> <br />
    <a href="view_album.php?albumID=<?=$album_id?>">View</a>
    </td>[/html]If you need to do any more advanced PHP processing do it via an include or at the top of the document as suggested. Doing it that way makes it really easy to work with HTML templates produced by others, and also for them to change them if necessary.


    That requires asp tags turned on at the server.
    I'm not sure if its common to have turned on.

    But it is the tidiest way of inserting dynamic values.


  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    That requires asp tags turned on at the server.
    Short tags, you mean?

    If they aren't turned on, or just if you want to take the safest approach ::

    <?=$value;?>

    is the same as

    <?php echo $value; ?>


    The longer one will work regardless.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Some good tips here..

    I think i'll probably go with normal html then open php tags whereever I need dynamic data.. this is probably best as I am doing all the php and a friend is doing all the html on a project atm..


  • Advertisement
  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    I think that's the best idea. I don't know PHP, but did a lot of ASP coding a while ago, and where ever possible I separated the code from the markup, using variables or if statements, rather than spitting out lots of HTML on the fly.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    If you've a LOT of HTML, then put the HTML into a template file and use str_replace or sprintf to replace placeholders.


  • Closed Accounts Posts: 176 ✭✭elyod


    techguy wrote: »
    Hi,
    Also, when doing this you don't get to see anything in design view or get code suggestions in Dreamweaver..

    If you want Dreamweaver to have access to specific chunks of code which will only show in certain situations you could do something like below.

    Using the structure of a function to hide chunks of code, yet editors like Dreamweaver and Expression web will "see" them and allow you to edit them in design mode.

    [php]
    <div id='content'>
    <?php
    $input = 1;
    if($input == 1)
    {
    one();
    }
    elseif($input == 2)
    {
    two("argument");
    }
    else
    {
    three();
    }
    ?>
    </div>

    <?php
    //Functions
    function one()
    {
    ?>
    <p>Contents of function one</p>
    <?php
    } //end one()
    function two($arg)
    {
    ?>
    <p>Contents of function two, with an <?php echo $arg ?></p>
    <?php
    } //end two()
    function three()
    {
    ?>
    <p>Contents of function three </p>
    <?php
    } //end three()
    ?>
    [/php]


Advertisement