Refactoring an array - is something like this possible?

Hi again, still struggling with todays posts and responses. Trying to have a break and think of new areas to break…
I have an array of dates/sales, something like this:

$columns = array(
"year1"  =>array(
"header"=> $year - 10, 
"type"=>"barchart",  
"align"=>"center", 
"width"=>"100px", 
"wrap"=>"nowrap", 
"visible"=>"true", 
"on_js_event"=>"", 
"field"=>"", 
"maximum_value"=>$max_value, 
"display_type"=>"vertical"),

"year2"  =>array(
"header"=> $year - 9, 
"type"=>"barchart",  
"align"=>"center", 
"width"=>"100px", 
"wrap"=>"nowrap", 
"visible"=>"true", 
"on_js_event"=>"", 
"field"=>"", 
"maximum_value"=>$max_value, 
"display_type"=>"vertical"),

"year3"  =>array(
"header"=> $year - 8, 
"type"=>"barchart",  
"align"=>"center", 
"width"=>"100px", 
"wrap"=>"nowrap", 
"visible"=>"true", 
"on_js_event"=>"", 
"field"=>"", 
"maximum_value"=>$max_value, 
"display_type"=>"vertical"),
.....

I have to repeat these 12-15 times for each report, and the columns are pretty static. A programmer will be needed if a user wants a different date range…

Is it possible to do some thing like this (php and mySQL)…

$vm_columns = array(  
for ($y= (date(Y)-10); $y <= (date(Y)+1); ++$y)
{
"year"  =>array(
"header"=> $i, 
"type"=>"barchart",  
"align"=>"center", 
"width"=>"100px", 
"wrap"=>"nowrap", 
"visible"=>"true", 
"on_js_event"=>"", 
"field"=>"", 
"maximum_value"=>$max_value, 
"display_type"=>"vertical");
}

Try this, it will output a little better than wasting an extra array inside…

Here are a few tips for future reference :slight_smile:

  1. Always use single quotes, unless you are parsing a variable, for example:
echo "My name is $name" // This is good since there is a variable inside
echo "My name is James" // This is bad since there is nothing special to parse.

Otherwise PHP will waste time parsing “double quoted” strings when it doesn’t need to.

  1. Define your Variables before the for() loop, not inside of it. Of course small loops are fine, because if you passed a date() function in a for loop, everytime it counted up one, PHP has to reprocess the date() function every loop, so that’d be ten loops returning the same date() value and wasting resources.

<?php

# Todays Year (2010)
$yrNow = date('Y');

# Ten years ago (2000)
$yrMinusTen =  $yrNow - 10;

# Placement value beside the [year] inside the array
# Start at 0 not 1, because year0 would be 2000, and [year1] would be confusing, [year0] looks more like 2000
$i = 0;

# Loop through it.
for ($y = $yrMinusTen; $y <= $yrNow; $y++)
{
	$vm_columns['year'.$i++] = array(
		'header'	=> 'blah', 
		'type'		=>'barchart',  
		'align'		=>'center', 
		'width'		=>'100px', 
		'wrap'		=>'nowrap', 
		'visible'	=>'true', 
		'on_js_event'=>'', 
		'field'		=> '', 
		'maximum_value'=> 'MAX VALUE VAR HERE', 
		'display_type'=>'vertical'
	);
}

echo '<pre>';
print_r($vm_columns);

Got both of them!!!

I had an extra loop:

"year" =>array(

I took that out, all is now manageable…

THANKS A TON

YAY!!! haha, it looks pretty good too nice little GUI there

This is really helpful… I’m almost there!!!

This is my code so far:

$max_value = "3000";

# Todays Year (2010)
$yrNow = date('Y');

# Ten years ago (2000)
$yrMinusTen =  $yrNow - 10;

# Placement value beside the [year] inside the array
# Start at 0 not 1, because year0 would be 2000, and [year1] would be confusing, [year0] looks more like 2000
$i = 0;

# Loop through it.
for ($y = $yrMinusTen; $y <= $yrNow+2; $y++)
{ 
$vm_columns['year'.$i++] = array(
  
"year" =>array(
"header"=> ($yrMinusTen + $i), 
"type"=>"barchart",  
"align"=>"center", 
"width"=>"100px", 
"wrap"=>"nowrap", 
"visible"=>"true", 
"on_js_event"=>"", 
"field"=>"", 
"maximum_value"=>$max_value, 
"display_type"=>"vertical")
);
}

( there are some other grid components that are put together before the whole thing is printed)

The results look like the first screenshot. What I need is the second… Two things are still off. First - I’d like to have the names of the years (eg 2001, 2002, etc, and second the height of the table is off…

Many thanks…

David