PDA

View Full Version : Yay! i made a php!


ThunderPoit
2007-11-16, 22:54
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:

<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>

and here is the pretty code it writes!:

<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 table

hooray 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!

ast3r3x
2007-11-17, 02:26
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)

<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>

Jerman
2007-11-17, 05:07
Mad props on a thread title that made my night. :)

rollercoaster375
2007-11-18, 11:58
Ewww... Smarty :/

(But congrats on your first script.)

chucker
2007-11-18, 12:18
Yep, congrats. :) Now move on to a less icky language, if you can. ;)

ast3r3x
2007-11-18, 12:27
Ewww... Smarty :/

(But congrats on your first script.)

What the problem is? Something else you prefer?

Gargoyle
2007-11-18, 13:07
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...


$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";


should give you this...


<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>

ast3r3x
2007-11-18, 13:53
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 :p He needs to be force fed XHTML. :D


Also keep in mind you can always break out of your PHP:
<?php
//…
foreach($rows as $rowNum)
{
?><td><?
echo('Row: '.$rowNum);
?><br /><?
echo('Col: '.$colNum;
?></td><?
}
//…
?>

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.

rollercoaster375
2007-11-18, 23:56
What the problem is? Something else you prefer?

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.)

Brad
2007-11-19, 01:26
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").
...except that PHP does check single-quoted strings for escaped characters.

<?php $foo = 'Moe\'s Bar'; ?>

;)

Since we're discussing different manners of output, there's also this syntax:

Let's have drinks at <?=$foo?> tonight.

ast3r3x
2007-11-19, 06:50
...except that PHP does check single-quoted strings for escaped characters.

<?php $foo = 'Moe\'s Bar'; ?>

;)
I'll reword. If you plan on using php's noted 'special characters' you need to use a double quoted string. However, it is the slimmest amount faster to use a single quoted string. Information about about what to use when can be seen at php.net (http://us3.php.net/types.string)