1

Topic: Syntax question - echo content from several fields

I'm not a coder. I have customized my MySQL tables.

This works to show someone's real name on a page:

<? echo ($user['firstname']) ?> <? echo ($user['surname']) ?>

But can I shorten this line? I've tried all kinds of variations, but can't figure out what the syntax is.

Re: Syntax question - echo content from several fields

<?php echo $user['firstname'].$user['surname']; ?>

3

Re: Syntax question - echo content from several fields

Smartys wrote:
<?php echo $user['firstname'].$user['surname']; ?>

That will place them as an unbroken output though, will it not? Should that not be:

<?php echo "$user['firstname'] $user['surname']"; ?>

for proper firstname[:space:]lastname type output?

Re: Syntax question - echo content from several fields

Fair enough

<?php echo $user['firstname'].' '.$user['surname']; ?>

5

Re: Syntax question - echo content from several fields

Thanks Smartys!!

I did this to get a space between first and last name:

<? echo $user['firstname'].' '.$user['surname']; ?>

This will help me fix lots of other things in my site. :-)

Re: Syntax question - echo content from several fields

As a side note, it's always good practice to use full php tags ( <?php )not short tags ( <? ), since short tags are not enabled on all servers.

7 (edited by Peter 2007-09-16 20:31)

Re: Syntax question - echo content from several fields

Related question. In want to get the full name in this line, so firstname and surname with a space inbetween. (I'm customizing PunBB, so I have these database fields.)

<? echo $lang_index['Newest user'] ?>: <a href="profile.php?id=<? echo $stats['last_user']['id'] ?>"><? echo pun_htmlspecialchars($stats['last_user']['firstname']) ?></a>

firstname and surname seperate works, but I can't figure out he right syntax to get both first and surname. For example, this doesn't work:

<? echo pun_htmlspecialchars($stats['last_user']['firstname']['surname']) ?>

What's the cleanest/quickest way?

Thanks!

BTW, thanks for the side note, Reines. I didn't know that. It's good to know when I move to another server. For now it works and I like to remove "unnecessary" things from the code whereever possible. I'm easily confused. ;-)

Re: Syntax question - echo content from several fields

So if I look at this right, $stats is a 2D array correct? If so you would access the surname by doing this:

<?php echo pun_htmlspecialchars($stats['last_user']['surname']) ?>
aka Mr Puto

9

Re: Syntax question - echo content from several fields

Sure, but how do I get firstname and surname at the same time? There must be a cleaner way to do it than just putting both these lines in a row.

Re: Syntax question - echo content from several fields

<?php echo pun_htmlspecialchars($stats['last_user']['firstname']).' '.pun_htmlspecialchars($stats['last_user']['surname']) ?>

11

Re: Syntax question - echo content from several fields

That works. Thanks again Smartys!