Solved

Duplicate values in array from SQL query - PHP

Posted on 2011-03-23
2
327 Views
Last Modified: 2012-05-11
I am querying a database for a list of activities which I wish to display in a month-view calendar matrix.  Obviously I don't want to make a query for each day of the month(!); so I have to split the results so that each day's activities can be looped separately as needed.

I now have a multidimensional array with sub-levels selected by day of the month - which is what I want.  However, this sub-array(?) shows all its values twice, each one against two keys, as this test output shows:

[acts] => Array
(
      [6] => Array
            (
                  [0] => 6
                  [day] => 6
                  [1] => 28244
                  [aid] => 28244
                  [2] => activity
                  [activityname] => activity
                  [3] => 28336
                  [enid] => 28336
            )

      [7] => Array
            (
                  [0] => 7
                  [day] => 7
                  [1] => 28245
                  [aid] => 28245
                  [2] => another activity
                  [activityname] => another activity
                  [3] => 28337
                  [enid] => 28337
            )

      [15] => Array
            (
                  [0] => 15
                  [day] => 15
                  [1] => 28246
                  [aid] => 28246
                  [2] => yet another activity
                  [activityname] => yet another activity
                  [3] => 28338
                  [enid] => 28338
            )

      [17] => Array
            (
                  [0] => 17
                  [day] => 17
                  [1] => 28247
                  [aid] => 28247
                  [2] => yet another activity still
                  [activityname] => yet another activity still
                  [3] => 28339
                  [enid] => 28339
            )

)

I suspect that this is why my attempt to display this data in the correct place on the matrix fails.

The original query, array reorganisation, and function to select the correct data (if any) for each day is this:
$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`") or die ("<p>The system is currently unable to respond to your request. Please try later.</p>");

// Reorganise the events into an array keyed by day of the month
$acts = array();
while ($row = mysql_fetch_array($sql)) {
	if (!$acts[$row['day']]) $acts[$row['day']] = array();
	$acts[$row['day']] = $row;
}

//Insert activities for day in that day's cell
function insert_acts() {
	if ($acts[$day]) {
		foreach ($acts[$day] AS $row) {
			//Clean & display events
			$this_aid = htmlentities($row['aid']);
			$this_actname = htmlentities($row['activityname']);
			$this_enid = htmlentities($row['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


How can I fix this problem?
0
Comment
Question by:hnmcc
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35199458
replace :
mysql_fetch_array

by :
mysql_fetch_assoc
0
 

Author Closing Comment

by:hnmcc
ID: 35201551
Exactly right: thanks!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question