Jump to content

explode, for, array help


dennismonsewicz

Recommended Posts

I have the folllowing code:

 

<?php $field = mysql_num_fields($who_qry);
	while($row = mysql_fetch_assoc($who_qry)) {
		for($i = 3; $i < $field; $i++) {
			$names = mysql_field_name($who_qry, $i);
			$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);			
					$title .= str_replace($numbers, '', ucwords($names . ','));
				}						
			}

$excel=new ExcelWriter();
$explode = explode(',', $title);
for($i = 0; $i < count($explode); $i++) {
	$pieces .= $explode[$i] . ',';
}
$trim = trim($pieces, ',');

$myArr=array($trim);
$excel->writeLine($myArr); ?>

 

When it writes to the excel spreadsheet is placing all of the row names on one line :( i am guessing because the names are stored as one var instead of separate pieces of the array? Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/127884-explode-for-array-help/
Share on other sites

why all the moving from comma delimited strings to arrays and back again?

 

And did you really want the column names repeating for every row?

 

Anyway, try

$who_qry = mysql_query ("SELECT * FROM user"); 
$field = mysql_num_fields($who_qry);
while($row = mysql_fetch_assoc($who_qry)) {
    for($i = 3; $i < $field; $i++) {
        $names = mysql_field_name($who_qry, $i);
        $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);         
        $title[] = str_replace($numbers, '', ucwords($names . ','));
    }                  
}

$excel=new ExcelWriter();
foreach ($title as $t)
{
    $excel->writeLine($t); 
}

now its not even printing the names :(

 

updated code:

 

	$who_qry = mysql_query("SELECT * FROM has_had_projects")or die(mysql_error()); 
$field = mysql_num_fields($who_qry);
while($row = mysql_fetch_assoc($who_qry)) {
    for($i = 3; $i < $field; $i++) {
        $names = mysql_field_name($who_qry, $i);
        $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);         
        $title[] = str_replace($numbers, '', ucwords($names . ','));
    }                  
}

$excel=new ExcelWriter();
foreach ($title as $t)
{
    $excel->writeLine($t); 
}

add echo statement

$excel=new ExcelWriter();
foreach ($title as $t)
{
    $excel->writeLine($t);
    echo "$t<br/>";                      // print field name
}

 

I don't know your ExcelWriter class but does it need a filename?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.