simpli Posted March 24, 2009 Share Posted March 24, 2009 Hi, I downladed a js script that is supposed to replicate the array_count_values php function. It's my first time using arrays in php and I am stuck. I don't seem to be getting the data in the array. Obviously the array_count_values doesnt work but I think it's my implementation of arrays that doesnt work. I tried running the script I have no error in my browser so I don't know where to look now. Can anyone troubleshoot me? Thanks <script type='text/javascript'> function formValidator(){ // Make quick references to our fields var p1 = document.frmchoixronde1.player_1; var p2 = document.frmchoixronde1.player_2; var p3 = document.frmchoixronde1.player_3; var p4 = document.frmchoixronde1.player_4; var p5 = document.frmchoixronde1.player_5; var p6 = document.frmchoixronde1.player_6; var arrayPlayers = new Array(); arrayPlayers[0] = p1; arrayPlayers[1] = p2; arrayPlayers[2] = p3; arrayPlayers[3] = p4; arrayPlayers[4] = p5; arrayPlayers[5] = p6; alert(arrayPlayers[0]); // Validation of the text fields! if(PasDeDoublons(arrayPlayers)){ return true; } return false; } function PasDeDoublons( array) { var tmpArray= new Array(); var i = 0; tmpArray = array_count_values(array); for (i=0; i<=tmpArray.length;i++) { if(tmpArray[i] >= 1) { alert("Le joueur ne peut etre choisi deux fois"); return false; } } return true; } 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; } </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> Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/ Share on other sites More sharing options...
darkfreaks Posted March 24, 2009 Share Posted March 24, 2009 why are you using a variable as a function ??? and your switch statement is messed up Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792283 Share on other sites More sharing options...
simpli Posted March 24, 2009 Author Share Posted March 24, 2009 I don't understand what you mean. What variable am I using as a function. The switch may be screwed up. I didnt get to troubleshoot it. But it's not getting there ok so I need to solve this first. Can you elaborate on your first statement? Thanks, J-R Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792301 Share on other sites More sharing options...
darkfreaks Posted March 24, 2009 Share Posted March 24, 2009 run your javascript on jslint.com you will see what i mean Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792305 Share on other sites More sharing options...
simpli Posted March 24, 2009 Author Share Posted March 24, 2009 Here's the javascript I copied at jslint function PasDeDoublons( array) { var tmpArray= new Array(); var i = 0; tmpArray = array_count_values(array); for (i=0; i<=tmpArray.length;i++) { if(tmpArray[i] >= 1) { alert("Le joueur ne peut etre choisi deux fois"); return false; } } return true; } 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(){ // Make quick references to our fields var p1 = document.frmchoixronde1.player_1; var p2 = document.frmchoixronde1.player_2; var p3 = document.frmchoixronde1.player_3; var p4 = document.frmchoixronde1.player_4; var p5 = document.frmchoixronde1.player_5; var p6 = document.frmchoixronde1.player_6; var arrayPlayers = new Array(); arrayPlayers[0] = p1; arrayPlayers[1] = p2; arrayPlayers[2] = p3; arrayPlayers[3] = p4; arrayPlayers[4] = p5; arrayPlayers[5] = p6; alert(arrayPlayers[0]); // Validation of the text fields! if(PasDeDoublons(arrayPlayers)){ return true; } return false; } And here is the first error message I get: Problem at line 3 character 23: Use the array literal notation []. Well that's chinese to me. Even after I googled it I don't see what's wrong with the way I declare my array. That's how it's declared on most tutorial sites. so I'm at a loss. J-R Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792311 Share on other sites More sharing options...
simpli Posted March 24, 2009 Author Share Posted March 24, 2009 I just replaced var tmpArray= new Array(); by var tmpArray= []; and so on everywhere but I still have the same issue Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792315 Share on other sites More sharing options...
darkfreaks Posted March 24, 2009 Share Posted March 24, 2009 yeah but your switch is missing breaks http://javascript.about.com/od/decisionmaking/a/des11.htm Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792325 Share on other sites More sharing options...
simpli Posted March 24, 2009 Author Share Posted March 24, 2009 I don't think the switch is the problem because my alert that is placed much before it doesn't return anything good. I think it's the assignment of the text inputs that is not working for some reason. To verify this I removed the whole function that is bothering you. I'm concentrating on retrieving the information from the textboxes. But it's not working. I can troubleshoot the switch if there's a problem in it but since I have a more pressing issue, I'd like to solve it first. Anyway, thanks for your effort. Quote Link to comment https://forums.phpfreaks.com/topic/150797-solved-why-isnt-this-array_count_values-script-not-working-array-help/#findComment-792492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.