dropbop Posted July 21, 2011 Share Posted July 21, 2011 I have been trying to figure this out for a while and I cant find anything that specificly shows how this is done.. I have a php script that exports certain fields from a mysql database table to a csv file. One of the fields in the table is 'products_image'. From the code below the result is: products_artist, products_title, products_label, products_image Townes Van Zandt, Rain On A Conga Drum, Exile, image.jpg What I would like to show is a full url of the image. Its not showing like that in the database so I would need to be able to add it to the script somehow so it outputs as below: products_artist, products_title, products_label, products_image Townes Van Zandt, Rain On A Conga Drum, Exile, http://www.domainname.com/images/image.jpg <?php $conn = mysql_connect( 'localhost', 'user', 'pass' ) or die( mysql_error( ) ); mysql_select_db( 'database_name', $conn ) or die( mysql_error( $conn ) ); $query = sprintf( 'SELECT products_artist, products_title, products_label, products_image FROM products' ); $result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) ); header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment;filename=export.csv' ); $row = mysql_fetch_assoc( $result ); if ( $row ) { echocsv( array_keys( $row ) ); } while ( $row ) { echocsv( $row ); $row = mysql_fetch_assoc( $result ); } function echocsv( $fields ) { $separator = ''; foreach ( $fields as $field ) { if ( preg_match( '/\\r|\\n|,|"/', $field ) ) { $field = '"' . str_replace( '"', '""', $field ) . '"'; } echo $separator . $field; $separator = ','; } echo "\r\n"; } ?> If anyone can point me in the right direction that would be great. Many Thanks DB Quote Link to comment https://forums.phpfreaks.com/topic/242538-exporting-mysql-to-csv-can-i-add-extra-text-to-a-field/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2011 Share Posted July 21, 2011 Add the following line of code right before you call echocsv( $row ); - $row['products_image'] = 'http://www.domainname.com/images/' . $row['products_image']; Quote Link to comment https://forums.phpfreaks.com/topic/242538-exporting-mysql-to-csv-can-i-add-extra-text-to-a-field/#findComment-1245626 Share on other sites More sharing options...
dropbop Posted July 21, 2011 Author Share Posted July 21, 2011 Thank you PFMaBiSmAd, that's worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/242538-exporting-mysql-to-csv-can-i-add-extra-text-to-a-field/#findComment-1245635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.