Making sawdust
Join Date: May 2004
Location: Minnesota
|
dont really need any help here, just happy for myself.
ive been trying to teach myself php/mysql to set up a comic site for my gf, and after getting a little bored with the book i was using, i decided to play around a little bit and see what i could do w/o an example sitting in front of me. And here is what i made: [PHP]<table border="1"> <?php $row = 0; while ($row < 4) { $column = 0; echo ("\t<tr>\n"); while ( $column < 3 ) { echo ("\t\t<td>\n"); echo ("\t\t\tColumn = $column<br>\n\t\t\tRow = $row\n"); echo ("\t\t</td>\n"); $column++; } echo ("\t</tr>\n"); $row++; } ?> </table>[/PHP] and here is the pretty code it writes!: Code:
<table border="1">
<tr>
<td>
Column = 0<br>
Row = 0
</td>
<td>
Column = 1<br>
Row = 0
</td>
<td>
Column = 2<br>
Row = 0
</td>
<tr>
<td>
Column = 0<br>
Row = 1
</td>
<td>
Column = 1<br>
Row = 1
</td>
<td>
Column = 2<br>
Row = 1
</td>
<tr>
<td>
Column = 0<br>
Row = 2
</td>
<td>
Column = 1<br>
Row = 2
</td>
<td>
Column = 2<br>
Row = 2
</td>
<tr>
<td>
Column = 0<br>
Row = 3
</td>
<td>
Column = 1<br>
Row = 3
</td>
<td>
Column = 2<br>
Row = 3
</td>
</table> It writes a 3x4 tablehooray for me and my kindergarten level php skills! ps, if anyone knows a better or cleaner way to do this, let me know, im happy to learn! edit: oh wow, i just realized i wasnt closing out my rows! fixed now! |
quote |
25 chars of wasted space.
|
Congrats! I really like PHP, and have been using it for years, so I think you've made a good choice about what language to start learning. If you haven't seen before, there are a few skilled PHP programmers on this board, so don't be afraid to ask questions.
If you are learning PHP, it might not be a bad idea to lear smarty as well so you can do something like the following to output the same as you have above. (well actually it starts at 1 instead of 0, but that wouldn't be hard to fix) Code:
<table border="1">
{section name=row loop=4}
<tr>
{section name=col loop=3}
<td>Row = {$smarty.section.row.iteration}<br />
Col = {$smarty.section.col.iteration}</td>
{/section}
</tr>
{/section}
</table> |
quote |
Senior Member
Join Date: Jun 2005
Location: Siloam Springs, AR
|
Mad props on a thread title that made my night.
![]() |
quote |
Senior Member
|
Ewww... Smarty :/
(But congrats on your first script.) |
quote |
‽
|
Yep, congrats.
![]() ![]() |
quote |
25 chars of wasted space.
|
|
quote |
http://ga.rgoyle.com
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
|
ThunderPoit, I would not get into the habit of throwing too much formatting into your PHP generated HTML output. It makes your PHP code a lot more difficult to read, browsers don't really care all that much for tabs and nice code layout and when you start using firebug, you won't really spend all that much time reading your own generated HTML anyway.
Here is a slight variation on your example, same thing just a different way to go about it... [php] $rows = array( 'row1' => array('col1', 'col2', 'col3'), 'row2' => array('col1', 'col2', 'col3') ); echo "<table>\n"; foreach($rows as $rowname => $columns){ echo "<tr id=" . $rowname . ">"; foreach($columns as $value){ echo "<td>" . $value . "</td>"; } echo "</tr>\n"; } echo "</table>\n"; [/php] should give you this... Code:
<table>
<tr id=row1><td>col1</td><td>col2</td><td>col3</td></tr>
<tr id=row2><td>col1</td><td>col2</td><td>col3</td></tr>
</table> OK, I have given up keeping this sig up to date. Lets just say I'm the guy that installs every latest version as soon as its available! |
quote |
25 chars of wasted space.
|
While I don't often follow my own advise, Gargoyle is correct about trying to format your PHP. I normally do format it because I'm bad person, but it's really a lot better if you don't. That is one of the reasons I recommend smarty, because while it may make outputting easier to read, it definitely makes your PHP code easier to read. However, for shame Gargoyle about not using putting quotes around your id attribute. You know better
![]() ![]() Also keep in mind you can always break out of your PHP: [PHP]<?php //… foreach($rows as $rowNum) { ?><td><? echo('Row: '.$rowNum); ?><br /><? echo('Col: '.$colNum; ?></td><? } //… ?>[/PHP] And some random tips: Single quoted strings ('like this') are faster because PHP doesn't have to parse them looking for escaped characters and variables in double quoted strings ("like this"). When you use echo, it's faster to separate items by a comma than to concatenate them with a period. |
quote |
Senior Member
|
Biggest smarty problems, IMO:
1. Excessive Bloat 2. 'Soft' wall between M/C and V. 3. Poor semantic support for XHTML/XML 4. Arbitrary syntax introduction 5. Code generation-based caching makes trace-based debugging all that much harder. And no, AFAIK there's nothing worth using that's packaged. I usually end up writing my own sort of system that isn't really generalized (It uses a Data Object-esque class for managing the generated page). I started work on a templater a while back with a friend. While it works, it has serious usability issues (though, on a semantic level, it's quite nice.) I really have nothing to put here, but I feel it's rather strange to not have one. |
quote |
Selfish Heathen
Join Date: May 2004
Location: Zone of Pain
|
Quote:
[php]<?php $foo = 'Moe\'s Bar'; ?>[/php] ![]() Since we're discussing different manners of output, there's also this syntax: [php]Let's have drinks at <?=$foo?> tonight.[/php] |
|
quote |
25 chars of wasted space.
|
Quote:
|
|
quote |
Posting Rules | Navigation |
|
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Secure Cart Cookbook: PHP & MySQL | Moogs | Programmer's Nook | 18 | 2007-06-30 23:15 |
IE sucks and PHP isn't working | Kraetos | Programmer's Nook | 12 | 2006-09-01 15:39 |
please explain 'class' in PHP | nassau | Programmer's Nook | 12 | 2006-07-13 13:36 |
PHP vs all the others? | nassau | Programmer's Nook | 18 | 2006-06-29 03:49 |
PHP & Apache 2 | ast3r3x | Genius Bar | 0 | 2005-03-16 20:38 |