Jump to content


Photo

creating an array()


  • Please log in to reply
8 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 27 April 2006 - 07:27 PM

Well, I guess you can't do this:
Any suggestions???

$graphValues$c[]=$row['RATING'];

while ($c <= $tl){
$EVAL=$NAME[$c];
$result2 = mysql_query("Select
Avg(surveyresults.RATING) as RATING, surveyresults.`DATE` From surveyresults
Where
surveyresults.EVALUATOR = '$EVAL'
AND DATE_SUB(NOW(),INTERVAL 30 DAY)<=DATE
Group By
surveyresults.`DATE`
Order By
surveyresults.`DATE` Asc");
while ($row = mysql_fetch_array($result2)){
         $graphValues$c[]=$row['RATING'];
}


#2 inztinkt

inztinkt
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationCyprus ( OnThisIsland )

Posted 27 April 2006 - 07:38 PM

while ($row...){
$graphValues$c .= $row['RATING'];
}

should put each rating into the variable
<!-- // Blah // -->
OnThisIsland.com

The greatest leader is not always he who holds the most power, but he who has the ability to do the most with the power he has. - Inztinkt is Distinct!

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 April 2006 - 07:39 PM

Why don't you think you can't do that? Errors? What did you get, what did you expect to get?

Maybe what you want is
<?php $graphValues[$c][]=$row['RATING']; ?>

Ken

#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 27 April 2006 - 08:09 PM

Sorry,

What I am trying to do is crate a series of arrays, like this:
I am tryint to do $graphValues$c[]=$row['RATING']; to create the variable names dynamically...

$graphValues1[]
$graphValues2[]
$graphValues3[]

I am going to build a line graph with three different lines...

here is the error:

Parse error: parse error, unexpected T_VARIABLE in c:\inetpub\wwwroot\corp\graph30tl.php on line 65


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 April 2006 - 09:16 PM

Why don't you do it this way:
<?php $graphValues[$c][]=$row['RATING']; ?>
You will have one 2-diminsional array instead of many different arrays. It will be much easier to code.

Ken

#6 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 27 April 2006 - 10:35 PM

you'd do it this way...for dynamic variables

$arrays = "graphValues" . $c;
$$arrays[]=$row['RATING'];


but yeah kenrbnsn is right
a multi-dim array would work more smoothly

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#7 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 April 2006 - 01:54 AM

This seems to work...

<?php
$c=1;
$arrays = "graphValues" . $c;
$$arrays .=1;
$$arrays .=2;
$$arrays .=3;
$c=2;
$arrays = "graphValues" . $c;
$$arrays .=1;
$$arrays .=2;
$$arrays .=3;
$c=3;
$arrays = "graphValues" . $c;
$$arrays .=1;
$$arrays .=2;
$$arrays .=3;

echo $graphValues1[0];
echo "<br/>";
echo $graphValues1[1];
echo "<br/>";
echo $graphValues1[2];
echo "<br/>";
echo $graphValues2[0];
echo "<br/>";
echo $graphValues2[1];
echo "<br/>";
echo $graphValues2[2];
echo "<br/>";
echo $graphValues3[0];
echo "<br/>";
echo $graphValues3[1];
echo "<br/>";
echo $graphValues3[2];
echo "<br/>";

?>

this gives:

1
2
3
1
2
3
1
2
3

//(here is the loop version)

<?php
for($c=1; $c<=3; $c++){
$arrays = "graphValues" . $c;
for($i=1; $i<=3; $i++){
$$arrays .=$i;
}
}
?>

Now, I just have to make it a loop...
Ken, I will study the multi-array... I've not done one before... but I get the concept...
I'm just starting to learn arrays the past couple of weeks...

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 April 2006 - 04:20 AM

Here's your code as one array...
<?php
$graphValues = array();
for ($i=1;$i<4;$i++) 
     for ($j=1;$j<4;$j++)
          $graphValues[$i][$j] = $j;
echo '<pre>' . print_r($graphValues,true) . '</pre>';
for ($i=1;$i<4;$i++)
     for ($j=1;$j<4;$j++)
          echo '$graphValues[' . $i . '][' . $j . '] = ' . $graphValues[$i][$j] . '<br>';
?>
Which results in
Array
(
    [1] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [2] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [3] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

)

$graphValues[1][1] = 1
$graphValues[1][2] = 2
$graphValues[1][3] = 3
$graphValues[2][1] = 1
$graphValues[2][2] = 2
$graphValues[2][3] = 3
$graphValues[3][1] = 1
$graphValues[3][2] = 2
$graphValues[3][3] = 3

Ken

#9 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 April 2006 - 05:31 PM

Ken

I'm not quite up to your last example, but I'm still trying to get my head around it...

meanwhile, I was proud of what my final solution was: (I marked the section below)

<?
header("Content-type: image/png");
$imgWidth=600;
$imgHeight=125;
// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$color[1]=imagecolorallocate($image, 0, 0, 255);
$color[2]=imagecolorallocate($image, 0, 255, 255);
$color[3]=imagecolorallocate($image, 255, 0, 255);
$color[4]=imagecolorallocate($image, 255, 0, 0);
$color[6]=imagecolorallocate($image, 25, 150, 0);
$color[5]=imagecolorallocate($image, 0, 255, 0);
$color[7]=imagecolorallocate($image, 255, 255, 255);

// Create border around image
// right vert
imageline($image, 599, 599, 599, 0, $colorGrey);
// bottom horiz
imageline($image, 0, 124, 599, 124, $colorGrey);
// top horiz
imageline($image, 599, 0, 0, 0, $colorGrey);
// left vert
imageline($image, 0, 0, 0, 599, $colorGrey);
// Create grid
for ($i=1; $i<30; $i++){
//H
imageline($image, $i*20, 0, $i*20, 600, $colorGrey);
//V
imageline($image, 0, $i*25, 600, $i*25, $colorGrey);}

$user="";
$host = "localhost";
$password="";
$database = "teknetix";
mysql_connect($host,$user,$password);
mysql_select_db($database);

$result = mysql_query("Select count(distinct `EVALUATOR`) as EVALUATOR From surveyresults
Where EVALUATOR not like '0018'
AND EVALUATOR not like '0311'");
IF ($row1 = mysql_fetch_array($result)){
    $COUNT=$row1['EVALUATOR'];
    }

$result1 = mysql_query("Select distinct `EVALUATOR` as EVALUATOR From surveyresults
where `EVALUATOR` <> '0018' AND `EVALUATOR` <> '0311'");
$c=1;

while ($row1 = mysql_fetch_array($result1)){
    $NAME[$c] = $row1['EVALUATOR'];
    $result0 = mysql_query("Select FNAME, LNAME from survey Where `ID` like '$NAME[$c]'");
$c++;
}

// HERE IS THE SECTION !!!!!!!

$tl=$c-1;
$c=1;
while ($c <= $tl){
     $col=$color[$c];
     $EVAL=$NAME[$c];
     $result2 = mysql_query("Select
     Avg(surveyresults.RATING) as RATING, surveyresults.`DATE` From surveyresults
     Where
     surveyresults.EVALUATOR = '$EVAL'
     Group By surveyresults.`DATE`
     Order By surveyresults.`DATE` Asc LIMIT 30");
     while ($row = mysql_fetch_array($result2)){
          $graphValues[$c][] = $row['RATING'];
      }
     // Create line graph
     for ($i=0; $i<30; $i++) {
     if (isset($graphValues[$c][$i+1])) {
            imageline($image, $i*20,
            (125-($graphValues[$c][$i])*25 + 12), ($i+1)*20,
            (125-($graphValues[$c][$i+1])*25 + 12), $color[$c]);
             }
      }
$c++;
}
imagepng($image);
imagedestroy($image);
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users