[prog] php and if/else statements
wolf
wolf at wolfrising.net
Tue Dec 23 11:16:24 EST 2003
Thank you : ) And thank you for the advice on how to make the code look
more presentable
as well. It is per the usual to sometimes use an if/else inside a
do/while? It seems
do/while makes tables very nicely with php.
On Dec 23, 2003, at 3:52 AM, Jacinta Richardson wrote:
> On Mon, 22 Dec 2003, wolf wrote:
>
>> <?php do { ?>
>> <tr> </tr>
>> <td><?php echo $row_Term_Number['term_number'];?></td>
>> <td> <?php echo $row_Term_Number['term'];?> <?php echo
>> $row_Term_Number['year'];?></td>
>>
>> <?php } while ($row_Term_Number =
>> mysql_fetch_assoc($Term_Number)); ?>
>
> G'day Wolf,
>
> I can't help but cringe when I see code like that. I fully understand
> embedding php within html but I feel that code like that rarely does
> more
> than confuse the issue. So, for starters, here is the code rewritten
> in a
> more readable manner:
>
> <?php
> do {
> echo "<tr></tr>\n"; # Check that you mean to close
> this table
> # this table row straight after
> opening it
> echo "<td>";
>
> echo $row_Term_Number['term_number'];
> echo "</td>\n";
> echo "<td>";
> echo $row_Term_Number['term'];
> echo " ";
> echo $row_Term_Number['year'];
> echo "</td>\n";
> # I think you mean to have </tr> here
>
> } while ($row_Term_Number = mysql_fetch_assoc($Term_Number));
> ?>
>
> The spacing in the html source will be different with this code, but
> you
> can change that easily enough (but you probably won't care unless
> you're
> fanatical). Anyway, your idea for how to use an if/else in here to get
> it to work the way you want it to was correct. Now that we've got the
> code in a more manageable form, this should be easy:
>
> <?php
> do {
> echo "<tr>\n"; # I've taken out the close tr.
> echo "<td>";
>
> # Handle the case where term_number is 0.
> if ($row_Term_Number['term_number'] == 0) {
> echo " </td>\n";
> echo "<td>TC/EC</td>\n";
> }
> else {
> echo $row_Term_Number['term_number'];
> echo "</td>\n";
> echo "<td>";
> echo $row_Term_Number['term'];
> echo " ";
> echo $row_Term_Number['year'];
> echo "</td>\n";
> }
>
> echo "</tr>\n"; # closing the table row
>
> } while ($row_Term_Number = mysql_fetch_assoc($Term_Number));
> ?>
>
>> ground. Does any one have any ideas on how this
>> might actually work? Or is it not possible to change it over ? As
>> always, thank you : )
>
> Things are almost always possible.
>
>> <?php
>> if ($row_Term_Number['term_number'] == 0) {
>> echo "<td>" " " "</td>" "<td>" TC/EC "</td>"
>
> To join strings together like you're trying to do here try any of the
> following:
> 1. echo "<td>" . " ". "</td>". "<td>". "TC/EC". "</td>";
> (where the . joins the strings together. Notice that
> we
> have to put TC/EC in quotes as well)
>
> 2. echo "<td> </td><td>TC/EC</td>";
> (where we put everything into one string)
>
> 3. if ($row_Term_Number['term_number'] == 0) {?>
> <td> </td><td>TC/EC</td>
> <?php ...
> (where we use php's embeddedness to pass the quoting
> over to php (but doesn't it look ugly?))
>
>> } else { ?>
>>
>> <tr> </tr>
>> <td><?php echo $row_Term_Number['term_number'];?></td>
>> <td> <?php echo $row_Term_Number['term'];?> <?php echo
>> $row_Term_Number['year'];?></td>
>> <?php } while ($row_Term_Number =
>> mysql_fetch_assoc($Term_Number)); ?>
>
> You don't clode the open curly brace from your else here. You must do
> this. You could do it like this:
>
> } else { ?>
> <tr> </tr>
> <td><?php echo $row_Term_Number['term_number'];?></td>
> <td><?php echo $row_Term_Number['term'];?>
> <?php echo $row_Term_Number['year'];?></td>
> <?php } # End else statement
> } while ($row_Term_Number = mysql_fetch_assoc($Term_Number));?>
>
> All in all I think that once php code gets even moderately complicated
> the correct thing is to move it all into one code block printing out
> your html.
>
> I hope this helps,
>
> Jacinta
>
> --
> ("`-''-/").___..--''"`-._ | Jacinta Richardson |
> `6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia |
> (_Y_.)' ._ ) `._ `. ``-..-' | +613 9354 6001 |
> _..`--'_..-_/ /--'_.' ,' | contact at perltraining.com.au |
> (il),-'' (li),' ((!.-' | www.perltraining.com.au |
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>
More information about the Programming
mailing list