Flatronez Posted January 24, 2023 Share Posted January 24, 2023 Hello, I have inherited a website, but I can't find the php version, and when I run the website on 7.4 php, I get a lot of errors, maybe you can help? 54 $st=""; 55 $stno=0; 56 $cellt = array(); 57 $result = sqlsrv_query($conn, " SELECT C.date, C.station, C.fuel, C.vmm, C.vol, C.fmm FROM purchase C inner JOIN (SELECT D.station, D.fuel, MAX(D.DATE) as date FROM purchase D GROUP BY D.station, D.fuel) S on C.DATE = S.date where c.fuel!='' order by c.station, c.fuel "); 58 while ($row = sqlsrv_fetch_array($result)) 59 { 60 if ($row[1]!=$st) 61 { 62 //$station[$stno]=""; 63 if (!empty($st)) 64 { 65 for ($k=1; $k<=count($fuel); $k++) 66 { 67 if ($cellt[$k][0]==0) 68 echo'<td>-</td>'; 69 else AND 125 for ($k=1; $k<=count($fuel); $k++) 126 { 127 if ($cellt[$k][0]==0) 128 echo'<td>-</td>'; 129 else 130 if ($cellt[$k][1]==0) 131 echo'<td><a href="levelFuel.php?station=' . $station[$stno] . '&fuel=' . $fuel[$k] . '&type=all">' . $cellt[$k][0] . '</a></td>'; 132 else if ($cellt[$k][1]==1) 133 echo'<td class = "low"><a href="levelFuel.php?station=' . $station[$stno] . '&fuel=' . $fuel[$k] . '&type=all">' . $cellt[$k][0] . '</a></td>'; 134 else if ($cellt[$k][1]==2) 135 echo'<td class = "alert"><a href="levelFuel.php?station=' . $station[$stno] . '&fuel=' . $fuel[$k] . '&type=all">' . $cellt[$k][0] . '</a></td>'; 136 else if ($cellt[$k][1]==3) 137 echo'<td class = "water"><a href="levelFuel.php?station=' . $station[$stno] . '&fuel=' . $fuel[$k] . '&type=all">' . $cellt[$k][0] . '</a></td>'; 138 } Notice: Undefined offset: 8 in C:\xampp\htdocs\levelMain.php on line 67 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 67 Notice: Undefined offset: 8 in C:\xampp\htdocs\levelMain.php on line 67 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 67 Notice: Undefined offset: 7 in C:\xampp\htdocs\levelMain.php on line 67 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 67 Notice: Undefined offset: 7 in C:\xampp\htdocs\levelMain.php on line 67 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 67 Notice: Undefined offset: 8 in C:\xampp\htdocs\levelMain.php on line 67 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 67 Notice: Undefined offset: 8 in C:\xampp\htdocs\levelMain.php on line 127 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\levelMain.php on line 127 Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/ Share on other sites More sharing options...
requinix Posted January 24, 2023 Share Posted January 24, 2023 Those two lines assume that something exists in $cellt when it, apparently, does not. Hard to tell why or what's going on from just this code... Is everything behaving the way you expect it to, besides the notices? Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/#findComment-1604976 Share on other sites More sharing options...
mac_gyver Posted January 24, 2023 Share Posted January 24, 2023 the code is unconditionally looping over some number of $fuel choices and expects there to be corresponding data in $cellt. IF (a big if) the data in $cellt has gaps in the 1st index, then the code should be testing if $cellt[$k] isset() before referencing it. what does adding the the following, after the point where $cellt is being built, show - echo '<pre>'; print_r($cellt); echo '</pre>'; this code is apparently (hard to tell without all the actual code, sample data, and desired output) outputting data for each station (starts a new section when the station value changes) and each fuel type. the code is repetitive, because it must complete the previous section when a new section is detected, and it must also complete the last section at the end. you can greatly simplify all of this by pre-fetching the data into an array and indexing/pivoting it by first the station, then the fuel. to produce the output from the pre-fetched data then just takes a couple of loops, without repetition in the code or extra variables to detect when the station changes. you should also use associative index names for the fetched data, so you are not having to keep track of numbers. lastly, in the code building the links, the only thing that is different is the class='...' markup. this is the only thing that should be conditional. the rest of the markup is identical and should only exist once in the code - Don't Repeat Yourself (DRY.) build the class='...' markup in a php variable, then use that variable when building the single link. you should also build the query string key/values in an array, then use http_build_query() to produce the query sting for the link. Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/#findComment-1604978 Share on other sites More sharing options...
Flatronez Posted January 24, 2023 Author Share Posted January 24, 2023 1 hour ago, requinix said: Those two lines assume that something exists in $cellt when it, apparently, does not. Hard to tell why or what's going on from just this code... Is everything behaving the way you expect it to, besides the notices? everything seems fine, I am attaching the whole code, maybe your eyes will see the bug levelmain.php.txt Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/#findComment-1604981 Share on other sites More sharing options...
mac_gyver Posted January 24, 2023 Share Posted January 24, 2023 if you do this - 2 hours ago, mac_gyver said: by pre-fetching the data into an array and indexing/pivoting it by first the station, then the fuel. a majority of this code will go away. assuming i decoded what you are trying to produce as output. the following should be all the code that you need for the data table section - // pre-fetch the data, indexing/pivoting it using the station, then fuel as indexes $data = []; while ($row = sqlsrv_fetch_array($result)) { $data[$row['station']][$row['fuel']] = $row; } // produce the output ?> <table class = "main"> <tr class="main"><td></td> <?php // output the heading foreach($fuel as $fu) { echo "<td class = 'main'>$fu</td>"; } ?> <td class = "main">Atnaujinta</td></tr> <?php // output the data foreach($station as $st) { echo "<tr><td class='main'><a href='levelStation.php?station=$st'><b>$st</b></a></td>"; foreach($fuel as $fu) { if(empty($data[$st][$fu]['vol'])) { echo '<td>-</td>'; } else { $row = $data[$st][$fu]; $class = ''; if ($row['fmm']<=100) $class = 'class="low"'; if ($row['fmm']>100 AND $row['fmm']<250) $class = 'class="alert"'; if ($row['vmm']>=5) $class = 'class="water"'; $qs = []; $qs['station'] = $st; $qs['fuel'] = $fu; $qs['type'] = 'all'; $qs = http_build_query($qs); echo "<td $class><a href='levelFuel.php?$qs'>{$row['vol']}</a></td>"; } } echo "<td>".date_format(date_create($row['date']),"Y-m-d H:i:s")."</td></tr>\n"; } // output the totals ?> <tr><td>Iš viso:</td> <?php foreach($fuel as $fu) { $sum = 0; foreach($station as $st) { $sum += $data[$st][$fu]['vol']??0; } echo "<td class = 'main'>$sum</td>"; } ?> <td></td></tr> </table> 1 Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/#findComment-1604992 Share on other sites More sharing options...
Flatronez Posted January 25, 2023 Author Share Posted January 25, 2023 Thank you very much for help! GJ Quote Link to comment https://forums.phpfreaks.com/topic/315833-notice-trying-to-access-array-offset-on-value-of-type-null-in/#findComment-1605033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.