User Name
Password
AppleNova Forums » Programmer's Nook »

Yay! i made a php!


Register Members List Calendar Search FAQ Posting Guidelines
Yay! i made a php!
Thread Tools
ThunderPoit
Captain Potatoman
 
Join Date: May 2004
Location: Saint Paul
 
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:

[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 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!
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to 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)

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
Jerman
Senior Member
 
Join Date: Jun 2005
Location: Siloam Springs, AR
 
2007-11-17, 05:07

Mad props on a thread title that made my night.
  quote
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2007-11-18, 11:58

Ewww... Smarty :/

(But congrats on your first script.)
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2007-11-18, 12:18

Yep, congrats. Now move on to a less icky language, if you can.
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2007-11-18, 12:27

Quote:
Originally Posted by rollercoaster375 View Post
Ewww... Smarty :/

(But congrats on your first script.)
What the problem is? Something else you prefer?
  quote
Gargoyle
http://ga.rgoyle.com
 
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
 
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...

[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
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to 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 He needs to be force fed XHTML.


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
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2007-11-18, 23:56

Quote:
Originally Posted by ast3r3x View Post
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.)

I really have nothing to put here, but I feel it's rather strange to not have one.
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2007-11-19, 01:26

Quote:
Originally Posted by ast3r3x View Post
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]<?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
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2007-11-19, 06:50

Quote:
Originally Posted by Brad View Post
...except that PHP does check single-quoted strings for escaped characters.

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

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
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
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


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 21:43.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2019, AppleNova