Shorten code using loops/arrays?
Posted: Mon Feb 18, 2008 2:43 pm
I'm creating a report from data submitted in a form. There are several sections that are virtually the same, but with different data - so I'm wondering if there's a way to shorted my code a bit rather than duplicate so much throughout the page.
For example, I have 5 "topics" each of which has data for 5 "images" (how often they were chosen).
I've called the image data $ia1, $ib1, $ic1, $id1, $ie1... $ia5, $ib5, $ic5, $id5, $ie5 -- (these are raw counts ranging in value from 0 to 4, and get converted to percentages as needed).
Basically, the first letter lets me know it's an image count variable, the second letter tells me which image, and the number tells me which topic.
So I'm creating charts, hover text, and feedback text that is conditional based on these variables.
Here's an example of some code for one topic's feedback area:
What I've ended up doing is recreating that same code for each topic, just changing the topic number in the variables each time (e.g., $ia1 changes to $ia2, $epitomizing1 changes to $epitomizing2, and so on). (Variables in the above code that don't have a number at the end are the same for every topic, so they don't change, and their values are set elsewhere.)
My question is whether there's a way to simplify/shorten this using arrays and loops, since the code is identical for each topic other than the variable name for the instance?
Thanks,
Scott
For example, I have 5 "topics" each of which has data for 5 "images" (how often they were chosen).
I've called the image data $ia1, $ib1, $ic1, $id1, $ie1... $ia5, $ib5, $ic5, $id5, $ie5 -- (these are raw counts ranging in value from 0 to 4, and get converted to percentages as needed).
Basically, the first letter lets me know it's an image count variable, the second letter tells me which image, and the number tells me which topic.
So I'm creating charts, hover text, and feedback text that is conditional based on these variables.
Here's an example of some code for one topic's feedback area:
Code: Select all
//CREATE REPORT AREA FOR PICTURE CHOICES FOR TOPIC 1
//SET HOVER TEXT FOR EPITOMIZING AND ANTITHETICAL PICTURES BASE (needs to be reset for each topic?)
$hover_a = "Neither epitomizing nor antithetical";
$hover_b = "Neither epitomizing nor antithetical";
$hover_c = "Neither epitomizing nor antithetical";
$hover_d = "Neither epitomizing nor antithetical";
$hover_e = "Neither epitomizing nor antithetical";
//ALTER HOVER TEXT FOR PICTURES THAT WERE EPITOMIZING OR ANTITHETICAL (CHOSEN MOST OR LEAST OFTEN)
$epitomizing1 = max(array($ia1 / 4 * 100,$ib1 / 4 * 100,$ic1 / 4 * 100,$id1 / 4 * 100,$ie1 / 4 * 100));
if($epitomizing1 == $ia1 / 4 * 100){
$hover_a = 'Epitomzing picture chosen';
}
if($epitomizing1 == $ib1 / 4 * 100){
$hover_b = 'Epitomzing picture chosen';
}
if($epitomizing1 == $ic1 / 4 * 100){
$hover_c = 'Epitomzing picture chosen';
}
if($epitomizing1 == $id1 / 4 * 100){
$hover_d = 'Epitomzing picture chosen';
}
if($epitomizing1 == $ie1 / 4 * 100){
$hover_e = 'Epitomzing picture chosen';
}
$antithetical1 = min(array($ia1 / 4 * 100,$ib1 / 4 * 100,$ic1 / 4 * 100,$id1 / 4 * 100,$ie1 / 4 * 100));
if($antithetical1 == $ia1 / 4 * 100){
$hover_a = 'Antithetical picture chosen';
}
if($antithetical1 == $ib1 / 4 * 100){
$hover_b = 'Antithetical picture chosen';
}
if($antithetical1 == $ic1 / 4 * 100){
$hover_c = 'Antithetical picture chosen';
}
if($antithetical1 == $id1 / 4 * 100){
$hover_d = 'Antithetical picture chosen';
}
if($antithetical1 == $ie1 / 4 * 100){
$hover_e = 'Antithetical picture chosen';
}
//CREATE CHART OF PICTURE CHOICE PERCENTAGES
//create Column3D chart
$EPIT1 = new FusionCharts("Column3D","600","400");
//set the relative path for the swf
$EPIT1->setSWFPath("FCharts/FusionCharts/");
//set chart attributes
$strParam="caption=Epitomizing Picture for Topic 1;yAxisName=%25 times chosen;yAxisMinValue=0;yAxisMaxValue=100;decimalPrecision=0;formatNumberScale=5;numberSuffix=%25;numdivlines=3;canvasBgColor=eeeeee;canvasBaseColor=eeeeee";
$EPIT1->setChartParams($strParam);
//add chart values
$EPIT1->addChartData($ia1 / 4 * 100,"name=Image A;alpha=75;hoverText=".$hover_a);
$EPIT1->addChartData($ib1 / 4 * 100,"name=Image B;alpha=75;hoverText=".$hover_b);
$EPIT1->addChartData($ic1 / 4 * 100,"name=Image C;alpha=75;hoverText=".$hover_c);
$EPIT1->addChartData($id1 / 4 * 100,"name=Image D;alpha=75;hoverText=".$hover_d);
$EPIT1->addChartData($ie1 / 4 * 100,"name=Image E;alpha=75;hoverText=".$hover_e);
echo '<div class="chart">';
//RENDER THE CHART
$EPIT1->renderChart();
echo '<br />';
echo '<div class="charttxt">';
//DISPLAY EPITOMIZING IMAGE(S) & DESCRIPTIONS
if($epitomizing1 == $ia1 / 4 * 100){
echo $image_a.''.$epit_text.' for '.$_POST['topic1'].', chosen '.$epitomizing.$presented.' '.$descrip_a.$_POST['topic1'].'" like that?</div>';
}
if($epitomizing1 == $ib1 / 4 * 100){
echo $image_b.' '.$epit_text.' for '.$_POST['topic1'].', chosen '.$epitomizing.$presented.' '.$descrip_b.$_POST['topic1'].'" like that?</div>';
}
if($epitomizing1 == $ic1 / 4 * 100){
echo $image_c.' '.$epit_text.' for '.$_POST['topic1'].', chosen '.$epitomizing.$presented.' '.$descrip_c.$_POST['topic1'].'" like that?</div>';
}
if($epitomizing1 == $id1 / 4 * 100){
echo $image_d.' '.$epit_text.' for '.$_POST['topic1'].', chosen '.$epitomizing.$presented.' '.$descrip_d.$_POST['topic1'].'" like that?</div>';
}
if($epitomizing1 == $ie1 / 4 * 100){
echo $image_e.' '.$epit_text.' for '.$_POST['topic1'].', chosen '.$epitomizing.$presented.' '.$descrip_e.$_POST['topic1'].'" like that?</div>';
}
//DISPLAY ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical1 == $ia1 / 4 * 100){
echo $image_a.' '.$anti_text.' for '.$_POST['topic1'].', chosen '.$antithetical.$presented.' '.$descrip_a.$_POST['topic1'].'" not like that?</div>';
}
if($antithetical1 == $ib1 / 4 * 100){
echo $image_b.' '.$anti_text.' for '.$_POST['topic1'].', chosen '.$antithetical.$presented.' '.$descrip_b.$_POST['topic1'].'" not like that?</div>';
}
if($antithetical1 == $ic1 / 4 * 100){
echo $image_c.' '.$anti_text.' for '.$_POST['topic1'].', chosen '.$antithetical.$presented.' '.$descrip_c.$_POST['topic1'].'" not like that?</div>';
}
if($antithetical1 == $id1 / 4 * 100){
echo $image_d.' '.$anti_text.' for '.$_POST['topic1'].', chosen '.$antithetical.$presented.' '.$descrip_d.$_POST['topic1'].'" not like that?</div>';
}
if($antithetical1 == $ie1 / 4 * 100){
echo $image_e.' '.$anti_text.' for '.$_POST['topic1'].', chosen '.$antithetical.$presented.' '.$descrip_e.$_POST['topic1'].'" not like that?</div>';
}
echo "</div></div>";
//END REPORT SECTION FOR TOPIC 1
My question is whether there's a way to simplify/shorten this using arrays and loops, since the code is identical for each topic other than the variable name for the instance?
Thanks,
Scott