Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-23-2003, 03:36 PM   #1 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
Switching in & out of PHP/HTML

I'm putting together a script that is going to parse sports stats from an XML document and load all the data into an array. Then I need to print everything out in tables. So what I had in mind might look something like this:

Code:
<tr>
<td><? echo("$data['key1']"); ?></td>
<td><? echo("$data['key2']"); ?></td>
<td><? echo("$data['key3']"); ?></td>
</tr>
<tr>
<td><? echo("$data['key4']"); ?></td>
<td><? echo("$data['key5']"); ?></td>
<td><? echo("$data['key6']"); ?></td>
</tr>
The question is whether it's bad design to switch in/out of PHP mode so much (maybe several hundred times in one page).

Is it worthwhile to just print out the table tags in PHP with print(""), or does it really matter much? It's on a 2GHz dedicated server with 1GB of RAM.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 08-23-2003, 03:39 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
I dont think it really matters. although this shortcut might save you some time:
PHP Code:
<tr>
<td><?=$data['key1']?></td>
<td><?=$data['key2']?></td>
<td><?=$data['key3']=?></td>
</tr>
<tr>
<td><?=$data['key4']?></td>
<td><?=$data['key5']?></td>
<td><?=$data['key6']?></td>
</tr>
also, if you are using echo(), you really don't need the parenthesis if you don't want.

and, if you are only echoing a variable, then you don't need the quotes.
PHP Code:
<?
echo $data['key1'];
?>
but then it's all up to you and how you prefer.
__________________
Mike
sde is offline   Reply With Quote
Old 08-23-2003, 03:59 PM   #3 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
Cool, thanks for the shortcuts. Saving time = good!
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 08-23-2003, 04:23 PM   #4 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
I like using the HEREDOC style when I can, something like this:

PHP Code:
<?
echo <<< END

<tr>
<td>
{$data['key1']}</td>
<td>
{$data['key2']}</td>
<td>
{$data['key3']}</td>
</tr>
<tr>
<td>
{$data['key4']}</td>
<td>
{$data['key5']}</td>
<td>
{$data['key6']}</td>
</tr>

END;
?>
That should work fine; note that your closing END cannot have any indentation, no spaces or tabs. Somewhat of a limitation, IMHO. IIRC, you need to put braces around arrays, like {$array['key']} but normal variables don't need anything around it.

Does save alot of typing quotes.
bdl is offline   Reply With Quote
Old 08-23-2003, 05:24 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
hah cool .. i have never seen that before.
__________________
Mike
sde is offline   Reply With Quote
Old 08-23-2003, 05:39 PM   #6 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by sde
hah cool .. i have never seen that before.
Yeah, I've used it in several places to avoid matching quotes problems, although you're still in PHP mode, so it's only saving time if you have alot of vars or array values to echo. Not meant to be used as a substitute for exiting PHP and just doing straight html tags, in other words.

I've also seen something like this done:
PHP Code:
<?

$table 
= <<< END
<table>
 <tr>
  <td>some data</td>
 </tr>
</table>
END;

echo 
$table;

?>
bdl is offline   Reply With Quote
Old 08-23-2003, 06:01 PM   #7 (permalink)
EscapeCharacter
GNU/Punk
 
EscapeCharacter's Avatar
 
Join Date: Jul 2002
Location: stuffs
Posts: 66
EscapeCharacter is on a distinguished road
Send a message via AIM to EscapeCharacter
Quote:
Originally posted by bdl
Yeah, I've used it in several places to avoid matching quotes problems, although you're still in PHP mode, so it's only saving time if you have alot of vars or array values to echo. Not meant to be used as a substitute for exiting PHP and just doing straight html tags, in other words.

I've also seen something like this done:
PHP Code:
<?

$table 
= <<< END
<table>
 <tr>
  <td>some data</td>
 </tr>
</table>
END;

echo 
$table;

?>
wow cool never knew you do that.
__________________
cd /usr/ports/misc/life && make install
EscapeCharacter is offline   Reply With Quote
Old 09-03-2003, 09:07 AM   #8 (permalink)
Nitro
Registered User
 
Nitro's Avatar
 
Join Date: Jun 2003
Location: London, England
Posts: 29
Nitro is on a distinguished road
Send a message via AIM to Nitro
Hmm, I heard that it was best to keep the whole script in php, as it slowed it down or something switching in and out. I'm not sure on this, so perhaps someone could set me straight.
Nitro is offline   Reply With Quote
Old 09-06-2003, 06:01 PM   #9 (permalink)
DigitaLink
Registered User
 
DigitaLink's Avatar
 
Join Date: Sep 2003
Location: deep in a hole
Posts: 1
DigitaLink is on a distinguished road
Cool

I've always been understood that it was considered sloppy coding to escape in and out of HTML with PHP. I always try to code in PHP from page top to page bottom. Occasionally I miss a quote somewhere, but have gotten pretty good at finding those. There's nothing TECHNICALLY wrong with escaping to HTML, but it makes it hard to read and looks sloppy IMHO. At any rate, the important part is USING PHP, right? :rock: Rock on.
DigitaLink is offline   Reply With Quote
Old 10-14-2003, 11:48 AM   #10 (permalink)
Antagony
Registered User
 
Antagony's Avatar
 
Join Date: Mar 2003
Posts: 31
Antagony is on a distinguished road
Send a message via ICQ to Antagony
Quote:
Originally posted by Nitro
Hmm, I heard that it was best to keep the whole script in php, as it slowed it down or something switching in and out. I'm not sure on this, so perhaps someone could set me straight.
I have also heard this. Not to mention that the code looks less than appealing if you switch back and forth a lot.

Check out the HEREDOC action that was posted above.

Another popular technique to use is to just echo all the HTML, but concatenate successive lines. For example:
PHP Code:
<?
echo "<tr>\n"
   
"<td>$data_one</td>\n"
   
"<td>$data_two</td>\n"
   
"<td>$data_three</td>\n"
   
"</tr>";
?>
... so on and so forth.

But my big question for the example code you posted is why didn't you use a foreach loop?

I guess maybe you can't in your real code, I don't know. Just curious.
Antagony is offline   Reply With Quote
Old 11-15-2003, 08:22 PM   #11 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
Quote:
But my big question for the example code you posted is why didn't you use a foreach loop?

I guess maybe you can't in your real code, I don't know. Just curious.
I just used 'key1', key2', etc. to simplify the example. What I'm actually doing is parsing an XML document into an associative array where the array keys, sometimes 10 or 15 levels deep (an array of arrays), correspond to the named XML tags. So there's no way to loop through it numerically.

But I've had it up and working for quite a while now, and some of the suggestions here helped a lot. Thanks to everyone!
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
switching from lilo to grub sde Linux / BSD / OS X 2 10-24-2002 01:14 PM


All times are GMT -8. The time now is 11:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting