Page 1 of 1

Can someone show me a tidier way to output mysql_fetch_array

Posted: Sat Nov 11, 2006 7:39 am
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,

Posted: Sat Nov 11, 2006 8:11 am
by feyd
using echo()?

Posted: Sat Nov 11, 2006 8:12 am
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>';
} 
?>

Posted: Sat Nov 11, 2006 9:15 am
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,

Posted: Sat Nov 11, 2006 11:20 am
by RobertGonzalez
You know, you were using echo, just not for everything. :wink:

Posted: Sat Nov 11, 2006 11:42 am
by brendandonhue
You could also use printf() to get all the echos into one line.

Posted: Sat Nov 11, 2006 1:02 pm
by Mordred

Code: Select all

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