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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grantp22
Forum Newbie
Posts: 24
Joined: Tue Sep 15, 2009 9:34 pm

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

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

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

Post 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!
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

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

Post 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.
Post Reply