
jonnyenglish89
Members-
Posts
16 -
Joined
-
Last visited
Everything posted by jonnyenglish89
-
Great, I'll make the changes. thank you for the advice!
-
Hi, I’d appreciate some help with my homework. I’ve got to display each student’s name and the results for each of their modules using vanilla JavaScript. the following code displays the values I’m after, but I’d really like to know how to improve it. var data = { "students":[ { "name":"Eric Smith", "modules":{ "1":{ "id":"COMP40003", "name":"Software Development", "result":80 }, "2":{ "id":"COMP40001", "name":"Digital technologies", "result":71 }, "3":{ "id":"COMP40004", "name":"Web Development", "result":47 }, "4":{ "id":"COMP40002", "name":"Networking Concepts and Cyber Security", "result":0 } } }, { "name":"Bob", "modules":{ "1":{ "id":"COMP40003", "name":"Software Development", "result":80 }, "2":{ "id":"COMP40001", "name":"Digital technologies", "result":71 }, "3":{ "id":"COMP40004", "name":"Web Development", "result":47 }, "4":{ "id":"COMP40002", "name":"Networking Concepts and Cyber Security", "result":0 } } } ] }; var json = JSON.parse(JSON.stringify(data)); for (var key in json["students"]) { console.log(json["students"][key]["name"]); console.log('-------------------'); for (var modkey in json["students"][key]["modules"]) { console.log(json["students"][key]["modules"][modkey]["name"] + " " + json["students"][key]["modules"][modkey]["result"]); console.log('-------------------'); }; }; Thanks
-
Thanks Requinix
-
Thanks for the translation I could do with some help with 5b. I can remove the first instance of of the selected letter within the function selectNextNumber with unset($bestOrder[$x]) and return the updated $bestOrder array and $numberToPick is that a good way to do it or would you recommend somethingelse?
-
Hi guys, I’d appreciate a little guidance but I’m gonna struggle to explain what the problem is first… so apologies in advance. $drawOrder is an array of 1 to 10 letters that are drawn at random Array ( [101] => C [102] => F [103] => D [104] => J [105] => B [106] => H [107] => I [108] => G [109] => E [110] => A ) $bestOrder contains the same values as $drawOrder but the letters are given a priority. Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J ) $bestPossibleOrder – I want this to contain the closest order possible to $bestOrder based on the draw order and 3 letter draw limit Array ( [101] => C [103] => D [105] => B [102] => F [106] => H [108] => G [109] => E [110] => A [107] => I [104] => J ) $availableLetters contains 3 letters that are drawn according to $drawOrder. Array ( [101] => C [102] => F [103] => D ) add “[101] => C” to $bestPossibleOrder array remove “[2] => C” from the $bestOrder array because the letters are not always unique add “[104] => J “ to $availableLetters because it is next in the draw order. Array ( [102] => F [103] => D [104] => J ) add “[103] => D” to my $bestPossibleOrder array remove “[103] => D” from the $bestOrder array because the letters are not always unique add “[102] => B“ to $availableLetters because it is next in the draw order. Array ( [102] => F [104] => J [102] => B ) Repeat the process until all letters are in the $bestPossibleOrder array Array ( [101] => C [103] => D [105] => B [102] => F [106] => H [108] => G [109] => E [110] => A [107] => I [104] => J ) I’ve attached what I’ve written so far but I’m not sure if it’s a good approach <?php $BR = "<br />"; echo "best order:" . $BR; $bestOrder = array("A","B","C","D","E","F","G","H","I","J"); print ("<pre>" . print_r($bestOrder, true) . "</pre>"); echo "draw order:" . $BR; $drawOrder = shuffle_assoc($bestOrder); $drawOrder = array_combine(range(101,(count($bestOrder)+100)),$drawOrder);// $drawOrder array keys have to start from 101 print ("<pre>" . print_r($drawOrder, true) . "</pre>"); echo "available Letters (FIRST DRAW):" . $BR; $availableLetters = array_slice($drawOrder, 0, 3,true); print ("<pre>" . print_r($availableLetters, true) . "</pre>"); $numberToPick = selectNextNumber($bestOrder,$availableLetters); echo "The key with the closest letter to best order is [" . $numberToPick . "]"; function selectNextNumber($bestOrder,$availableLetters){ for ($x = 0; $x <= count($bestOrder); $x++){ $numberToPick = array_search($bestOrder[$x], $availableLetters); if ($numberToPick !== false) { //unset($bestOrder[$x]); //print ("<pre>" . print_r($bestOrder, true) . "</pre>"); return $numberToPick; } } } function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list); shuffle($keys); $random = array(); foreach ($keys as $key) { $random[$key] = $list[$key]; } return $random; }
-
Call variable from another function in class
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
thank you very much for the help guys! Unfortunately, I’m unable to share the actual code ☹ but you’ve helped me nonetheless -
Call variable from another function in class
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
Thanks, but the above code is not the code I am using – I wrote that as a simple example of my problem. Taking away the class would mean changing a lot of code -
Hey guys, How can I access the $currentAccount and $numberOfAccounts variables from the construct function and use them in the updateDB function without making them global variables? <?php class Client { public function __construct($start = []) { ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $sql = mysqli_connect('localhost', 'root', '', 'test'); $query = "SELECT * FROM users;"; $ret = mysqli_fetch_all(mysqli_query($sql, $query) , MYSQLI_ASSOC); $this->print(count($ret) . ' Accounts Loaded!'); $numberOfAccounts = (count($ret)); foreach($ret as $k => $v) { $start = []; $start['name'] = $v['name']; $start['password'] = $v['password']; $currentAccount = 1; $this->print("processing account number {$currentAccount} of {$numberOfAccounts} ..."); $currentAccount++; $this->start = $start; $this->handshake($start); } } public function handshake($start) { // makes a connection to an api with the $start array as parameters and returns $account_info $this->updateDB($account_info); } public function updateDB($account_info) { $this->print("processed account number {$currentAccount} of {$numberOfAccounts} ..."); } public function print ($str) { print ('[' . date('H:i:s', time()) . '] ' . $str . PHP_EOL); } }
-
Hey guys how can i use php to turn a string like this: “A, B#2, C#2, D#3, E” Into an array like this: Array ( [1] => A [2] => B [3] => B [4] => C [5] => C [6] => D [7] => D [8] => D [9]=> E ) I can get the letter and count into an array like this with the help of regex: Array ( [A] => 1 => 2 [C] => 2 [D] => 3 [D]=> 1 ) But I have no idea how I would get the result I desire – any help with this part would be greatly appreciated
-
Hey, I’m considering hiring someone to create an app that will act as an intermediary between tasks sent from a vps to a program on my PC which will return the results to the vps. What should I be searching for when researching the pros and con of something like this? Ps, the tasks are very CPU intensive else I wouldn’t consider using my PC.
-
How do I get specific values from a json encoded string?
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
where's the best hint button? -
How do I get specific values from a json encoded string?
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
nevermind... $com_array = array_keys($main[0]); $com_id = $commander_array[0]; $dom_array = array_keys($main[1]); $dom_id = $dominion_array[0]; $cards = json_encode($main[2]); this does the trick. -
How do I get specific values from a json encoded string?
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
ok, I'm still stuck... I've tried this: foreach($main as $key => $value) { $t[] = array_keys($value); } $com_id = json_encode($t[0]); = [25256] $dom_id = json_encode($t[1]); = [50005] when I do a var_dump it says it's a string: string(7) "[25256]"what am I doing wrong? -
Hey guys, I needed to covert the $main array in to a string so I used json_encode to do it but now I’m unsure how to get just the key values for $main[0] and $main[1] – can anyone help? main array: Array ( [0] => Array ( [25256] => 1 ) [1] => Array ( [50005] => 1 ) [2] => Array ( [301] => 1 [2318] => 1 [13403] => 1 [49489] => 1 ) ) current code and output: $com_id = json_encode($main[0]); = {"25256":1} $dom_id = json_encode($main[1]); = {"50005":1} $car_id = json_encode($main[2]); = {"301":1,"2318":1,"13403":1,"49489":1} Desired output: $com_id = {"25256"} $dom_id = {"50005"} $car_id = {"301":1,"2318":1,"13403":1,"49489":1}
-
Extracting specific data from xml
jonnyenglish89 replied to jonnyenglish89's topic in PHP Coding Help
Thank you for the advice and solution -
Hey guys, I have the following xml from which I’d like to extract the <name> followed by the <level> and <card_id> values from the last <upgrade> element within each <unit> elements E.g. name = Infantry, level = 3, card id = 3 name = Bazooka Marine, level = 3, card id = 351 name = Anvil, level = 6, card id = 421 <?xml version="1.0" encoding="UTF-8"?> <root> <unit> <id>1</id> <name>Infantry</name> <picture>basicinfantry.jpg</picture> <attack>1</attack> <health>2</health> <cost>0</cost> <rarity>1</rarity> <type>1</type> <set>1000</set> <upgrade> <level>2</level> <card_id>2</card_id> <health>3</health> </upgrade> <upgrade> <level>3</level> <picture>basicinfantry_lv3.jpg</picture> <card_id>3</card_id> <health>5</health> </upgrade> </unit> <unit> <id>4</id> <name>Bazooka Marine</name> <picture>bazookamarine.jpg</picture> <attack>2</attack> <health>4</health> <cost>1</cost> <rarity>1</rarity> <skill id="pierce" x="1" /> <type>1</type> <set>1000</set> <upgrade> <level>2</level> <card_id>350</card_id> <health>5</health> </upgrade> <upgrade> <level>3</level> <card_id>351</card_id> <health>6</health> <skill id="pierce" x="2" /> </upgrade> </unit> <unit> <id>416</id> <name>Anvil</name> <picture>abraham.jpg</picture> <attack>3</attack> <health>5</health> <cost>2</cost> <rarity>3</rarity> <skill id="protect" x="1" y="2" /> <skill id="rally" x="1" y="2" /> <type>2</type> <set>4700</set> <upgrade> <level>2</level> <card_id>417</card_id> <skill id="protect" x="1" y="2" /> <skill id="rally" x="2" y="2" /> <health>6</health> </upgrade> <upgrade> <level>3</level> <card_id>418</card_id> <picture>abraham_lv3.jpg</picture> <attack>4</attack> <health>8</health> </upgrade> <upgrade> <level>4</level> <card_id>419</card_id> <skill id="protect" x="2" y="2" /> <skill id="rally" x="2" y="2" /> <attack>4</attack> <health>10</health> </upgrade> <upgrade> <level>5</level> <card_id>420</card_id> <skill id="protect" x="2" y="2" /> <skill id="rally" x="3" y="2" /> <health>11</health> </upgrade> <upgrade> <level>6</level> <card_id>421</card_id> <picture>abraham_lv6.jpg</picture> <skill id="protect" x="3" y="2" /> <skill id="rally" x="3" y="2" /> <health>13</health> </upgrade> </unit> </root> I can to extract all the names levels and card id’s with the code below… how do I restrict the results... can anyone help? $xmlSource = simplexml_load_file("cards_section_1.xml"); //print("<pre>".print_r($xmlSource,true)."</pre>"); foreach ($xmlSource->unit as $units) { printf( "<p>the card ID for %s at level one is %d.</p>", $units->name, $units->id ); if (isset($units->upgrade)) { foreach ($units->upgrade as $unitsUpgrade) { printf( "<p>the card ID for %s at level %d is %s.</p>", $units->name, $unitsUpgrade->level, $unitsUpgrade->card_id ); }}}