Writing to .csv

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
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Writing to .csv

Post by tomnoble »

Hi All,

I am using the following code to write to a .csv. Unfortunately, it is not actually writing anything in and my array is not working.

Any ideas/help/code corrections would be greatly appreciated:

Code: Select all

<?php 

//Open DB and file connections
mysql_connect("localhost", "dbname", "dbpass") or die("Connection Failed");
mysql_select_db("dbname")or die("Connection Failed");
$file = fopen("Product_Database.csv","w");  

$Position= 0;
$line[1][$Position]=".|";
//Load in all the NID's
$result = mysql_query("SELECT nid FROM drupal_node WHERE type = 'product'"); //just get nid not *

while($row = mysql_fetch_array( $result ))
  {
  $info = $row['nid'];
  
	$revisions = mysql_query("SELECT title, body FROM 	drupal_node_revisions WHERE nid = '$info'"); 
	$rowrevise = mysql_fetch_array( $revisions );
	$line[2][$Position]= $rowrevise['title'];
	$line[3][$Position]= $rowrevise['body'];

	$skucode = mysql_query("SELECT model FROM 	drupal_uc_products WHERE nid = '$info'");
	$sku = mysql_fetch_array( $skucode );
	$line[0][$Position]= $sku['model'];

	$prices = mysql_query("SELECT country_id, sell_price FROM 	drupal_uc_multigeog WHERE nid = '$info'");
	
	while($cost = mysql_fetch_array( $prices ))
	  {
		If ($cost['country_id'] = 930)
		  $line[4][$Position]= $cost['sell_price'];
		If ($cost['country_id'] = 900)
		  $line[5][$Position]= $cost['sell_price'];
		If ($cost['country_id'] = 915)
		  $line[6][$Position]= $cost['sell_price'];
		If ($cost['country_id'] = 946)
		  $line[7][$Position]= $cost['sell_price'];
		If ($cost['country_id'] = 960)
		  $line[8][$Position]= $cost['sell_price'];  
	  }
	
	$taxid = mysql_query("SELECT tid FROM 	
	drupal_term_node WHERE nid = '$info'");

	while($tax = mysql_fetch_array( $taxid ))
	  {
		$id = $tax['tid'];
		$taxname = mysql_query("SELECT description FROM 	
		drupal_term_data WHERE tid = '$id'");
		$taxdesc = mysql_fetch_array( $taxname );
		$line[1][$Position]= ($line[1][$Position]) . "|" . ($taxdesc['description']);
	  }

if(substr('$line[1][$Position]', 0, 2) == '.|')
{
   $line[1][$Position] = substr($line[1][$Position], 2);
}
fputcsv($file,split(',',$line));
$Position ++;

  }

print_r($line);

/*
//Write the data to the .csv file and print out individual lines
foreach ($list as $line)
  {
  fputcsv($file,split(',',$line));
  }
*/
print_r($line);
//Close DB and file modelconnections
mysql_close(mysql_connect("localhost", "dbname", "dbpass"));
fclose($file);

//header('Location:http://domain/pdm');

?>
I know all of the data is being read into through the queries correctly.

Kind Regards

Tom
Post Reply