How can I include PHP subs in a html <TD> tag using echo

Discussion in 'Programming' started by grantp22, Sep 15, 2009.

  1. #1
    How can I include PHP IF-ELSE subs in a html <TD> tag using echo, for example, I need to do the following:

    <?
    if($session->logged_in){
       echo "<h1>Logged In</h1>";
       echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
           ."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] &nbsp;&nbsp;"
           ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";
       if($session->isAdmin()){
          echo "[<a href=\"admin/admin.php\">Admin Center</a>] &nbsp;&nbsp;";
       }
       echo "[<a href=\"process.php\">Logout</a>]";
    }
    else{
    ?>
    Code (text):
    And this is what I have, see below, but it doesn't work correctly, the IF-Else get ignored:

    <TD height="82" align="center" style="font-size: 8pt"><? echo "<b>Member Total:</b> ".$database->getNumMembers(); ?>
    <p><? echo "There are ".$database->num_active_users; ?><? echo " registered members and ".$database->num_active_guests; ?><? echo " guests viewing the site."; ?></p>
    <p>
       <? echo "<?"; ?> 
       <? echo "if(".$session->logged_in; ?><? echo "){"; ?>
       <? echo "<h1>Logged In</h1>"; ?>
       <? echo "Welcome <b>".$session->username; ?><? echo "</b>, you are logged in. <br><br>"; ?>
       <? echo "[<a href=\"userinfo.php?user=".$session->username; ?><? echo "\">My Account</a>] &nbsp;&nbsp;"; ?>
       <? echo "[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;"; ?>
       <? echo "if(".$session->isAdmin(); ?><? echo "){"; ?>
       <? echo "[<a href=\"admin/admin.php\">Admin Center</a>] &nbsp;&nbsp;"; ?>
       <? echo "}"; ?>
       <? echo "[<a href=\"process.php\">Logout</a>]"; ?>
       <? echo "}"; ?>
       <? echo "else{"; ?>
       <? echo "?>"; ?>
    </p>
    </TD>
    Code (text):
    I basically just need to have this subroutine in my table data cell, any help will be much appreciated, coz this is driving me nuts! :confused:

    Thanks
     
    grantp22, Sep 15, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Oh My God! :) You can't put php code in a echo! I think you have to build a better piece of php code to do what you want to, but without put php code in echo!
     
    pneulameiro, Sep 16, 2009 IP
  3. brian65

    brian65 Active Member

    Messages:
    1,172
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    88
    #3
    If the first PHP example is what you need why don't you use it, i.e. try it and see if it works?

    In the second example, you are echo PHP statements as text, i.e. you are telling PHP to output statements looking like PHP to the web browser.
     
    brian65, Sep 16, 2009 IP
  4. pepprs

    pepprs Peon

    Messages:
    195
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you want the first script to output inside a TD tag, include that file inside the tag or use eval function. The second script wont be helping your case and it is not a valid approach.
     
    pepprs, Sep 18, 2009 IP