PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Sat Nov 11, 2006 7:39 am
At the moment I'm having the results of mysql_fetch_arry output into a table. Because of this I'm finding I'm closing and opening PHP and HTML tags all over the place. This is what my code looks like at the moment:
Code: Select all
$query = mysql_query("SELECT *
FROM customers, jobs
WHERE customers.id = jobs.customerID
");
while($r = mysql_fetch_array($query)) {
?>
<td bgcolor = "CCFFFF">
<?php
echo $r['id'];
echo " ", $r['customer'];
?>
</td>
<?php
}
As you can see there are open and close tags every couple of lines making it looks horrible. Is there a more tidy way to do this?
Regards,
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Nov 11, 2006 8:11 am
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sat Nov 11, 2006 8:12 am
Just echo throughout. Not exactly prettier, but a lot less cluttered.
Code: Select all
<?php
$query = mysql_query("SELECT *
FROM customers, jobs
WHERE customers.id = jobs.customerID
");
while($r = mysql_fetch_array($query)) {
echo '<td bgcolor = "CCFFFF">';
echo $r['id'] ," ", $r['customer'];
echo '</td>';
}
?>
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Sat Nov 11, 2006 9:15 am
It'd never occured to me to use echo, it should've as it's very simple. It's what I was looking for anyway.
Stephen,
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sat Nov 11, 2006 11:20 am
You know, you were using echo, just not for everything.
brendandonhue
Forum Commoner
Posts: 71 Joined: Mon Sep 25, 2006 3:21 pm
Post
by brendandonhue » Sat Nov 11, 2006 11:42 am
You could also use printf() to get all the echos into one line.
Mordred
DevNet Resident
Posts: 1579 Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria
Post
by Mordred » Sat Nov 11, 2006 1:02 pm
Code: Select all
<?php
while($r = mysql_fetch_array($query)) {
echo "<td bgcolor = 'CCFFFF'>{$r['id']} {$r['customer']}</td>";
}
?>