Page 1 of 1

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

Posted: Tue Sep 15, 2009 9:49 pm
by grantp22
How can I include PHP IF-ELSE subs in a html <TD> tag using echo, for example, I need to do the following:

Code: Select all

<?
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{
?>
And this is what I have, see below, but it doesn't work correctly, the IF-Else get ignored:

Code: Select all

<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>
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! :banghead:

Thanks

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

Posted: Wed Sep 16, 2009 12:20 am
by califdon
PHP is a server language. It cannot be executed in the browser, after the page has loaded. You can only use PHP to do things before the page has been sent to the browser.

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

Posted: Wed Sep 16, 2009 1:51 am
by Ollie Saunders
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!
No doubt! What you're trying to do is impossible. Reach for the JavaScript!

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

Posted: Wed Sep 16, 2009 1:59 am
by Griven
Your code needed a bit of cleaning up. Try this:

Code: Select all

<td height="82" align="center" style="font-size: 8pt">
<b>Member Total:</b> <?php echo $database->getNumMembers(); ?>
<p>There are <?php echo $database->num_active_users; ?> registered members and <?php echo $database->num_active_guests; ?> guests viewing the site.</p>
<p>
<?php
    if($session->logged_in){ 
        echo '<h1>Logged In</h1>';
        echo 'Welcome <b>', $session->username ,'</b>, you are logged in. <br /><br />'; 
        echo '[<a href="userinfo.php?user=', $session->username ,'">My Account</a>] &nbsp;&nbsp;';
        echo '[<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>]';
    } 
?>
</p>
</td>
Your biggest issue was that you were trying to echo an IF statement, which isn't something that you can echo in this scenario. Also, you don't need that last ELSE statement, either. If the corresponding IF statement returns false, then nothing will appear anyway. Use the ELSE statement only when you explicitly want something else to happen.

If the above code doesn't work, you may want to check your $session class for errors--it may not be returning a TRUE value when someone is logged in as an admin, like it should be.

I hope this helps.