Solved

Duplicate values in array from SQL query - PHP

Posted on 2011-03-23
2
310 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now