This is a discussion on Re: [PHP] Array Push and Keys within the PHP General forums, part of the PHP Programming Forums category; On Thu, 28 Aug 2003 18:14:39 -0400, you wrote: >While($row=mysql_fetch_array($res) { >That *should* create ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
On Thu, 28 Aug 2003 18:14:39 -0400, you wrote:
>While($row=mysql_fetch_array($res) { >That *should* create the array above, but as I mentioned array_push does not >seem to be taking the keys.... I just get a Parse error message for the >line. (the first array_push) Ahem. Count the brackets? :) (On a style point, your code would be easier to read for bugs like this if you added some whitespace.) |
|
|||
|
Correct on the brackets - but not on the results still working... The code
I had in the email was just a sample - here is the real code... $res=mysql_query("select distinct dealercode from transactions order by dealercode",$intranet); $transactions=Array(); while($row=mysql_fetch_array($res)) { $amt=mysql_query("select sum(amount) as total from transactions where date between '2003-04-01' and '2003-06-31' and dealercode='$row[dealercode]'",$intranet); $afftotal=mysql_fetch_array($amt); array_push($transactions,$row[dealercode]=>Array("onlineu",$afftotal[total]) ); $amt=mysql_query("select usercountry,sum(amount) as total from orders_placed where date between '2003-04-01' and '2003-06-31' and dealercode='$row[dealercode]' and sent='y' group by usercountry",$intranet); while($cdtotal=mysql_fetch_array($amt)) { array_push($transactions[$row[dealercode]],$cdtotal[usercountry]=>$cdtotal[t otal]); } } And as for your offline suggestion of putting it into one query - I did try that first of course, but due to the large amount of data, and the shoddy nature of the database design (inherited not built) the query times out. This atleast returns within 1 min with the data when I can retrieve it :) Ideally the database would have one table with all the transaction information in it, and when I redesign it will, but in the meantime I am stuck with this... On 8/28/03 9:09 PM, "David Otton" <phpmail@jawbone.freeserve.co.uk> wrote: > On Thu, 28 Aug 2003 18:14:39 -0400, you wrote: > >> While($row=mysql_fetch_array($res) { > >> That *should* create the array above, but as I mentioned array_push does not >> seem to be taking the keys.... I just get a Parse error message for the >> line. (the first array_push) > > Ahem. Count the brackets? :) > > (On a style point, your code would be easier to read for bugs like this if > you added some whitespace.) -- Cheers Mike Morton ************************************************** ** * * Tel: 905-465-1263 * Email: mike@webtraxx.com * ************************************************** ** "Indeed, it would not be an exaggeration to describe the history of the computer industry for the past decade as a massive effort to keep up with Apple." - Byte Magazine Given infinite time, 100 monkeys could type out the complete works of Shakespeare. Win 98 source code? Eight monkeys, five minutes. -- NullGrey |
|
|||
|
On Fri, 29 Aug 2003 07:54:45 -0400, you wrote:
>Correct on the brackets - but not on the results still working... The code >I had in the email was just a sample - here is the real code... Ack. For anyone watching, if you have a problem please post a minimal code snippet that illustrates the problem. Test it before you send. Otherwise you may be wasting the time of anyone who tries to help you. >$res=mysql_query("select distinct dealercode from transactions order by >dealercode",$intranet); >$transactions=Array(); >while($row=mysql_fetch_array($res)) { > $amt=mysql_query("select sum(amount) as total from transactions where >date between '2003-04-01' and '2003-06-31' and >dealercode='$row[dealercode]'",$intranet); > $afftotal=mysql_fetch_array($amt); > >array_push($transactions,$row[dealercode]=>Array("onlineu",$afftotal[total]) >); > $amt=mysql_query("select usercountry,sum(amount) as total from >orders_placed where date between '2003-04-01' and '2003-06-31' and >dealercode='$row[dealercode]' and sent='y' group by usercountry",$intranet); > while($cdtotal=mysql_fetch_array($amt)) { > >array_push($transactions[$row[dealercode]],$cdtotal[usercountry]=>$cdtotal[t >otal]); > } >} Ok, is this a MySQL database? date is a reserved word. "$row[dealercode]" should be "$row['dealercode']", same goes for the other dictionaries. Your code should be throwing a notice on that, which suggests that your error-level is set low and you may be missing other problems. You want a structure like: array ( 33 = array ( 'a' = 52.00 'b' = 53.00 'c' = 54.00 ) 34 = array ( 'a' = 55.00 'b' = 56.00 'c' = 57.00 ) ) What's it supposed to look like when 33=>'a' exists more than once? Is that possible? That array_push looks like a mess. Try something like... $data = array(); for each row in query : $dealerid = $row[0]; $data[$dealerid] = array(); for each subrow in subquery : $country = $subrow[0]; $amount = $subrow[1]; $data[$dealerid][$country] = $amount; >And as for your offline suggestion of putting it into one query - I did try >that first of course, but due to the large amount of data, and the shoddy >nature of the database design (inherited not built) the query times out. >This atleast returns within 1 min with the data when I can retrieve it :) Do you have a seperate dealer table? You're pulling "SELECT DISTINCT dealercode FROM transactions", which is odd. Somewhere you may have a table where dealername is associated with dealercode. That's what you should be doing the join against. If I were you I'd ask on a database-specific mailing list, because it's never faster to do it in software when you can push it all up into the database instead. |
|
|||
|
Look - David - I do appreciate your efforts to help - and I realize that
working blind on an email list is tough, but the question is not about the structure of the code, the database, reserved words or anything else. The question for anyone out there is simple - does array_push allow you to push key=>value pairs? That is what is causing errors - nothing else, everything else is a result of inherited systems, and other developers crappy design. I agree with you David on all the issues that you bring up - date IS a reserved word - but it still works - that is the previous developers mentality anyhow :) > "$row[dealercode]" should be "$row['dealercode']", same goes for the other > dictionaries. Your code should be throwing a notice on that, which suggests > that your error-level is set low and you may be missing other problems. Fair if I was coding for anyting other than a one time report - I am not going to spend a great deal of time with 'proper' coding for a one time report that does not matter beyond it working once. > You want a structure like: > > array ( > 33 = array ( > 'a' = 52.00 > 'b' = 53.00 > 'c' = 54.00 > ) > 34 = array ( > 'a' = 55.00 > 'b' = 56.00 > 'c' = 57.00 > ) > ) yes > What's it supposed to look like when 33=>'a' exists more than once? Is that > possible? It does not 33=>a does not occur more than once. > That array_push looks like a mess. Try something like... > > $data = array(); > > for each row in query : > > $dealerid = $row[0]; > $data[$dealerid] = array(); > > for each subrow in subquery : > $country = $subrow[0]; > $amount = $subrow[1]; > > $data[$dealerid][$country] = $amount; This is what I am doing now, and yes it works, but this does not answer my original question. > Do you have a seperate dealer table? You're pulling "SELECT DISTINCT > dealercode FROM transactions", which is odd. Somewhere you may have a table > where dealername is associated with dealercode. That's what you should be > doing the join against. > > If I were you I'd ask on a database-specific mailing list, because it's > never faster to do it in software when you can push it all up into the > database instead. Thanks for the opinion - but I have tried it both ways. Software in this case is faster. 25 seconds execution in PHP, 3 mins in mysql. But this STILL does not answer the original question. -- Cheers Mike Morton ************************************************** ** * * Tel: 905-465-1263 * Email: mike@webtraxx.com * ************************************************** ** "Indeed, it would not be an exaggeration to describe the history of the computer industry for the past decade as a massive effort to keep up with Apple." - Byte Magazine Given infinite time, 100 monkeys could type out the complete works of Shakespeare. Win 98 source code? Eight monkeys, five minutes. -- NullGrey |
|
|||
|
And as a follow up, from the phpbuilder list where I posted the very same
question today, I think this answers the question: "I don't think its a bug. I think the problem you're having is to do with what type of argument you can give to array_push. There are two possibilities ... 1) variable 2) array If you pass a single item (1), then no problem, works as expected. However, if you pass an array(2), then it concats this array onto the target array. So, maybe its taking for 2nd argument array($key => array($u, $v)) and converting into array($key, array($u, $v)) and then concat'ing it to the target array. " On 8/29/03 11:18 AM, "Mike Morton" <mike@webtraxx.com> wrote: > Look - David - I do appreciate your efforts to help - and I realize that > working blind on an email list is tough, but the question is not about the > structure of the code, the database, reserved words or anything else. > > The question for anyone out there is simple - does array_push allow you to > push key=>value pairs? That is what is causing errors - nothing else, > everything else is a result of inherited systems, and other developers > crappy design. I agree with you David on all the issues that you bring up - > date IS a reserved word - but it still works - that is the previous > developers mentality anyhow :) > > >> "$row[dealercode]" should be "$row['dealercode']", same goes for the other >> dictionaries. Your code should be throwing a notice on that, which suggests >> that your error-level is set low and you may be missing other problems. > > Fair if I was coding for anyting other than a one time report - I am not > going to spend a great deal of time with 'proper' coding for a one time > report that does not matter beyond it working once. > >> You want a structure like: >> >> array ( >> 33 = array ( >> 'a' = 52.00 >> 'b' = 53.00 >> 'c' = 54.00 >> ) >> 34 = array ( >> 'a' = 55.00 >> 'b' = 56.00 >> 'c' = 57.00 >> ) >> ) > > yes > >> What's it supposed to look like when 33=>'a' exists more than once? Is that >> possible? > > It does not 33=>a does not occur more than once. > >> That array_push looks like a mess. Try something like... >> >> $data = array(); >> >> for each row in query : >> >> $dealerid = $row[0]; >> $data[$dealerid] = array(); >> >> for each subrow in subquery : >> $country = $subrow[0]; >> $amount = $subrow[1]; >> >> $data[$dealerid][$country] = $amount; > > This is what I am doing now, and yes it works, but this does not answer my > original question. > >> Do you have a seperate dealer table? You're pulling "SELECT DISTINCT >> dealercode FROM transactions", which is odd. Somewhere you may have a table >> where dealername is associated with dealercode. That's what you should be >> doing the join against. >> >> If I were you I'd ask on a database-specific mailing list, because it's >> never faster to do it in software when you can push it all up into the >> database instead. > > Thanks for the opinion - but I have tried it both ways. Software in this > case is faster. 25 seconds execution in PHP, 3 mins in mysql. But this > STILL does not answer the original question. > > > > > > > -- > Cheers > > Mike Morton > > ************************************************** ** > * > * Tel: 905-465-1263 > * Email: mike@webtraxx.com > * > ************************************************** ** > > "Indeed, it would not be an exaggeration to describe the history of the > computer industry for the past decade as a massive effort to keep up with > Apple." > - Byte Magazine > > Given infinite time, 100 monkeys could type out the complete works of > Shakespeare. Win 98 source code? Eight monkeys, five minutes. > -- NullGrey -- Cheers Mike Morton ************************************************** ** * * Tel: 905-465-1263 * Email: mike@webtraxx.com * ************************************************** ** "Indeed, it would not be an exaggeration to describe the history of the computer industry for the past decade as a massive effort to keep up with Apple." - Byte Magazine Given infinite time, 100 monkeys could type out the complete works of Shakespeare. Win 98 source code? Eight monkeys, five minutes. -- NullGrey |
|
|||
|
On Fri, 29 Aug 2003 11:18:18 -0400, you wrote:
>The question for anyone out there is simple - does array_push allow you to >push key=>value pairs? That is what is causing errors - nothing else, >everything else is a result of inherited systems, and other developers >crappy design. I agree with you David on all the issues that you bring up - >date IS a reserved word - but it still works - that is the previous >developers mentality anyhow :) >Fair if I was coding for anyting other than a one time report - I am not >going to spend a great deal of time with 'proper' coding for a one time >report that does not matter beyond it working once. This list is used as a learning resource by a lot of people, not just thee and me. If I didn't correct an error when I saw it, I might be causing someone to think that was valid code. >> $data[$dealerid][$country] = $amount; > >This is what I am doing now, and yes it works, but this does not answer my >original question. Yeah, I see that now - I'm sorry that we ended up talking at cross-purposes. In my defence you posted a large lump of code, including database queries, so I assumed you were asking the standard "how do I get this code to produce this structure" question, rather than asking about the behaviour of a single function. The question quoted above (first paragraph) is much clearer, and got you an answer from Mike Ford within 15 minutes. There's a lesson for both of us here :) |