Solved

Duplicate values in array from SQL query - PHP

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl to output a url 7 48
ajax to record click 3 16
Extracting content from meta tag PHP MYHTML DOM 3 21
Giant ibd file for our biggest table on mysql 2 18
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

828 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