Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Why is this loop not working?

I have an array drawn from a database.  It is a series of activities on various dates: I need to display the activities on the appropriate date in a calendar matrix (month view).

Currently, the loop I'm using doesn't work;  I have (with some help) created parts of a multidimensional array that are keyed by "dayofmonth", which is what I need.  But the loop doesn't work at all.  PHP tells me that there are undefined indices (line 6, first chunk) and undefined variables ("$day" & "$act", line 2, second chunk - flagged 31 times for each variable) in the code below.

The code in question is:

$sql = mysql_query("SELECT DAYOFMONTH(a.`date`) AS day, a.`aid`, a.`activityname`, u.`enid` FROM activities a JOIN user_entries u JOIN diaries d ON a.`aid` = u.`aid_FK` AND u.`did_FK` = d.`did` WHERE `mem_no_FK` = 2353 AND a.`date` BETWEEN '2009-07-01' AND '2009-08-01' ORDER BY `date`"));
	
// Reorganise the events into an array keyed by day of the month
$acts = array();
while ($row = mysql_fetch_assoc($sql)) {
	if (!$acts[$row['day']]) $acts[$row['day']] = array();
	$acts[$row['day']] = $row;
}

Open in new window

and
	function insert_acts() {
if ($acts[$day]) {
	foreach ($acts[$day] AS $this_cell) {
		//Clean & display events
		$this_aid = htmlentities($this_cell['aid']);
		$this_actname = htmlentities($this_cell['activityname']);
		$this_enid = htmlentities($this_cell['enid']);
		print "<div><a href=\"CPD_activity_details.php?aid=$this_aid\" class=\"\">$this_actname</a><br /><a href=\"CPD_activity_review.php?enid=$this_enid\" class=\"\">review</a></div>";
	}
	print "</td>";
} else {
	print "</td>";
}
}

Open in new window

Using a test rig, what's stored is:
[acts] => Array
        (
            [6] => Array
                (
                    [day] => 6
                    [aid] => 28244
                    [activityname] => DoH Leadership workshop
                    [enid] => 28336
                )

            [7] => Array
                (
                    [day] => 7
                    [aid] => 28245
                    [activityname] => Timeout and senior team meeting
                    [enid] => 28337
                )

            [15] => Array
                (
                    [day] => 15
                    [aid] => 28246
                    [activityname] => journal club
                    [enid] => 28338
                )

            [17] => Array
                (
                    [day] => 17
                    [aid] => 28247
                    [activityname] => SRSB and RNIB Losing patients event
                    [enid] => 28339
                )

            [22] => Array
                (
                    [day] => 22
                    [aid] => 28248
                    [activityname] => TBI meeting
                    [enid] => 28340
                )

        )

    [row] =>
I'm at a loss.  What does this mean?  And how do I make it work?
0
hnmcc
Asked:
hnmcc
  • 5
  • 3
2 Solutions
 
tsmgeekCommented:
$sql = mysql_query("SELECT DAYOFMONTH(a.`date`) AS day, a.`aid`, a.`activityname`, u.`enid` FROM activities a JOIN user_entries u JOIN diaries d ON a.`aid` = u.`aid_FK` AND u.`did_FK` = d.`did` WHERE `mem_no_FK` = 2353 AND a.`date` BETWEEN '2009-07-01' AND '2009-08-01' ORDER BY `date`"));
        
// Reorganise the events into an array keyed by day of the month
$acts = array();
while ($row = mysql_fetch_assoc($sql)) {
	# this will allow multiple entries per day []
        $acts[$row['day']][] = $row;
}

	function insert_acts() {
#check to see if $acts has been set and is an array
if (isset($acts) && is_array($acts)) {
	# loop each day
	foreach ($acts as $acts_day) {
		# loop though each event for day
                foreach($acts_day as $this_cell){
			//Clean & display events
			$this_aid = htmlentities($this_cell['aid']);
			$this_actname = htmlentities($this_cell['activityname']);
			$this_enid = htmlentities($this_cell['enid']);
			print "<div><a href=\"CPD_activity_details.php?aid=$this_aid\" class=\"\">$this_actname</a><br /><a href=\"CPD_activity_review.php?enid=$this_enid\" class=\"\">review</a></div>";
		}
	}
}

Open in new window

0
 
hnmccAuthor Commented:
Thanks tsmgeek - I like the changes and have adopted them.

But...

Still no data displaying when it should (i.e. on 6, 7, 15, 17, & 22 July 2009).  And still PHP reports "Undefined index" for each of these days, referring to line 4 of the chunk below.

 	// Reorganise the events into an array keyed by day of the month
	$acts = array();
	while ($row = mysql_fetch_assoc($sql)) {
		if (!$acts[$row['day']]) $acts[$row['day']] = array();
		# empty[] allows more than one activity per day
		$acts[$row['day']][] = $row;
	}

Open in new window


And the test rig shows the same variables set - different only in that the intermediate key in Sacts is reinstated.

What is going wrong?
0
 
tsmgeekCommented:
take line 4 out as you dont need to predefine sub arrays ahead of time, it will be fine
if you want to check an array you need to use is_array() command
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
hnmccAuthor Commented:
Thanks again tsmgeek - the error notices have disappeared, which is a definite improvement!

It seems to me that the problem here is in the test: $day is not being compared to the 'day' value in the acts array.  The test cannot be passed, so the results are never displayed.

I had assumed that the table was working because the </td> tag was being pulled out of the function.  But it wasn't: the browsers (all of them) were rendering the table accurately with unclosed <td> tags throughout.  Which seems to mean that  function insert_acts() is being ignored completely.

This is the code as it stands:
// Reorganise the events into an array keyed by day of the month
$acts = array();
while ($row = mysql_fetch_assoc($sql)) {
	#if (!$acts[$row['day']]) $acts[$row['day']] = array();
	# empty[] allows more than one activity per day
	$acts[$row['day']][] = $row;
}

//Insert activities for day in that day's cell
function insert_acts() {
	# check to see if $acts has been set and is an array
	if (isset($acts) && is_array($acts)) {
		# loop each day
		foreach ($acts as $acts_day) {
			# loop though each event for day
			foreach($acts_day as $this_cell){
				//Clean & display events
				$this_aid = htmlentities($this_cell['aid']);
				$this_actname = htmlentities($this_cell['activityname']);
				$this_enid = htmlentities($this_cell['enid']);
				print "<div><a href=\"CPD_activity_details.php?aid=$this_aid\" class=\"\">$this_actname</a><br /><a href=\"CPD_activity_review.php?enid=$this_enid\" class=\"\">review</a></div>";
			}
		}
	}
#print "</td>";
}

Open in new window


Any thoughts?
0
 
tsmgeekCommented:
call the function where you then want the divs to be placed
ie
<table>
  <tr><td>
    <?php insert_acts(); ?>
  </td></tr>
</table>

Open in new window


or just take out lines 10 & 26
0
 
hnmccAuthor Commented:
In fact,  insert_acts()  as shown above (or the same code placed directly where it's needed) does NOT work.

We need to check that a matching key is set for each day, as in line 4 below.
if (isset($acts)) {
	if (is_array($acts)) {
		# loop each day
		if (array_key_exists($day,$acts)) {
		# loop though each event for day
			foreach($acts[$day] as $this_cell){
				//Clean & display events
				$this_aid = htmlentities($this_cell['aid']);
				$this_actname = htmlentities($this_cell['activityname']);
				$this_enid = htmlentities($this_cell['enid']);
				print "<div><a href=\"CPD_activity_details.php?aid=$this_aid\" class=\"\">$this_actname</a><br /><a href=\"CPD_activity_review.php?enid=$this_enid\" class=\"\">review</a></div>";
			}
		}
	}
}

Open in new window

0
 
hnmccAuthor Commented:
I intended to give some points to tsmgeek for his(?) assistance - but only some, as I answered the question correctly myself - and not in a way suggested by tsmgeek.
0
 
hnmccAuthor Commented:
Notice: hnmcc has requested that this question be closed by accepting tsmgeek's comment #35203689 (500 points) as the solution and hnmcc's comment #35222286 (0 points) as the assisted solution for the following reason:
PHP array functions...
To cancel this request and generate a request in Community Support for Moderator review, state your reason for objecting and click the 'Object' button. This question will be closed on 3/30/2011 if there are no objections.

I have not requested this.

I want tsmgeek to have some points for trying to help - but NOT all of them, because he/she didn't solve my problem or point me towards a way of solving it.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now