Solved

Duplicate values in array from SQL query - PHP

Posted on 2011-03-23
2
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 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