Echo results on a new line

Hi All

I am trying to display the results of a query so that an address that contains commas will print each section onto a new line. eg.

8c Telegraph House, 29/35 Moor Lane, Crosby, Liverpool L23 2SF

Would print as

8c Telegraph House,

29/35 Moor Lane,

Crosby,

Liverpool L23 2SF

I appreciate that the database should have been set up as address1, address 2, Town, City, Postcode etc. but unfortunately it wasn’t and I don’t want to have to convert them all now.

The present echo is

echo stripslashes($row['address']);

Can it be done?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

BTW viewed on the web there are s in there that shouldn’t be

echo stripslashes($row[‘address’]);

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Try using preg_replace() to substitute a break for each comma.

$string = preg_replace(‘/,s?/’,‘
’,$string);

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You could also do it all in the query:

SELECT REPLACE(`address`,',','<br />') AS `address` WHERE ...

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Walter

I will give that a go tonight.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

OK Walter I give up

I have tried this every which way but cannot get it to fly. Is it because I am trying to do it with multiple reults

echo stripslashes($row['address']);

How do I get the preg_replace into this line or the SELECT REPLACE for that matter. Or do I have an extra line before/after the echo?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

or a slightly more cumbersome way…

$address_format = str_replace(',',',<br />',stripslashes($row['address']));
echo $address_format;

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

That did it Mike - thanks very much.

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

OK - next question

At the moment my results are displayed in a 1 column table 1 on top of another.

How do I get the results to display over three columns left to right before moving to the second row and repeating.

Current code:

     $row = mysql_fetch_array($result);
echo '<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=5 COLSPAN=1>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style28"><span class="body12VB">';
echo stripslashes($row['company']);
echo '</span></p></td>';
echo '</tr>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style88"><span class="body12V">';
$address_format = str_replace(',',',<br />',stripslashes($row['address']));
echo $address_format;
echo '</span></p></td>';
echo '</tr>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=TOP HEIGHT =12><p class="style88"><span class="body12V">';
echo 'Contact: '.stripslashes($row['contact']);
echo '</td>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style88"><span class="body12V">';
echo 'Tel: '.stripslashes($row['telephone']);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style88"><span class="body12V">';
echo 'Fax: '.stripslashes($row['fax']);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style88"><span class="body12V">';
echo 'Email: <a href="mailto:' . ($row['email']). '">' . ($row['email']) . '</a>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td WIDTH=300 VALIGN=CENTRE HEIGHT =12><p class="style88"><span class="body12V">';
echo 'Web: <a href="http://' . ($row['website']). '"target="_blank">' . ($row['website']) . '</a>';
echo '</td>';
echo '</tr>';
echo '</TABLE>';

TIA David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

There’s two basic ways you can go here, and they have different benefits. If you want the source code to read the same as the page, then do this:

Make one column in your layout, then position floated DIVs inside it to make the “columns”. Then your source can flow in the same order as you would read it on the page, and the code will be mostly free of extra layout.

$count = 0;
$out = '';
while ($row = mysql_fetch_object($result)){
    $count = ($count == 3) ? 0 : $count;
    $out .= '<div class="column">' . print_r($row, true) . '</div>';
    if($count == 2) $out .= '<br class="clearer" />';
    $count ++;
}

Now I’ve deliberately just done a raw dump of the address inside each div to keep things simple in this example. To style this, you would make the class “column” something simple, like 32% width, 1% right margin, and float left; and the class “clearer” float left, 100% width, clear both. The source will spit out in the order you want to read it, and the layout will flow across. Then when you reach the third address, there will be a break tag to start over at the next row. (This isn’t strictly necessary, but it will keep things lining up like rows should your data turn out to be of different lengths.)

The other way to do this is to do the table. You follow the same sort of model as above, but instead of making a single DIV for each address, you make a TD, and at the end of the row, you simply close the TR and open a new one.

$count = 0;
$out = '<table>';
while ($row = mysql_fetch_object($result)){
    $count = ($count == 3) ? 0 : $count;
    if($count == 0) $out .= '<tr>';
    $out .= '<td>' . print_r($row, true) . '</td>';
    if($count == 2) $out .= '</tr>';
    $count ++;
}
if ($count > 0 && $count < 2) $out .= str_repeat('<td></td>', 3 - $count) . '</tr>';
$out .= '</table>';

That last line after the loop is there to make up any difference between the number of columns and your data. You always want to have the same number of cells in your rows all the way down the table.

So either one of these should get your addresses lined up the way you want.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options