Pentacore Posted January 19, 2013 Share Posted January 19, 2013 (edited) so is there any way to fetch what array to fetch the data from in the Function input? this is what i got so far function onHoverCounter( ChampID ) { var show1 = ChampID[1]; $("#"+show1).addClass('counter'); } ChampID is fetched from <a href="?champion=ahri"><div class="champion apcarry mid" id="ahri" onmouseover="onHoverCounter(this.id);" onmouseout="onmouseoutCounter(this.id);"><img src="img/champions/ahri.jpg"> Ahri </div></a> Edited January 19, 2013 by Pentacore Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/ Share on other sites More sharing options...
requinix Posted January 20, 2013 Share Posted January 20, 2013 so is there any way to fetch what array to fetch the data from in the Function input? Depends: has anyone really been far even as decided to use even go to want to do look more like? On a more serious note, What? Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407056 Share on other sites More sharing options...
Pentacore Posted January 20, 2013 Author Share Posted January 20, 2013 Depends: has anyone really been far even as decided to use even go to want to do look more like? On a more serious note, What? ya i just read that through.. sorry about that what i mean is i want to get the name of the array that the function use, by using the Fucntion input, is there a way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407069 Share on other sites More sharing options...
cpd Posted January 20, 2013 Share Posted January 20, 2013 You're still not making sense, not to me anyway. You're passing the element ID to the method and then attempting to add a class to the element. There is no array involved so I've no idea what you're referring to when you talk about "the array that the function usesssss" Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407088 Share on other sites More sharing options...
salathe Posted January 20, 2013 Share Posted January 20, 2013 As cpd said, there is no array at the moment (which is probably what is confusing us, and you). Your onHoverCounter() function takes the id of the <div> that you're attaching it to, e.g. ahri. This is a string value, not an array. Your function then gets the second letter of that string when doing ChampID[1] (JS, like PHP, allows you to access individual letters in a string by their zero-based offset). So in your example, show1 contains the string h. Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407094 Share on other sites More sharing options...
Pentacore Posted January 20, 2013 Author Share Posted January 20, 2013 (edited) As cpd said, there is no array at the moment (which is probably what is confusing us, and you). Your onHoverCounter() function takes the id of the <div> that you're attaching it to, e.g. ahri. This is a string value, not an array. Your function then gets the second letter of that string when doing ChampID[1] (JS, like PHP, allows you to access individual letters in a string by their zero-based offset). So in your example, show1 contains the string h. You're still not making sense, not to me anyway. You're passing the element ID to the method and then attempting to add a class to the element. There is no array involved so I've no idea what you're referring to when you talk about "the array that the function usesssss" oh srry i forgot to include this part, the array's exists and is created through PHP $counters; $mysqli = new mysqli($host, $sqluser, $sqlpass, "counters"); foreach (glob("img/champions/*.jpg") as $filename) { if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } $path = $filename; $file = basename($path); $file = basename($path, ".jpg"); $search = "SELECT * FROM counters WHERE `IMG_Name`='".$file."'"; if ($searchresult = mysqli_query($mysqli, $search)) { /* fetch associative array */ while ($row = mysqli_fetch_assoc($searchresult)) { ${$file}["1"] = $row["Counter 1"]; ${$file}["2"] = $row["Counter 2"]; ${$file}["3"] = $row["Counter 3"]; ${$file}["4"] = $row["Counter 4"]; ${$file}["5"] = $row["Counter 5"]; ${$file}["6"] = $row["Counter 1"]; ${$file}["7"] = $row["Counter 2"]; ${$file}["8"] = $row["Counter 3"]; ${$file}["9"] = $row["Counter 4"]; ${$file}["10"] = $row["Counter 5"]; ${$file}["11"] = $row["Good With 1"]; ${$file}["12"] = $row["Good With 2"]; ${$file}["13"] = $row["Good With 3"]; ${$file}["14"] = $row["Good With 4"]; ${$file}["15"] = $row["Good With 5"]; } } echo $file." = ".json_encode(${$file})." ,"; } ?> And an example of an array that gets created: var ahri = {"1":"ryze","2":"fizz","3":"Akali","4":"LeBlanc","5":"Diana","6":"ryze","7":"fizz","8":"Akali","9":"LeBlanc","10":"Diana","11":"Jax","12":"Irelia","13":"Shen","14":"Ezreal","15":"Amumu"} Edited January 20, 2013 by Pentacore Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407136 Share on other sites More sharing options...
salathe Posted January 20, 2013 Share Posted January 20, 2013 Using variable variables, as you are, seems a lot like going out of your way to make life more difficult. Why not use a nested array like $files[$file][1]. This will allow you to have a nice, single, object in Javascript containing all of the information for all of the files. If you just want to take what you have right now and do something with it, it sounds like you're looking for a way to reference the ahri variable from the this.id string. Assuming the variables are created in the window-level scope (it looks like they are) then you can, inside your onHoverCounter() function, use something like window[ChampID] to refer to that variable by name. For example: var file = window[ChampID]; var show1 = file[1]; ... Quote Link to comment https://forums.phpfreaks.com/topic/273364-getting-the-name-of-the-array-to-fetch-data-from-in-the-function-input/#findComment-1407153 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.