Author Topic: Help with each() depreciation  (Read 349 times)

0 Members and 1 Guest are viewing this topic.

Offline dasman

  • Forecaster
  • *****
  • Posts: 491
    • Wx Peotone
Help with each() depreciation
« on: August 03, 2018, 10:49:02 AM »
I have a page written for me some time ago that shows stats from my NOAA reports files and a MySQL data base. The page is http://www.peotoneweather.com/selectstat.php. I recently switched from php 7.1 to php 7.2 on my site. Under 7.2 the mentioned page shows the following error:

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/ybuhhqulwjts/public_html/bar.php on line 66

I understand only that the each() function works in 7.2 but will not in the future. Fixing this is way beyond me. Is this an easy fix? I have included below the  bar.php file that the error statement mentions.

Code: [Select]
<?php 

// include_once("noaastats.php"); 
# ------- The graph values in the form of associative array

 
$img_width=380;
$img_height=240
$bar_width=22;
$margins=20;

 
# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width $margins 2;
$graph_height=$img_height $margins 2
$imgimagecreatetruecolor($img_width,$img_height);
 
$total_bars=count($values);
$gap= ($graph_width$total_bars $bar_width ) / ($total_bars +1);
  
$bv_gap 9;
  
$tdate date("U");
 
# -------  Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,$border_colors[0],$border_colors[1],$border_colors[2]);
$line_color=imagecolorallocate($img,220,220,220);
$line_color2=imagecolorallocate($img,220,20,20);
 
# ------ Create the border around the graph ------

// print_r($border_colors);


imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins+7,$margins-11,$img_width-4-$margins,$img_height-$margins,$bar_color); // inside border
imagefilledrectangle($img,$margins+8,$margins-10,$img_width-5-$margins,$img_height-1-$margins,$background_color); // grey sizing

 
# ------- Max value is required to adjust the scale -------
$max_value=max($values);
$min_value=min($values);

if($max_value <= 0){
$max_value 1;
}
// print_r($max_value);   echo "<br />\n";

$ratio$graph_height/$max_value;
 
# -------- Create scale and draw horizontal lines  --------
$horizontal_lines=10;
$horizontal_gap=$graph_height/$horizontal_lines;
for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height $margins $horizontal_gap $i ;
imageline($img,$margins+8,$y,$img_width-$margins-5,$y,$line_color);
$v=round($horizontal_gap $i /$ratio,1);
// $v=$horizontal_gap * $i /$ratio;
imagestring($img,0,3,$y-5,$v,$bar_color);     // left side value - up & down
}
 
 
# ----------- Draw the bars here ------
for($i=0;$i$total_bars$i++){ 
 
# ------ Extract key and value pair from the current pointer position
list($key,$value)=each($values); 
  if(preg_match('|\d{2}|',$value) ) {  // adjust value spacing on top of bar
$bv_gap 7;
}
  if(preg_match('|\d{3}|',$value) ) {
$bv_gap 4;
}

$x1$margins $gap $i * ($gap+$bar_width) ;
$x2$x1 $bar_width
$y1=$margins +$graph_heightintval($value $ratio) ;
$y2=$img_height-$margins;
imagestring($img,0,$x1+$bv_gap,$y1-10,$value,$bar_color);  // move values on top of bars
imagestring($img,0,$x1+2,$img_height-15,$key,$bar_color);    // move year numbers on bottom
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}

imagepng($img"barchrt".$tdate.".png"); 

imagedestroy($img);

echo 
"<!-- Current chart file: barchrt".$tdate.".png size " filesize("barchrt".$tdate.".png") . " -->\n";

?>

<div style="text-align:center">

<table width="900" border="0" cellspacing="0" cellpadding="5" style="margin:10px auto 10px auto">
  <tr>
    <td><b><?php echo $top_name ?> <?php echo $var_amt ?></b><br /><img src="barchrt<?php echo $tdate ?>.png" width="<?php echo $img_width ?>" height="<?php echo $img_height ?>" alt="x" /></td>
  </tr>
</table>

</div>
<?php 
// Delete old bar chart files. BE CAREFULL !   DO NOT EDIT or you could delete ALL files
foreach (glob("barchrt*.png") as $bar_chart) {
  if (
$bar_chart !== "barchrt".$tdate.".png") {
echo "<!-- DELETED $bar_chart size " filesize($bar_chart) . " -->\n";
    
unlink($bar_chart);
  }
}
?>

Dave Sommerfed
Peotone Illinois USA
CW7762, KILPEOT1
NWS COOP, CoCoRaHS, Spotter Network

Offline mcrossley

  • Forecaster
  • *****
  • Posts: 1137
    • Wilmslow Astro
Re: Help with each() depreciation
« Reply #1 on: August 03, 2018, 12:00:36 PM »
Possibly something like this (completely untested!)...
Code: [Select]
# ----------- Draw the bars here ------
$i = 0;
foreach($values as $key => $value) {
    if (preg_match('|\d{2}|', $value)) { // adjust value spacing on top of bar
        $bv_gap = 7;
    }
    if (preg_match('|\d{3}|', $value)) {
        $bv_gap = 4;
    }

    $x1 = $margins + $gap + $i * ($gap + $bar_width);
    $x2 = $x1 + $bar_width;
    $y1 = $margins + $graph_height - intval($value * $ratio);
    $y2 = $img_height - $margins;
    imagestring($img, 0, $x1 + $bv_gap, $y1 - 10, $value, $bar_color); // move values on top of bars
    imagestring($img, 0, $x1 + 2, $img_height - 15, $key, $bar_color); // move year numbers on bottom
    imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color);
    $i++;
}
Mark

Offline dasman

  • Forecaster
  • *****
  • Posts: 491
    • Wx Peotone
Re: Help with each() depreciation
« Reply #2 on: August 03, 2018, 01:16:44 PM »
Wow! I made a back up and then added your snippet into the script and uploaded. For now it appears to be working perfectly, with no error shown \:D/

Thank you!!!!!!!!!!!!!!
Dave Sommerfed
Peotone Illinois USA
CW7762, KILPEOT1
NWS COOP, CoCoRaHS, Spotter Network

 

anything