Reply to Re: creating a cumulative total column, but not in sequence

Your name:

Reply:


Posted by mantrid on 12/31/07 21:10

"Ivαn Sαnchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:flb9cc$s85$1@hercules.cohp1...
> mantrid wrote:
>
> > it is missing out any records with duplicate selldates. How do I tell it
> > to include duplicates?
>
> You cannot, it's how ordered maps work.
>
> You can, however, use a custom ordering function (see usort() ) and forget
> creating a second array altogether.
>
> --
> ----------------------------------
> Ivαn Sαnchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
> Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net

Thanks again Ivan
Looked at usort and the example given. I applied it to my situation and came
up with

*******************
while($row = mysql_fetch_array($transactions)) {
extract($row);
$data[ $row['heldlength'] ] = $row;
}

ksort($data);
$accum = 0;
$heldorder = 0;
foreach($data as $key=>$item) {

if ($item['profnloss'] > 0){
$data[$key]['accum'] = $accum += $item['profnloss'];
$data[$key]['heldorder'] = $heldorder += 1;

}else{
$data[$key]['accum'] = $accum;
$data[$key]['heldorder'] = $heldorder += 1;
}
}

function usort_cmp($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}


usort($data, "usort_cmp");

******************************
Too my suprise, it works perfectly. Ever heard the one where you give a
group of monkeys pens and paper and given enough time they will write the
complete works of shakespere. I think something similar has happened here. I
ve solved it but havent a clue how?
I am particularly puzzled by what the function

function usort_cmp($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}

is doing exactly. Particularly where argument $b comes from, and how it and
the returned values -1,0 and 1 are affecting the ordering of the array.

If anyone wants to explain this to me in simple english I'd be grateful. If
not, no worries Im just glad it works. Been on this for 4 days now.
Ian

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация