simpli Posted March 25, 2009 Share Posted March 25, 2009 Hi, I downloaded this javascript that is supposed to be the equivalent of php's array_count_values function but it doesn't seem to be working. While I posted on the website I downloaded it from I don't know how responsive they are. I am on a mac using firefox so I don't know if that's the problem. I am using one of their example to showcase the function. Have anyone used that function on a mac/firefox? (http://phpjs.org/functions/view/308) Can anyone try it on their side to see if it works as it should? If not can anyone help me troubleshoot it? I wanted to find a solution to equal the php function and be quick on my way but I'm stuck trying to debug this. Thanks in advance, J-R Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/ Share on other sites More sharing options...
simpli Posted March 25, 2009 Author Share Posted March 25, 2009 sorry I forgot to paste the code: <script type='text/javascript'> function array_count_values( array ) { // http://kevin.vanzonneveld.net // + original by: Ates Goral (http://magnetiq.com) // + namespaced by: Michael White (http://getsprink.com) // + input by: sankai // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); // * returns 1: {3:2, 5:1, "foo":2, "bar":1} // * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }); // * returns 2: {3:2, 5:1, "foo":2, "bar":1} // * example 3: array_count_values([ true, 4.2, 42, "fubar" ]); // * returns 3: {42:1, "fubar":1} var tmp_arr = {}, key = '', t = ''; var __getType = function(obj) { // Objects are php associative arrays. var t = typeof obj; t = t.toLowerCase(); if (t == "object") { t = "array"; } return t; } var __countValue = function (value) { switch (typeof(value)) { case "number": if (Math.floor(value) != value) { return; } case "string": if (value in this) { ++this[value]; } else { this[value] = 1; } } }; t = __getType(array); if (t == 'array') { for ( key in array ) { __countValue.call(tmp_arr, array[key]); } } return tmp_arr; } function formValidator(){ var tmpArray= []; tmpArray = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); document.write(tmpArray[0] + tmpArray[1]+ tmpArray[2]); document.write(tmpArray[3] + tmpArray[4]+ tmpArray[5]); // document.write(tmpArray[0] + " " + tmpArray[1]+ " " + tmpArray[2] + " " + tmpArray[3]+ " " + tmpArray[4]+ " " + tmpArray[5]); } </script> <form id="frmchoixronde1" name="frmchoixronde1" method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'> <html><head><title>Page des poolers</title></head> <body> <b>Entrez vos choix pour la premiere ronde</b> </br><fieldset> <b><label for="player_1" style="width:2em">1</label></b><input name="player_1" id="player_1" type="text" size="30"></br> <b><label for="player_2" style="width:2em">2</label></b><input name="player_2" id="player_2" type="text" size="30"></br> <b><label for="player_3" style="width:2em">3</label></b><input name="player_3" id="player_3" type="text" size="30"></br> <b><label for="player_4" style="width:2em">4</label></b><input name="player_4" id="player_4" type="text" size="30"></br> <b><label for="player_5" style="width:2em">5</label></b><input name="player_5" id="player_5" type="text" size="30"></br> <b><label for="player_6" style="width:2em">6</label></b><input name="player_6" id="player_6" type="text" size="30"></br> <input type="submit" name="submit" value="Soumettre vos choix"> </form> </br> </body></html> Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793248 Share on other sites More sharing options...
darkfreaks Posted March 25, 2009 Share Posted March 25, 2009 can you setup a live demo i dont see one they have ??? Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793254 Share on other sites More sharing options...
simpli Posted March 25, 2009 Author Share Posted March 25, 2009 I don't understand your question. Can you expand? Thank you. J-R Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793260 Share on other sites More sharing options...
darkfreaks Posted March 25, 2009 Share Posted March 25, 2009 i corrected most of the errors let me know if it works ??? function array_count_values( array ) { // http://kevin.vanzonneveld.net // + original by: Ates Goral (http://magnetiq.com) // + namespaced by: Michael White (http://getsprink.com) // + input by: sankai // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); // * returns 1: {3:2, 5:1, "foo":2, "bar":1} // * example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" }); // * returns 2: {3:2, 5:1, "foo":2, "bar":1} // * example 3: array_count_values([ true, 4.2, 42, "fubar" ]); // * returns 3: {42:1, "fubar":1} var tmp_arr = {}, key = '', t = ''; var __getType = function(obj) { // Objects are php associative arrays. var t = typeof obj; t = t.toLowerCase(); if (t == "object") { t = "array"; } return t; } ;var __countValue = function (values) { switch (typeof(values)) { case "number": if (Math.floor(values) != values) { return; } case "string": if (values in this) { ++this[svalues]; break; } else { this[values] = 1; break; } } } ;t = __getType(array); if (t == 'array') { for ( key in array ) { __countValue.call(tmp_arr, array[key]); } } return tmp_arr; } function formValidator(){ var tmpArray= []; tmpArray = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]); document.write(tmpArray[0] + tmpArray[1]+ tmpArray[2]); document.write(tmpArray[3] + tmpArray[4]+ tmpArray[5]); // document.write(tmpArray[0] + " " + tmpArray[1]+ " " + tmpArray[2] + " " + tmpArray[3]+ " " + tmpArray[4]+ " " + tmpArray[5]); } Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793273 Share on other sites More sharing options...
simpli Posted March 25, 2009 Author Share Posted March 25, 2009 Hi, and thank you for your help. I ran the code you changed but it still doesn't work. I tried to see what change you brought to it and I couldn't see any except that you change value for values. There was a typo where you had replaced value with svalues but I corrected it. Can you tell me what other change you've done if any. Maybe by teaming up we can get it to work. Thanks again, J-R Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793282 Share on other sites More sharing options...
darkfreaks Posted March 25, 2009 Share Posted March 25, 2009 i added some missing semi colons and break statements only other error i get now is "Missing default case statement" Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793286 Share on other sites More sharing options...
simpli Posted March 25, 2009 Author Share Posted March 25, 2009 When you run do you get anything at all? I don't that's what's funny. And I get no error messages either. I'm gonna try to sleep on it see what I can do tomorrow morning. Thank you and good night. J-R Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-793293 Share on other sites More sharing options...
simpli Posted March 26, 2009 Author Share Posted March 26, 2009 I got it. The result is returned as an associative array. so I must use the associative form to access it. J-R Link to comment https://forums.phpfreaks.com/topic/150986-solved-array_count_values-javascript-equivalent-not-working/#findComment-794070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.