Page 1 of 1

Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 2:43 pm
by Sinemacula
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:

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
 
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

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 3:08 pm
by Christopher
Instead of $hover_a, $hover_b, ... and $ia1, $ib1, ... use arrays $hover[0], $hover[1], ... and $i1[0], $i1[1], ... Then you can easily loop.

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 3:51 pm
by Sinemacula
Okay, so based on your reply, I've looked over some other sample code I have, and the php manual... and I think I'm heading down the right path.

I'm sure this isn't exactly right, but I think it's getting close:

Code: Select all

 
//set variable for the five topics
$topic = array($_POST['topic1'],$_POST['topic2'],$_POST['topic3'],$_POST['topic4'],$_POST['topic5']);
 
//set the start of the loop for the topics
for ($i=1; $i<6; $i++) {
 
$epitomizing[] = max(array($ia[] / 4 * 100,$ib[] / 4 * 100,$ic[] / 4 * 100,$id[] / 4 * 100,$ie[] / 4 * 100));
 
$antithetical[] = min(array($ia[] / 4 * 100,$ib[] / 4 * 100,$ic[] / 4 * 100,$id[] / 4 * 100,$ie[] / 4 * 100));
 
if($epitomizing[] == $ia[] / 4 * 100){
    $hover_a[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ia[] / 4 * 100) {
    $hover_a[] = "Antithetical picture chosen";
} else {
    $hover_a[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ib[] / 4 * 100){
    $hover_b[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ib[] / 4 * 100) {
    $hover_b[] = "Antithetical picture chosen";
} else {
    $hover_b[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ic[] / 4 * 100){
    $hover_c[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ic[] / 4 * 100) {
    $hover_c[] = "Antithetical picture chosen";
} else {
    $hover_c[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $id[] / 4 * 100){
    $hover_d[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $id[] / 4 * 100) {
    $hover_d[] = "Antithetical picture chosen";
} else {
    $hover_d[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ie[] / 4 * 100){
    $hover_e[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ie[] / 4 * 100) {
    $hover_e[] = "Antithetical picture chosen";
} else {
    $hover_e[] = "Neither empitomizing nor antithetical";
}
 
 
//create Column3D chart
$EPIT[] = new FusionCharts("Column3D","600","400");
//set the relative path for the swf
$EPIT[]->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";
$EPIT[]->setChartParams($strParam);
 
 
//add chart values
$EPIT[]->addChartData($ia[] / 4 * 100,"name=Image A;alpha=75;hoverText=".$hover_a[]);
$EPIT[]->addChartData($ib[] / 4 * 100,"name=Image B;alpha=75;hoverText=".$hover_b[]);
$EPIT[]->addChartData($ic[] / 4 * 100,"name=Image C;alpha=75;hoverText=".$hover_c[]);
$EPIT[]->addChartData($id[] / 4 * 100,"name=Image D;alpha=75;hoverText=".$hover_d[]);
$EPIT[]->addChartData($ie[] / 4 * 100,"name=Image E;alpha=75;hoverText=".$hover_e[]);
 
echo '<div class="chart">';
$EPIT[]->renderChart();
echo '<br />';
echo '<div class="charttxt">';
 
//GET EPITOMIZING IMAGE(S) & DESCRIPTIONS
 
if($epitomizing[] == $ia[] / 4 * 100){
echo $image_a[].''.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_a.$topic[].'" like that?</div>';
}
if($epitomizing == $ib1 / 4 * 100){
echo $image_b[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_b.$topic[].'" like that?</div>';
}
if($epitomizing == $ic1 / 4 * 100){
echo $image_c[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_c.$topic[].'" like that?</div>';
}
if($epitomizing == $id1 / 4 * 100){
echo $image_d[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_d.$topic[].'" like that?</div>';
}
if($epitomizing == $ie1 / 4 * 100){
echo $image_e[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_e.$topic[].'" like that?</div>';
}
//GET ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical == $ia1 / 4 * 100){
echo $image_a[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_a.$topic[].'" not like that?</div>';
}
if($antithetical == $ib1 / 4 * 100){
echo $image_b[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_b.$topic[].'" not like that?</div>';
}
if($antithetical == $ic1 / 4 * 100){
echo $image_c[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_c.$topic[].'" not like that?</div>';
}
if($antithetical == $id1 / 4 * 100){
echo $image_d[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_d.$topic[].'" not like that?</div>';
}
if($antithetical == $ie1 / 4 * 100){
echo $image_e[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_e.$topic[].'" not like that?</div>';
}
echo "</div></div>";
 
} //return to top to loop and recreate for each topic
Is this the right approach?

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 4:09 pm
by Sinemacula
I found some missing "[]" in that, and some other little things... still not working yet, though.

Here's the whole script I'm working with - just added some values to the variables at the beginning in order to get it working, then I'll go on to trying it with the form passing the values in.

Code: Select all

<?php
include('Class/FusionCharts_Gen.php');
?>
<div class="section" id="epitomizing">
<h3>Epitomizing Picture Scores</h3>
<div class="info">The graphs below show how many times each image was chosen for each topic, as a percentage of the number of times the image was displayed for the topic.</div>
<div class="info">Which picture did you choose the most for each topic? Which did you choose the least? In what ways is the "epitomizing" picture like the topic? In what ways is the "antithetical" image different from the topic?</div>
 
<?php
$topic = array('topic1','topic2','topic3','topic4','topic5');
$ia = array(4,3,3,2,1);
$ib = array(3,4,2,2,2);
$ic = array(1,1,3,2,3);
$id = array(1,1,0,2,4);
$ie = array(1,1,2,2,0);
 
//SET TEXT FOR THE EPITOMIZING & ANTITHETICAL PICTURES AREA FOR ALL CHARTS
 
$image_a = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_a.png" width="150px"></span> Image A was the';
$image_b = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_b.png" width="150px"></span> Image B was the';
$image_c = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_c.png" width="150px"></span> Image C was the';
$image_d = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_d.png" width="150px"></span> Image D was the';
$image_e = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_e.png" width="150px"></span> Image E was the';
$epit_text = 'Epitomizing Picture';
$anti_text = 'Antithetical Picture';
$presented = '% of the times it was presented.<br />';
$descrip_a = 'Your description of Image A was:<blockquote>'.$_POST['desc_a'].'</blockquote>In what ways is "';
$descrip_b = 'Your description of Image B was:<blockquote>'.$_POST['desc_b'].'</blockquote>In what ways is "';
$descrip_c = 'Your description of Image C was:<blockquote>'.$_POST['desc_c'].'</blockquote>In what ways is "';
$descrip_d = 'Your description of Image D was:<blockquote>'.$_POST['desc_d'].'</blockquote>In what ways is "';
$descrip_e = 'Your description of Image E was:<blockquote>'.$_POST['desc_e'].'</blockquote>In what ways is "';
 
//set the start of the loop for the topics
for($i=1; $i<6; $i++) {
 
$epitomizing[] = max(array($ia[] / 4 * 100,$ib[] / 4 * 100,$ic[] / 4 * 100,$id[] / 4 * 100,$ie[] / 4 * 100));
 
$antithetical[] = min(array($ia[] / 4 * 100,$ib[] / 4 * 100,$ic[] / 4 * 100,$id[] / 4 * 100,$ie[] / 4 * 100));
 
if($epitomizing[] == $ia[] / 4 * 100){
    $hover_a[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ia[] / 4 * 100) {
    $hover_a[] = "Antithetical picture chosen";
} else {
    $hover_a[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ib[] / 4 * 100){
    $hover_b[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ib[] / 4 * 100) {
    $hover_b[] = "Antithetical picture chosen";
} else {
    $hover_b[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ic[] / 4 * 100){
    $hover_c[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ic[] / 4 * 100) {
    $hover_c[] = "Antithetical picture chosen";
} else {
    $hover_c[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $id[] / 4 * 100){
    $hover_d[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $id[] / 4 * 100) {
    $hover_d[] = "Antithetical picture chosen";
} else {
    $hover_d[] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[] == $ie[] / 4 * 100){
    $hover_e[] = "Epitomizing picture chosen";
} elseif($antithetical[] == $ie[] / 4 * 100) {
    $hover_e[] = "Antithetical picture chosen";
} else {
    $hover_e[] = "Neither empitomizing nor antithetical";
}
 
 
//create Column3D chart
$EPIT[] = new FusionCharts("Column3D","600","400");
//set the relative path for the swf
$EPIT[]->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";
$EPIT[]->setChartParams($strParam);
 
 
//add chart values
$EPIT[]->addChartData($ia[] / 4 * 100,"name=Image A;alpha=75;hoverText=".$hover_a[]);
$EPIT[]->addChartData($ib[] / 4 * 100,"name=Image B;alpha=75;hoverText=".$hover_b[]);
$EPIT[]->addChartData($ic[] / 4 * 100,"name=Image C;alpha=75;hoverText=".$hover_c[]);
$EPIT[]->addChartData($id[] / 4 * 100,"name=Image D;alpha=75;hoverText=".$hover_d[]);
$EPIT[]->addChartData($ie[] / 4 * 100,"name=Image E;alpha=75;hoverText=".$hover_e[]);
 
echo '<div class="chart">';
$EPIT[]->renderChart();
echo '<br />';
echo '<div class="charttxt">';
 
//GET EPITOMIZING IMAGE(S) & DESCRIPTIONS
 
if($epitomizing[] == $ia[] / 4 * 100){
echo $image_a[].''.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_a.$topic[].'" like that?</div>';
}
if($epitomizing[] == $ib[] / 4 * 100){
echo $image_b[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_b.$topic[].'" like that?</div>';
}
if($epitomizing[] == $ic[] / 4 * 100){
echo $image_c[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_c.$topic[].'" like that?</div>';
}
if($epitomizing[] == $id[] / 4 * 100){
echo $image_d[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_d.$topic[].'" like that?</div>';
}
if($epitomizing[] == $ie[] / 4 * 100){
echo $image_e[].' '.$epit_text.' for '.$topic[].', chosen '.$epitomizing.$presented.' '.$descrip_e.$topic[].'" like that?</div>';
}
//GET ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical[] == $ia[] / 4 * 100){
echo $image_a[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_a.$topic[].'" not like that?</div>';
}
if($antithetical[] == $ib[] / 4 * 100){
echo $image_b[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_b.$topic[].'" not like that?</div>';
}
if($antithetical[] == $ic[] / 4 * 100){
echo $image_c[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_c.$topic[].'" not like that?</div>';
}
if($antithetical[] == $id[] / 4 * 100){
echo $image_d[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_d.$topic[].'" not like that?</div>';
}
if($antithetical[] == $ie[] / 4 * 100){
echo $image_e[].' '.$anti_text.' for '.$topic[].', chosen '.$antithetical.$presented.' '.$descrip_e.$topic[].'" not like that?</div>';
}
echo "</div></div>";
 
} //return to top to loop and recreate for each topic
 
?>

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 4:29 pm
by Sinemacula
Yeah, me again... but this time... I got it working!!! :D

Thanks for pointing me in the right direction!

Here's the code, in case any other neophytes are looking to solve a similar problem:

Code: Select all

<?php
include('Class/FusionCharts_Gen.php');
?>
<html>
<head>
<script language='javascript' src='FusionCharts/FusionCharts.js'></script>
</head>
<body>
<div class="section" id="epitomizing">
<h3>Epitomizing Picture Scores</h3>
<div class="info">The graphs below show how many times each image was chosen for each topic, as a percentage of the number of times the image was displayed for the topic.</div>
<div class="info">Which picture did you choose the most for each topic? Which did you choose the least? In what ways is the "epitomizing" picture like the topic? In what ways is the "antithetical" image different from the topic?</div>
 
<?php
$topic = array('topic1','topic2','topic3','topic4','topic5');
$ia = array(4,3,3,2,1);
$ib = array(3,4,2,2,2);
$ic = array(1,1,3,2,3);
$id = array(1,1,0,2,4);
$ie = array(1,1,2,2,0);
 
//SET TEXT FOR THE EPITOMIZING & ANTITHETICAL PICTURES AREA FOR ALL CHARTS
 
$image_a = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_a.png" width="150px"></span> Image A was the';
$image_b = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_b.png" width="150px"></span> Image B was the';
$image_c = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_c.png" width="150px"></span> Image C was the';
$image_d = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_d.png" width="150px"></span> Image D was the';
$image_e = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_e.png" width="150px"></span> Image E was the';
$epit_text = 'Epitomizing Picture';
$anti_text = 'Antithetical Picture';
$presented = '% of the times it was presented.<br />';
$descrip_a = 'Your description of Image A was:<blockquote>'.$_POST['desc_a'].'</blockquote>In what ways is "';
$descrip_b = 'Your description of Image B was:<blockquote>'.$_POST['desc_b'].'</blockquote>In what ways is "';
$descrip_c = 'Your description of Image C was:<blockquote>'.$_POST['desc_c'].'</blockquote>In what ways is "';
$descrip_d = 'Your description of Image D was:<blockquote>'.$_POST['desc_d'].'</blockquote>In what ways is "';
$descrip_e = 'Your description of Image E was:<blockquote>'.$_POST['desc_e'].'</blockquote>In what ways is "';
 
//set the start of the loop for the topics
for($i=1; $i<6; $i++) {
 
$epitomizing[$i] = max(array($ia[$i] / 4 * 100,$ib[$i] / 4 * 100,$ic[$i] / 4 * 100,$id[$i] / 4 * 100,$ie[$i] / 4 * 100));
 
$antithetical[$i] = min(array($ia[$i] / 4 * 100,$ib[$i] / 4 * 100,$ic[$i] / 4 * 100,$id[$i] / 4 * 100,$ie[$i] / 4 * 100));
 
if($epitomizing[$i] == $ia[$i] / 4 * 100){
    $hover_a[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ia[$i] / 4 * 100) {
    $hover_a[$i] = "Antithetical picture chosen";
} else {
    $hover_a[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ib[$i] / 4 * 100){
    $hover_b[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ib[$i] / 4 * 100) {
    $hover_b[$i] = "Antithetical picture chosen";
} else {
    $hover_b[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ic[$i] / 4 * 100){
    $hover_c[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ic[$i] / 4 * 100) {
    $hover_c[$i] = "Antithetical picture chosen";
} else {
    $hover_c[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $id[$i] / 4 * 100){
    $hover_d[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $id[$i] / 4 * 100) {
    $hover_d[$i] = "Antithetical picture chosen";
} else {
    $hover_d[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ie[$i] / 4 * 100){
    $hover_e[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ie[$i] / 4 * 100) {
    $hover_e[$i] = "Antithetical picture chosen";
} else {
    $hover_e[$i] = "Neither empitomizing nor antithetical";
}
 
 
//create Column3D chart
$EPIT[$i] = new FusionCharts("Column3D","600","400");
//set the relative path for the swf
$EPIT[$i]->setSWFPath("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";
$EPIT[$i]->setChartParams($strParam);
 
 
//add chart values
$EPIT[$i]->addChartData($ia[$i] / 4 * 100,"name=Image A;alpha=75;hoverText=".$hover_a[$i]);
$EPIT[$i]->addChartData($ib[$i] / 4 * 100,"name=Image B;alpha=75;hoverText=".$hover_b[$i]);
$EPIT[$i]->addChartData($ic[$i] / 4 * 100,"name=Image C;alpha=75;hoverText=".$hover_c[$i]);
$EPIT[$i]->addChartData($id[$i] / 4 * 100,"name=Image D;alpha=75;hoverText=".$hover_d[$i]);
$EPIT[$i]->addChartData($ie[$i] / 4 * 100,"name=Image E;alpha=75;hoverText=".$hover_e[$i]);
 
echo '<div class="chart">';
$EPIT[$i]->renderChart();
echo '<br />';
echo '<div class="charttxt">';
 
//GET EPITOMIZING IMAGE(S) & DESCRIPTIONS
 
if($epitomizing[$i] == $ia[$i] / 4 * 100){
echo $image_a[$i].''.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing.$presented.' '.$descrip_a.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ib[$i] / 4 * 100){
echo $image_b[$i].' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing.$presented.' '.$descrip_b.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ic[$i] / 4 * 100){
echo $image_c[$i].' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing.$presented.' '.$descrip_c.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $id[$i] / 4 * 100){
echo $image_d[$i].' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing.$presented.' '.$descrip_d.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ie[$i] / 4 * 100){
echo $image_e[$i].' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing.$presented.' '.$descrip_e.$topic[$i].'" like that?</div>';
}
//GET ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical[$i] == $ia[$i] / 4 * 100){
echo $image_a[$i].' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical.$presented.' '.$descrip_a.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ib[$i] / 4 * 100){
echo $image_b[$i].' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical.$presented.' '.$descrip_b.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ic[$i] / 4 * 100){
echo $image_c[$i].' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical.$presented.' '.$descrip_c.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $id[$i] / 4 * 100){
echo $image_d[$i].' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical.$presented.' '.$descrip_d.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ie[$i] / 4 * 100){
echo $image_e[$i].' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical.$presented.' '.$descrip_e.$topic[$i].'" not like that?</div>';
}
echo "</div></div>";
 
} //return to top to loop and recreate for each topic
 
?>
</body>
</html>
No I just need to re-integrate it with the form and I'll be good to go!

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 4:31 pm
by Christopher
No, I don't think you are completly understanding arrays. You can do:

Code: Select all

if($epitomizing[] == $ia[] / 4 * 100){
because the [] is the operator to append an element to the end of the array.

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 4:48 pm
by Sinemacula
Well, I do think it's pretty safe to say that I don't completely understand arrays.

That said, when I put the $i inside the [] it worked, but without it, it didn't.

So, I'm not sure where to go from there.

Perhaps I'm confused because I'm needing to create the array via a loop? So I'm trying to do both at the same time?

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 6:10 pm
by Christopher
Ok ... your code so confused me that I cleaned it up just to see where to start. ;) Here is a version converted to arrays. I initialized all the variables and creates a mock class so I could run it without errors. I don't know what comes before this code. See if you can take this an convert it to loops now.

Code: Select all

<?php
// mock class
class FusionCharts {
    public function setSWFPath() {}
    public function setChartParams() {}
    public function addChartData() {}
    public function renderChart() {}
}
 
// get sanitized input
$topic1 = isset($_POST['topic1']) ? preg_replace('/[^a-zA-Z0-9\ \-]/', '', $_POST['topic1']) : '*NO TOPIC*';
 
// initialize variables
$epit_text = '*EPITOMIZING TEXT*';
$epitomizing = '*EPITOMIZING*';
$presented = '*PRESENTED*';
$antithetical = '*ANTITHETICAL*';
$anti_text = '*ANTITHETICAL TEXT*';
 
$i1[0] = 0;
$i1[1] = 0;
$i1[2] = 0;
$i1[3] = 0;
$i1[4] = 0;
 
$i1_calculated[0] = $i1[0] / 4 * 100;
$i1_calculated[1] = $i1[1] / 4 * 100;
$i1_calculated[2] = $i1[2] / 4 * 100;
$i1_calculated[3] = $i1[3] / 4 * 100;
$i1_calculated[4] = $i1[4] / 4 * 100;
 
$image[0] = '';
$image[1] = '';
$image[2] = '';
$image[3] = '';
$image[4] = '';
 
$descrip[0] = '';
$descrip[1] = '';
$descrip[2] = '';
$descrip[3] = '';
$descrip[4] = '';
 
//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[0] = "Neither epitomizing nor antithetical";
$hover[1] = "Neither epitomizing nor antithetical";
$hover[2] = "Neither epitomizing nor antithetical";
$hover[3] = "Neither epitomizing nor antithetical";
$hover[4] = "Neither epitomizing nor antithetical";
 
//ALTER HOVER TEXT FOR PICTURES THAT WERE EPITOMIZING OR ANTITHETICAL (CHOSEN MOST OR LEAST OFTEN)
 
$epitomizing1 = max(array($i1_calculated[0],$i1_calculated[1],$i1_calculated[2],$i1_calculated[3],$i1_calculated[4]));
if($epitomizing1 == $i1_calculated[0]){
    $hover[0] = 'Epitomzing picture chosen';
}
if($epitomizing1 == $i1_calculated[1]){
    $hover[1] = 'Epitomzing picture chosen';
}
if($epitomizing1 == $i1_calculated[2]){
    $hover[2] = 'Epitomzing picture chosen';
}
if($epitomizing1 == $i1_calculated[3]){
    $hover[3] = 'Epitomzing picture chosen';
}
if($epitomizing1 == $i1_calculated[4]){
    $hover[4] = 'Epitomzing picture chosen';
}
 
$antithetical1 = min(array($i1_calculated[0],$i1_calculated[1],$i1_calculated[2],$i1_calculated[3],$i1_calculated[4]));
if($antithetical1 == $i1_calculated[0]){
    $hover[0] = 'Antithetical picture chosen';
}
if($antithetical1 == $i1_calculated[1]){
    $hover[1] = 'Antithetical picture chosen';
}
if($antithetical1 == $i1_calculated[2]){
    $hover[2] = 'Antithetical picture chosen';
}
if($antithetical1 == $i1_calculated[3]){
    $hover[3] = 'Antithetical picture chosen';
}
if($antithetical1 == $i1_calculated[4]){
    $hover[4] = '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($i1_calculated[0],"name=Image A;alpha=75;hoverText=".$hover[0]);
$EPIT1->addChartData($i1_calculated[1],"name=Image B;alpha=75;hoverText=".$hover[1]);
$EPIT1->addChartData($i1_calculated[2],"name=Image C;alpha=75;hoverText=".$hover[2]);
$EPIT1->addChartData($i1_calculated[3],"name=Image D;alpha=75;hoverText=".$hover[3]);
$EPIT1->addChartData($i1_calculated[4],"name=Image E;alpha=75;hoverText=".$hover[4]);
 
echo '<div class="chart">';
//RENDER THE CHART
$EPIT1->renderChart();
echo '<br />';
echo '<div class="charttxt">';
//DISPLAY EPITOMIZING IMAGE(S) & DESCRIPTIONS
if($epitomizing1 == $i1_calculated[0]){
    echo $image[0].''.$epit_text.' for '.$topic1.', chosen '.$epitomizing.$presented.' '.$descrip[0].$topic1.'" like that?</div>';
}
if($epitomizing1 == $i1_calculated[1]){
    echo $image[1].' '.$epit_text.' for '.$topic1.', chosen '.$epitomizing.$presented.' '.$descrip[1].$topic1.'" like that?</div>';
}
if($epitomizing1 == $i1_calculated[2]){
    echo $image[2].' '.$epit_text.' for '.$topic1.', chosen '.$epitomizing.$presented.' '.$descrip[2].$topic1.'" like that?</div>';
}
if($epitomizing1 == $i1_calculated[3]){
    echo $image[3].' '.$epit_text.' for '.$topic1.', chosen '.$epitomizing.$presented.' '.$descrip[3].$topic1.'" like that?</div>';
}
if($epitomizing1 == $i1_calculated[4]){
    echo $image[4].' '.$epit_text.' for '.$topic1.', chosen '.$epitomizing.$presented.' '.$descrip[4].$topic1.'" like that?</div>';
}
 
//DISPLAY ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical1 == $i1_calculated[0]){
    echo $image[0].' '.$anti_text.' for '.$topic1.', chosen '.$antithetical.$presented.' '.$descrip[0].$topic1.'" not like that?</div>';
}
if($antithetical1 == $i1_calculated[1]){
    echo $image[1].' '.$anti_text.' for '.$topic1.', chosen '.$antithetical.$presented.' '.$descrip[1].$topic1.'" not like that?</div>';
}
if($antithetical1 == $i1_calculated[2]){
    echo $image[2].' '.$anti_text.' for '.$topic1.', chosen '.$antithetical.$presented.' '.$descrip[2].$topic1.'" not like that?</div>';
}
if($antithetical1 == $i1_calculated[3]){
    echo $image[3].' '.$anti_text.' for '.$topic1.', chosen '.$antithetical.$presented.' '.$descrip[3].$topic1.'" not like that?</div>';
}
if($antithetical1 == $i1_calculated[4]){
    echo $image[4].' '.$anti_text.' for '.$topic1.', chosen '.$antithetical.$presented.' '.$descrip[4].$topic1.'" not like that?</div>';
}
echo "</div></div>";
//END REPORT SECTION FOR TOPIC 1
 

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 7:50 pm
by Sinemacula
Thanks!

Now I'm really confused :? :wink: (I'm guessing though that I've been somewhat confused from the start here.)

Perhaps part of what's confusing here is that I only shared part of the code so that I could get it working with one topic. In reality, there are 5 topics, each one with the "same" variables, sort of:
  • Topic 1
    • ab1
    • cd1
    • ae1
    • bd1
    • ec1
    • da1
    • eb1
    • ca1
    • de1
    • bc1
    Topic 2
    • ab2
    • cd2
    • ae2
    • bd2
    • ec2
    • da2
    • eb2
    • ca2
    • de2
    • bc2
    ...
And so on for a total of 5 topics.

Each of those variables is named for the "choice" made on a form - e.g., the first variable "ab" was a choice between "a" and "b" (these letter represent images). Because of other requirements for scoring the data collected later, any choice on the left side of the column is equal to "1" and any choice on the right is equal to "2".

Here's the form code for that part to give a sense of part of the data I'm collecting:

Code: Select all

<td class="input">
<p>Topic 1:<br /><input type="text" name="topic1" /><br />
<p><input type="radio" name="ab1" value="1"/>A - B<input type="radio" name="ab1" value="2"/></p>
<p><input type="radio" name="cd1" value="1"/>C - D<input type="radio" name="cd1" value="2"/></p>
<p><input type="radio" name="ae1" value="1"/>A - E<input type="radio" name="ae1" value="2"/></p>
<p><input type="radio" name="bd1" value="1"/>B - D<input type="radio" name="bd1" value="2"/></p>
<p><input type="radio" name="ec1" value="1"/>E - C<input type="radio" name="ec1" value="2"/></p>
<p><input type="radio" name="da1" value="1"/>D - A<input type="radio" name="da1" value="2"/></p>
<p><input type="radio" name="eb1" value="1"/>E - B<input type="radio" name="eb1" value="2"/></p>
<p><input type="radio" name="ca1" value="1"/>C - A<input type="radio" name="ca1" value="2"/></p>
<p><input type="radio" name="de1" value="1"/>D - E<input type="radio" name="de1" value="2"/></p>
<p><input type="radio" name="bc1" value="1"/>B - C<input type="radio" name="bc1" value="2"/></p>
</td>
<td class="input">
<p>Topic 2:<br /><input type="text" name="topic2" /><br />
<p><input type="radio" name="ab2" value="1"/>A - B<input type="radio" name="ab2" value="2"/></p>
<p><input type="radio" name="cd2" value="1"/>C - D<input type="radio" name="cd2" value="2"/></p>
<p><input type="radio" name="ae2" value="1"/>A - E<input type="radio" name="ae2" value="2"/></p>
<p><input type="radio" name="bd2" value="1"/>B - D<input type="radio" name="bd2" value="2"/></p>
<p><input type="radio" name="ec2" value="1"/>E - C<input type="radio" name="ec2" value="2"/></p>
<p><input type="radio" name="da2" value="1"/>D - A<input type="radio" name="da2" value="2"/></p>
<p><input type="radio" name="eb2" value="1"/>E - B<input type="radio" name="eb2" value="2"/></p>
<p><input type="radio" name="ca2" value="1"/>C - A<input type="radio" name="ca2" value="2"/></p>
<p><input type="radio" name="de2" value="1"/>D - E<input type="radio" name="de2" value="2"/></p>
<p><input type="radio" name="bc2" value="1"/>B - C<input type="radio" name="bc2" value="2"/></p>
</td>
...and so on for 5 topics.

Then in the report, I need to get a "count" of how many times each letter was chosen for each topic.

So, for Topic 1 I want to know how many times was "A" chosen? I use $ia1 as the variable for that ("image" "a" for topic "1")

Code: Select all

 
//how many times was each image chosen for topic 1?
//get count of image a
$ia1 = 0;
 
if($_POST['ab1'] == 1){
    $ia1 = $ia1 + 1;
}
if($_POST['ae1'] == 1){
    $ia1 = $ia1 + 1;
}
if($_POST['da1'] == 2){
    $ia1 = $ia1 + 1;
}
if($_POST['ca1'] == 2){
    $ia1 = $ia1 + 1;
}
 
//get count of image b
$ib1 = 0;
 
if($_POST['ab1'] == 2){
    $ib1 = $ib1 + 1;
}
if($_POST['bd1'] == 1){
    $ib1 = $ib1 + 1;
}
if($_POST['eb1'] == 2){
    $ib1 = $ib1 + 1;
}
if($_POST['bc1'] == 1){
    $ib1 = $ib1 + 1;
}
 
//get count of image c
$ic1 = 0;
 
if($_POST['cd1'] == 1){
    $ic1 = $ic1 + 1;
}
if($_POST['ec1'] == 2){
    $ic1 = $ic1 + 1;
}
if($_POST['ca1'] == 1){
    $ic1 = $ic1 + 1;
}
if($_POST['bc1'] == 2){
    $ic1 = $ic1 + 1;
}
 
//get count of image d
$id1 = 0;
if($_POST['cd1'] == 2){
    $id1 = $id1 + 1;
}
if($_POST['bd1'] == 2){
    $id1 = $id1 + 1;
}
if($_POST['da1'] == 1){
    $id1 = $id1 + 1;
}
if($_POST['de1'] == 1){
    $id1 = $id1 + 1;
}
 
//get count of image e
$ie1 = 0;
 
if($_POST['ae1'] == 2){
    $ie1 = $ie1 + 1;
}
if($_POST['ec1'] == 1){
    $ie1 = $ie1 + 1;
}
if($_POST['eb1'] == 1){
    $ie1 = $ie1 + 1;
}
if($_POST['de1'] == 2){
    $ie1 = $ie1 + 1;
}
 
...and so on for all 5 topics.

Originally I got it working the long way, with a separate "copy" of that code for each topic (changing the "1" to the topic number in each case) - to lead to $ia1, $ib1, $ic1, $id1, $ie1, $ia2, $ib2... $id5, $ie5 as the 25 variables that are important for generating the charts and determining the "epitomizing" (most frequently chosen) and "antithetical" (least frequently chosen) images for each topic.

Anyway, so now I've changed that code to get the counts and got the "how many times was image x chosen for topic x" in a loop and also have the creation of the charts (1 per topic) in a loop.

I'm also collecting descriptions of the images (i.e., 1 description for each image = 5 descriptions total) - which will get displayed with the charts for each topic ONLY in the case that the image is epitomizing or antithetical for the topic. So, any given description could theoretically be displayed with the chart for every topic.

Here's the full code I've got now, that's working very well (there's more that comes after this, but it's not relevant to this issue):

Code: Select all

<?php
//get the class
include('FCharts/Class/FusionCharts_Gen.php');
$today = date("F j, Y");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">      
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="description" content="JOG Your Right Brain  ~ A Holistic Assessment Tool for Business & Research - Using the Projective Differential to uncover the hidden attitudes and perceptions that influence behavior.">
<meta name="keywords" content="Projective Differential, JOG Your Right Brain, PD, PD-JOG, intuitive, unconscious, abstract, implicit, latent attitudes, perceptions, identification, assessment, projective differential, r-mode, l-mode, right-brain, left-brain, organizational culture, corporate culture, psychological">
<title>Strategy SIG ~ JOG Your Right Brain Personalized Report for <?php echo $_POST['name']; ?></title>
<script language='javascript' src='FCharts/FusionCharts/FusionCharts.js'></script>
<style type="text/css">
.pd_table {
    text-align: center;
    min-width: 100px;
}
.info {
    margin: 10px 30px 10px 25px;
    width: 75%;
}
.hyphen {
    white-space: nowrap;
}
.images {
    text-align: center;
}
.section {
    margin-left: 5px;
    padding-bottom: 10px;
}
.chart {
    float:left;
    margin:0 0 15px 20px;
    width: 75%;
    padding:15px;
    border:1px solid black;
    text-align:center;
    clear: both;
}
.charttxt {
    text-align:left;
}
blockquote {
    font-style: italic;
    padding: 5px 15px 5px 25px;
    margin: 0px 25px 0px 150px;
    background-color: #eeeeee;
}
.img {
    float:left;
    padding-right: 10px;
}
.epitimage {
    margin: 0 5px 25px 5px;
    align: top;
    border-bottom: 1px dotted #999999;
    padding-bottom: 5px;
    min-height: 87px;
}
h2,h3 {
    background-color: #3062C4;
    color: #fff;
    padding-left: 10px;
    width: 80%;
}
.incong{
    width: 40%;
    color: #3062C4;
    border-top: 1px solid #57AC0B;
    border-bottom: 1px solid #57AC0B;
    background: #f6f6f6;
    padding-top: 5px;
}
.enddiv {
    clear:both;
}
.raw_data {
    border-top: 1px dashed;
    padding-top: 5px;
}
.small {
font-size: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="section" id="intro">
<h4>Report prepared for <?php echo $_POST['name']; ?></h4>
Session completed on <?php echo $today; ?>. 
<div class="info">
Thanks for using the online Projective Differential scorer, <?php echo $_POST['name']; ?>.</div>
<div class="section" id="epitomizing">
<h3>Epitomizing Picture Scores</h3>
<div class="info">The graphs below show how many times each image was chosen for each topic, as a percentage of the number of times the image was displayed for the topic.</div>
<div class="info">Which picture did you choose the most for each topic? Which did you choose the least? In what ways is the "epitomizing" picture like the topic? In what ways is the "antithetical" image different from the topic?</div>
<?php
//SET THE TEXT FOR THE EPITOMIZING & ANTITHETICAL PICTURES AREA FOR ALL CHARTS
 
$image_a = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_a.png" width="150px"></span> Image A was the';
$image_b = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_b.png" width="150px"></span> Image B was the';
$image_c = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_c.png" width="150px"></span> Image C was the';
$image_d = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_d.png" width="150px"></span> Image D was the';
$image_e = '<div class="epitimage"><span class="img"><img src="images/PDImages/image_e.png" width="150px"></span> Image E was the';
$epit_text = 'Epitomizing Picture';
$anti_text = 'Antithetical Picture';
$presented = '% of the times it was presented.<br />';
$descrip_a = 'Your description of Image A was:<blockquote>'.$_POST['desc_a'].'</blockquote>In what ways is "';
$descrip_b = 'Your description of Image B was:<blockquote>'.$_POST['desc_b'].'</blockquote>In what ways is "';
$descrip_c = 'Your description of Image C was:<blockquote>'.$_POST['desc_c'].'</blockquote>In what ways is "';
$descrip_d = 'Your description of Image D was:<blockquote>'.$_POST['desc_d'].'</blockquote>In what ways is "';
$descrip_e = 'Your description of Image E was:<blockquote>'.$_POST['desc_e'].'</blockquote>In what ways is "';
 
 
//get the topics
$topic = array($_POST['topic1'],$_POST['topic2'],$_POST['topic3'],$_POST['topic4'],$_POST['topic5']);
 
//get the raw data for each choice
$ab = array($_POST['ab1'],$_POST['ab2'],$_POST['ab3'],$_POST['ab4'],$_POST['ab5']);
$cd = array($_POST['cd1'],$_POST['cd2'],$_POST['cd3'],$_POST['cd4'],$_POST['cd5']);
$ae = array($_POST['ae1'],$_POST['ae2'],$_POST['ae3'],$_POST['ae4'],$_POST['ae5']);
$bd = array($_POST['bd1'],$_POST['bd2'],$_POST['bd3'],$_POST['bd4'],$_POST['bd5']);
$ec = array($_POST['ec1'],$_POST['ec2'],$_POST['ec3'],$_POST['ec4'],$_POST['ec5']);
$da = array($_POST['da1'],$_POST['da2'],$_POST['da3'],$_POST['da4'],$_POST['da5']);
$eb = array($_POST['eb1'],$_POST['eb2'],$_POST['eb3'],$_POST['eb4'],$_POST['eb5']);
$ca = array($_POST['ca1'],$_POST['ca2'],$_POST['ca3'],$_POST['ca4'],$_POST['ca5']);
$de = array($_POST['de1'],$_POST['de2'],$_POST['de3'],$_POST['de4'],$_POST['de5']);
$bc = array($_POST['bc1'],$_POST['bc2'],$_POST['bc3'],$_POST['bc4'],$_POST['bc5']);
 
//start looping - need to loop 5 times to get through the 5 topics
for($i=0; $i<=4; $i++){
 
//GENERATE PICTURE COUNTS
 
//get count for image a
$ia[$i] = 0;
 
if($ab[$i] == 1){
    $ia[$i] = $ia[$i] + 1;
}
if($ae[$i] == 1){
    $ia[$i] = $ia[$i] + 1;
}
if($da[$i] == 2){
    $ia[$i] = $ia[$i] + 1;
}
if($ca[$i] == 2){
    $ia[$i] = $ia[$i] + 1;
}
 
//get count of image b
$ib[$i] = 0;
 
if($ab[$i] == 2){
    $ib[$i] = $ib[$i] + 1;
}
if($bd[$i] == 1){
    $ib[$i] = $ib[$i] + 1;
}
if($eb[$i] == 2){
    $ib[$i] = $ib[$i] + 1;
}
if($bc[$i] == 1){
    $ib[$i] = $ib[$i] + 1;
}
 
//get count of image c
$ic[$i] = 0;
 
if($cd[$i] == 1){
    $ic[$i] = $ic[$i] + 1;
}
if($ec[$i] == 2){
    $ic[$i] = $ic[$i] + 1;
}
if($ca[$i] == 1){
    $ic[$i] = $ic[$i]+ 1;
}
if($bc[$i] == 2){
    $ic[$i] = $ic[$i] + 1;
}
 
//get count of image d
$id[$i] = 0;
if($cd[$i] == 2){
    $id[$i] = $id[$i] + 1;
}
if($bd[$i] == 2){
    $id[$i] = $id[$i] + 1;
}
if($da[$i] == 1){
    $id[$i] = $id[$i] + 1;
}
if($de[$i] == 1){
    $id[$i] = $id[$i] + 1;
}
 
//get count of image e
$ie[$i] = 0;
 
if($ae[$i] == 2){
    $ie[$i] = $ie[$i] + 1;
}
if($ec[$i] == 1){
    $ie[$i] = $ie[$i] + 1;
}
if($eb[$i] == 1){
    $ie[$i] = $ie[$i] + 1;
}
if($de[$i] == 2){
    $ie[$i] = $ie[$i] + 1;
}
 
//FIND OUT WHICH PICTURE(S) WERE CHOSEN THE MOST OFTEN (EPITOMIZING) FOR EACH TOPIC
$epitomizing[$i] = max(array($ia[$i] / 4 * 100,$ib[$i] / 4 * 100,$ic[$i] / 4 * 100,$id[$i] / 4 * 100,$ie[$i] / 4 * 100));
 
//FIND OUT WHICH PICTURE(S) WERE CHOSEN THE LEAST OFTEN (ANTITHETICAL) FOR EACH TOPIC
$antithetical[$i] = min(array($ia[$i] / 4 * 100,$ib[$i] / 4 * 100,$ic[$i] / 4 * 100,$id[$i] / 4 * 100,$ie[$i] / 4 * 100));
 
if($epitomizing[$i] == $ia[$i] / 4 * 100){
    $hover_a[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ia[$i] / 4 * 100) {
    $hover_a[$i] = "Antithetical picture chosen";
} else {
    $hover_a[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ib[$i] / 4 * 100){
    $hover_b[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ib[$i] / 4 * 100) {
    $hover_b[$i] = "Antithetical picture chosen";
} else {
    $hover_b[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ic[$i] / 4 * 100){
    $hover_c[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ic[$i] / 4 * 100) {
    $hover_c[$i] = "Antithetical picture chosen";
} else {
    $hover_c[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $id[$i] / 4 * 100){
    $hover_d[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $id[$i] / 4 * 100) {
    $hover_d[$i] = "Antithetical picture chosen";
} else {
    $hover_d[$i] = "Neither empitomizing nor antithetical";
}
 
if($epitomizing[$i] == $ie[$i] / 4 * 100){
    $hover_e[$i] = "Epitomizing picture chosen";
} elseif($antithetical[$i] == $ie[$i] / 4 * 100) {
    $hover_e[$i] = "Antithetical picture chosen";
} else {
    $hover_e[$i] = "Neither empitomizing nor antithetical";
}
 
 
//create Column3D chart
$EPIT[$i] = new FusionCharts("Column3D","600","400");
//set the relative path for the swf
$EPIT[$i]->setSWFPath("FCharts/FusionCharts/");
 
//set chart attributes
$strParam="caption=Epitomizing Picture for ".$topic[$i].";yAxisName=%25 times chosen;yAxisMinValue=0;yAxisMaxValue=100;decimalPrecision=0;formatNumberScale=5;numberSuffix=%25;numdivlines=3;canvasBgColor=eeeeee;canvasBaseColor=eeeeee";
$EPIT[$i]->setChartParams($strParam);
 
 
//add chart values
$EPIT[$i]->addChartData($ia[$i] / 4 * 100,"name=Image A;alpha=75;hoverText=".$hover_a[$i]);
$EPIT[$i]->addChartData($ib[$i] / 4 * 100,"name=Image B;alpha=75;hoverText=".$hover_b[$i]);
$EPIT[$i]->addChartData($ic[$i] / 4 * 100,"name=Image C;alpha=75;hoverText=".$hover_c[$i]);
$EPIT[$i]->addChartData($id[$i] / 4 * 100,"name=Image D;alpha=75;hoverText=".$hover_d[$i]);
$EPIT[$i]->addChartData($ie[$i] / 4 * 100,"name=Image E;alpha=75;hoverText=".$hover_e[$i]);
 
echo '<div class="chart">';
$EPIT[$i]->renderChart();
echo '<br />';
echo '<div class="charttxt">';
 
//GET EPITOMIZING IMAGE(S) & DESCRIPTIONS
 
if($epitomizing[$i] == $ia[$i] / 4 * 100){
echo $image_a.''.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing[$i].$presented.' '.$descrip_a.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ib[$i] / 4 * 100){
echo $image_b.' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing[$i].$presented.' '.$descrip_b.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ic[$i] / 4 * 100){
echo $image_c.' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing[$i].$presented.' '.$descrip_c.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $id[$i] / 4 * 100){
echo $image_d.' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing[$i].$presented.' '.$descrip_d.$topic[$i].'" like that?</div>';
}
if($epitomizing[$i] == $ie[$i] / 4 * 100){
echo $image_e.' '.$epit_text.' for '.$topic[$i].', chosen '.$epitomizing[$i].$presented.' '.$descrip_e.$topic[$i].'" like that?</div>';
}
//GET ANTITHETICAL IMAGE(S) & DESCRIPTIONS
if($antithetical[$i] == $ia[$i] / 4 * 100){
echo $image_a.' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical[$i].$presented.' '.$descrip_a.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ib[$i] / 4 * 100){
echo $image_b.' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical[$i].$presented.' '.$descrip_b.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ic[$i] / 4 * 100){
echo $image_c.' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical[$i].$presented.' '.$descrip_c.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $id[$i] / 4 * 100){
echo $image_d.' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical[$i].$presented.' '.$descrip_d.$topic[$i].'" not like that?</div>';
}
if($antithetical[$i] == $ie[$i] / 4 * 100){
echo $image_e.' '.$anti_text.' for '.$topic[$i].', chosen '.$antithetical[$i].$presented.' '.$descrip_e.$topic[$i].'" not like that?</div>';
}
echo "</div></div>";
 
} //return to top to loop and recreate for each topic
 
?>
This produces 5 charts (1 per topic) showing how often each image was chosen as a percentage of the times it was presented for a topic (each image is presented 4 times per topic). It also tells us which image(s) was/were "epitomizing" and "antithetical" for each topic, and provides a copy of those images and the descriptions provided by the respondent on the form.

I'll keep looking over the code you provided to see if I can figure it out. :wink: Since I'll be making a couple more of these, including some that draw the same data from a database, it's probably good that I figure out how to do it the "right" (or best/easiest/most efficient) way.

Thanks,
Scott

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 8:02 pm
by Christopher
You problem is just detailed, not that complicated. That is one of the reasons you want to reduce the script down to loops -- so you can actually see what is going on. For the topics you would just put everything I did in an outer array, so $a1[0] becomes $a[1][0] if 1 is the topic number you want to use. Honestly I would isolate the code for a single topic in a function and call it for each topic.

Re: Shorten code using loops/arrays?

Posted: Mon Feb 18, 2008 10:30 pm
by Sinemacula
arborint wrote:Honestly I would isolate the code for a single topic in a function and call it for each topic.
I'm still trying to figure out arrays and loops! :lol: Now I've got to tackle functions?!? :wink: :lol:

Studying business and psychology didn't prepare me for this!