Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Duplicate values in array from SQL query - PHP

Posted on 2011-03-23
2
Medium Priority
?
366 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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 count occurrences of each item in an array.

722 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