Forum Moderators: coopster

Message Too Old, No Replies

CSV equivalent to CDATA

         

petra

1:24 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



I've created a csv file that has php code in it to populate data from a mysql database.

The output is not what I expected, the fileds are misaligned and I think its because of some charaters in some of the items in the DB.

Is there an equivelent of a CDATA tag I can place in the csv file?

example.csv:

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database.");

$q = "SELECT * FROM _PRODUCTS";
$qe = mysql_query($q);?>
[_PROD_NAME], [_PROD_IMAGE], [_PROD_LINK], [_PROD_DESCRIPTION], [_PROD_PRICE]
<? while($qr = mysql_fetch_array($qe))
{
?>

<?=$qr[_PROD_NAME]?>, <?=$qr[PROD_IMAGE]?>, <?=$qr[_PRODLINK]?>, <?=$qr[_PROD_DESCRIPTION]?>, <?=$qr[PROD_LINK]?>, <?=$qr[PROD_PRICE]?>

<?
}
?>

PHP_Chimp

2:41 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
$q = "SELECT * FROM _PRODUCTS";
$qe = mysql_query($q);
$op = '[_PROD_NAME], [_PROD_IMAGE], [_PROD_LINK], [_PROD_DESCRIPTION], [_PROD_PRICE]'."\n";
while($qr = mysql_fetch_array($qe)) {
$op.= "{$qr[_PROD_NAME]}, {$qr[PROD_IMAGE]}, {$qr[_PROD_LINK]}, {$qr[_PROD_DESCRIPTION]}, {$qr[PROD_LINK]}, {$qr[PROD_PRICE]}\n";
}
echo $op;

The code is almost the same as you posted, however there are explicit \n at the end of lines. You may also want to quote anything that is a string, so that white space doesnt muck up the csv.
So im guessing that the second $op line should be -

$op.= "\"{$qr[_PROD_NAME]}\", \"{$qr[PROD_IMAGE]}\", \"{$qr[_PROD_LINK]}\", \"{$qr[_PROD_DESCRIPTION]}\", \"{$qr[PROD_LINK]}\", \"{$qr[PROD_PRICE]}\"\n";

Assuming you are returning strings for those calls, not numbers.

petra

3:33 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Many thanks for your help PHP_Chimp, the output is still not right. I sen you the link to the file in question, I'd really appreciate it if you could have a look and let me know why its not oputputting correctly.
Thanks again!

PHP_Chimp

4:11 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few problems. There are additional "s around almost all of the fields. So I guess that you have already quoted all of the strings in the database. Although there are a few that dont have the additional "s, so there is something wrong there. There also appear to be areas where you have used ' to quote the string, as there is '' for a blank field, although this could just be a single " in that field.

I suspect that the problem in the formatting lies in your query.


// $q = "SELECT * FROM _PRODUCTS";
// try
$q = "SELECT [_PROD_NAME], [_PROD_IMAGE], [_PROD_LINK], [_PROD_DESCRIPTION], [_PROD_PRICE] FROM _PRODUCTS;";

Of course you will need to change the [FIELD] to be the correct name for each of those fields.

As at the moment you are returning up to 8 fields with your select all statement.

See what just selecting the line you want does for the format.

<edit>
Unrelated to formatting issues -
When I typed the link into another computer to look at the output in windows and got the link address wrong your custom 404 page is not working.
As the response is the browser standard 404 with a warning 'Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request' so your 404 page is not working.

[edited by: PHP_Chimp at 4:15 pm (utc) on Jan. 10, 2008]

petra

5:46 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Many thanks PHP_Chimp!
You've been really helpful!
It works perfectly well now!
thanks again,
petra