Forum Moderators: coopster

Message Too Old, No Replies

PHP inside HTML Tables

Is there a better way of writing this...

         

Francis

8:04 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



Sorry, I am a newbie in PHP and just trying hard to learn.

I have a code below:

<table width="97%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td valign="top" colspan="2">

<?php
$result = mysql_query("SELECT * FROM man_info where id=$id");

if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }

while ( $row = mysql_fetch_array($result) ) {
echo("<H1>" . $row["firstname"] . "&nbsp" . $row["middlename"] . "&nbsp" . $row["lastname"] . "</H1>");
}
?>
</td>
</tr>
<tr>
<td width="43%" valign="top" bgcolor="#FFFFCC">
<p><span class="highlite">Region:</span> <br>
<span class="highlite">Province:</span> &nbsp;<br>
<span class="highlite">District:</span> <b>&nbsp;</b><br>
<span class="highlite">Municipality:</span> &nbsp;</p>
</td>
<td width="57%" valign="top" bgcolor="#FFFFCC">

<?php
$result = mysql_query("SELECT * FROM man_info where id=$id");

if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }

while ( $row = mysql_fetch_array($result) ) {
echo("<p> <b>" . $row["region"] . "</b><br>");
echo("<b>" . $row["province"] . "</b><br>");
echo("<b>" . $row["district"] . "</b><br>");
echo("<b>" . $row["municipality"] . "</b></p>");
}
?>

</td>
</tr>
<tr bgcolor="#FFCC99">
<td colspan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="43%" class="highlite">
<p class="highlite">Party:</p>
</td>
<td valign="top" width="57%">

<?php
$result = mysql_query("SELECT * FROM man_info where id=$id");

if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }

while ( $row = mysql_fetch_array($result) ) {
echo("<p> <b>" . $row["party"] . "</b></p>");
}
?>
</td>
</tr>
<tr>

If you had noticed, I used the same set of commands twice already:

$result = mysql_query("SELECT * FROM man_info where id=$id");

if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }

This is working already but I'm just wondering if there a better way of writing this? Probably just use PHP codes for tables (How do I do that anyway?) or some kind of global variable?

Thanks a lot for the help.

jatar_k

8:43 pm on Apr 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you are going to use it twice then what about loading it into an array the first time so it be reused later on?

Francis

8:49 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



I'll look it up. How do you do that anyway? Thanks.

Birdman

8:53 pm on Apr 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you are only going to return one record, just make one big while loop:


<?php
$result = mysql_query("SELECT * FROM man_info where id=$id");

if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }

while ( $row = mysql_fetch_array($result) ) {
echo("<H1>" . $row["firstname"] . "&nbsp" . $row["middlename"] . "&nbsp" . $row["lastname"] . "</ H1>");
?>
</td>
</tr>
<tr>
<td width="43%" valign="top" bgcolor="#FFFFCC">
<p><span class="highlite">Region:</span> <br>
<span class="highlite">Province:</span> &nbsp;<br>
<span class="highlite">District:</span> <b>&nbsp;</b><br>
<span class="highlite">Municipality:</span> &nbsp;</p>
</td>
<td width="57%" valign="top" bgcolor="#FFFFCC">

<?php
echo("<p> <b>" . $row["region"] . "</b><br>");
echo("<b>" . $row["province"] . "</b><br>");
echo("<b>" . $row["district"] . "</b><br>");
echo("<b>" . $row["municipality"] . "</b></p>");

?>

</td>
</tr>
<tr bgcolor="#FFCC99">
<td colspan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="43%" class="highlite">
<p class="highlite">Party:</p>
</td>
<td valign="top" width="57%">

<?php
echo("<p> <b>" . $row["party"] . "</b></p>");
} //end of loop
?>
</td>
</tr>
<tr>

[edited by: Birdman at 8:54 pm (utc) on April 2, 2003]

jatar_k

8:54 pm on Apr 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use counters because I can't help it, something like this should work

$counter = 0;
while ( $row = mysql_fetch_array($result) ) {
$man_infoarr[$counter] = array($row["region"],$row["province"],$row["district"],$row["municipality"]);
echo("<p> <b>" . $row["region"] . "</b><br>");
echo("<b>" . $row["province"] . "</b><br>");
echo("<b>" . $row["district"] . "</b><br>");
echo("<b>" . $row["municipality"] . "</b></p>");
$counter++;
}

then farther down you can just iterate through your array.

Francis

6:32 pm on Apr 3, 2003 (gmt 0)

10+ Year Member



Hey, thanks guys and gals for all the help I have been getting so far. You have made my life easier. Sometimes, I just spend hours looking for some of my answers here (I like it a lot because I learn from it), but if I'm rushing, I just post and ask. Thanks again for the help and the patience.

nosanity

10:32 pm on Apr 3, 2003 (gmt 0)

10+ Year Member



jatar_k,

everything you write has at least 13 counters! :P then again, im worse for my variable-variables...

J.