Jump to content

How to solve: Trying to access array offset on value of type bool


Go to solution Solved by OldGrim,

Recommended Posts

CMS: PHP-Fusion version 9.03.110, PHP version: 7.4.16, MySQL Server Version: 5.7.33-log, DB Driver: PDO MySQL

My own support people at PHP-Fusion main site have been unable to help me with this problem and I'm hoping for some new eyes and new input on this error. First of all my script works perfectly fine and outputs the result desired. I know that my script is not using PDO prepared statements and I am not prepared to modify it to do so because too much would have to be changed. And I'm not fluent on PDO.

My script is an event calendar and can be seen at this url: https://windy.whisperwillow.net/grims_blog/index.php

The error only occurs 3 times on 3 lines.

<?php
echo "<div class='col-sm-12'>\n";
	include BASEDIR."grims_blog/include/cal_func_inc.php";
$day=date('d');
$endDate=date('t',mktime(0,0,0,$cMonth,$day,$cYear));
echo "<div><span class='hdspan2'><b>".$locale['gb_510']."</b></span></div><p>\n";
echo "<table class='tbl-responsive' width='67%' align='center' border='0' cellpadding='0' cellspacing='0'><tr>\n";
echo "<td align='center' colspan='3'><img style='max-width:100%;height:auto;' src='".BASEDIR."grims_blog/images/calhead/forest.jpg'></td>\n";
echo "</tr><tr height='30' bgcolor='#960000'>\n";
echo "<td align='left'>&nbsp;<a class='mcaldate' href='".FUSION_SELF."?month=$prev_month&amp;year=$prev_year' title='Last Month'>".$locale['gb_511']."</a></td><td align='center'><span class='mnthhd'>".date("F Y",strtotime($cYear."-".$cMonth."-01"))."</span></td><td align='right'><a class='mcaldate' href='".FUSION_SELF."?month=$next_month&amp;year=$next_year' title='Next Month'>".$locale['gb_512']."</a>&nbsp;</td>\n";
echo "</tr></table>\n";
echo "<table width='67%' align='center' border='0' cellpadding='0' cellspacing='0'><tr><td class='cal-blank'>\n";
echo "<table width='100%' align='center' border='0' cellpadding='0' cellspacing='0'><tr bgcolor='#005E00' height='35'>\n";
    foreach ($short_days as $key=>$val) {
        echo "<th style='text-align:center' width='14%'><span style='font-size:12px; color:yellow;'><b>".$val."</b></span></th>\n"; 
    }
echo "</tr><tr>\n";
$s=date('w', mktime (0,0,0,$cMonth,1,$cYear));
for ($ds=1; $ds<=$s; $ds++) {
echo "<td class='cal-blank' height='35' style='text-align:center;' valign='middle'></td>\n";
}
for ($d=1; $d<=$endDate; $d++) {
if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 0) { echo "<tr>\n"; }
$result = dbquery("SELECT post_id, post_title, post_date FROM ".DB_GRIMS_BLOG_POST." WHERE active='1' AND  MONTH(post_date) = '$cMonth' AND DAY(post_date) = '$d'");
$data = dbarray($result);
	$stuff1 = $data['post_id'] ? $data['post_id'] : "";
		if ($stuff1) {
		$id = $stuff1;
	} else { $id = ""; }
	$stuff2 = $data['post_title'] ? $data['post_title'] : "";
		if ($stuff2) {
		$title = $stuff2;
	} else { $title = ""; }
	$stuff3 = strtotime($data['post_date']) ? strtotime($data['post_date']) : "";
		if ($stuff3) {
		$evday = $stuff3;
	} else { $evday = ""; }
if (date("d", intval($evday)) == $d) {
echo "<td height='35' class='event' align='center' valign='middle'>\n";
} elseif ($d == $day && $cMonth == date('m') && $cYear == date('Y')) {
echo "<td height='35' class='today' align='center' valign='middle'>\n";
} else {
echo "<td height='35' class='cal-norm' align='center' valign='middle'>\n";
}
if ($d == $day && $cMonth == date('m') && $d <> $evday) {
echo "<a class='cal2' href='".BASEDIR."grims_blog/filtered.php?post_id=$id' title='".$locale['gb_513']." $title'><b>$d</b></a>\n";
} elseif ($d == $day && $cMonth == date('m') && $d == $evday) {
echo "<a class='cal2' href='#'><b>$d</b></a>\n";
} elseif ($evday) {
echo "<a class='ecal' href='".BASEDIR."grims_blog/filtered.php?post_id=$id' title='$title'><b>$d</b></a>\n";
} else {
echo "<span class='noevt'><b>$d</b></span>\n";
}
echo "</td>\n";
if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 6) { echo "</tr>\n"; }
}
echo "</table></td></tr></table>\n";
echo "</td></tr></table><p></div>\n";
?>

Well I could not add comments to the posted script. These 3 lines of the db query area give the error in my error log:

1) $stuff1 = $data['post_id'] ? $data['post_id'] : ""; /* Trying to access array offset on value of type bool */

2) $stuff2 = $data['post_title'] ? $data['post_title'] : ""; /* Trying to access array offset on value of type bool */

3) $stuff3 = strtotime($data['post_date']) ? strtotime($data['post_date']) : ""; /* Trying to access array offset on value of type bool */

I know I am asking a lot for help on older code but would really appreciate it if someone can take a look.

Edited by OldGrim
Clarify error lines

This is the output of var_dump($data);

array(3) { ["post_id"]=> string(2) "35" ["post_title"]=> string(8) "Fake Day" ["post_date"]=> string(10) "2021-04-01" }
bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)
array(3) { ["post_id"]=> string(2) "36" ["post_title"]=> string(12) "Carbon Tales" ["post_date"]=> string(10) "2021-04-18" }
bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)

Actually it's dead on. If you count the 2 events plus the bool{false} items they total 30 items (30 days in April). The dbquery is inside the forLoop which counts and prints the days of the month in the calendar. I have tried many many different scenarios of having the query outside the forLoop but they just won't work. If I could solve the "bool{false}" counts I'd be home free. In older versions of PHP this wasn't a problem but if I lower the active PHP version version it causes other problems that are even more difficult to solve. I may be asking the impossible here but I thought I'd try. You guys are all top notch PHP people.

So you called dbarray() 30 times. Twice it returned the array containing post data and 28 times it returned false.

Yet every time you try to process it as though they all contain post data.

Check that $data is not false before trying to get the $stuff.

  • Solution

Thanks again Barand. This now works fine.

$result = dbquery("SELECT post_id, post_title, post_date FROM ".DB_GRIMS_BLOG_POST." WHERE active='1' AND  MONTH(post_date) = '$cMonth' AND DAY(post_date) = '$d'");
$data = dbarray($result);
	if (empty($data['post_id'])) {
		$id = null;
	} else { $id = $data['post_id']; }
	if (empty($data['post_title'])) {
		$title = null;
	} else { $title = $data['post_title']; }
	if (empty($data['post_date'])) {
		$evday = null;
	} else { $evday = strtotime($data['post_date']); }

As I said before you guys are the greatest! Mainly because you know how to make US work for it and figure it out ourselves. :)

or (cleaner) ...

$data = dbarray($result);
if (!$data) {
    $id = $title = $evday = null;
}
else {
    $id = $data['post_id'];
    $title = $data['post_title'];
    $evday = strtotime($data['post_date']);
}

 

3 hours ago, Barand said:

$data = dbarray($result); if (!$data) { $id = $title = $evday = null; } else { $id = $data['post_id']; $title = $data['post_title']; $evday = strtotime($data['post_date']); }

Do you actually mean:

$data = dbarray($result);
	if (!$data) {
		$id = null; $title = null; $evday = null;
	} else {
		$id = $data['post_id'];
		$title = $data['post_title'];
		$evday = strtotime($data['post_date']);
	}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.