Can someone show me a tidier way to output mysql_fetch_array

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

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Can someone show me a tidier way to output mysql_fetch_array

Post by impulse() »

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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using echo()?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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() »

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,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know, you were using echo, just not for everything. :wink:
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

You could also use printf() to get all the echos into one line.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Code: Select all

<?php 
while($r = mysql_fetch_array($query)) { 
    echo "<td bgcolor = 'CCFFFF'>{$r['id']} {$r['customer']}</td>";
} 
?>
Post Reply