|
 |
|
 |
08-23-2003, 03:36 PM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
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--
|
|
|
08-23-2003, 03:39 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
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
|
|
|
08-23-2003, 03:59 PM
|
#3 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
Cool, thanks for the shortcuts. Saving time = good!
__________________
--Epsilon--
|
|
|
08-23-2003, 04:23 PM
|
#4 (permalink)
|
|
Senior Contributor
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
|
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. 
|
|
|
08-23-2003, 05:24 PM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
hah cool .. i have never seen that before.
__________________
Mike
|
|
|
08-23-2003, 05:39 PM
|
#6 (permalink)
|
|
Senior Contributor
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
|
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;
?>
|
|
|
08-23-2003, 06:01 PM
|
#7 (permalink)
|
|
GNU/Punk
Join Date: Jul 2002
Location: stuffs
Posts: 66
|
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
|
|
|
09-03-2003, 09:07 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Jun 2003
Location: London, England
Posts: 29
|
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.
|
|
|
09-06-2003, 06:01 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Sep 2003
Location: deep in a hole
Posts: 1
|
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.
|
|
|
10-14-2003, 11:48 AM
|
#10 (permalink)
|
|
Registered User
Join Date: Mar 2003
Posts: 31
|
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.
|
|
|
11-15-2003, 08:22 PM
|
#11 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
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--
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:36 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|