Sim Posted February 23, 2021 Posted February 23, 2021 Here's a function i use to select drop down or multiselected drop downs. That other devs may find useful. I monthly use it in ACP for crying new entries amd editing entries. //first parameter is data being stored in combo/multiselected //second paramater is the data that was selected Both paramaters are arrays so if data not being parsed is not an array, you can cast as an array as shown in example below. But as seen, the data passed must use the ids keys id and name, but can easily be changed public function getSelectedListBoxData($listArray, $selDataArray = '') { if(empty($selDataArray)) { return $listArray; } $Data = array(); foreach($listArray as $item) { $sel = ""; if(in_array($item['id'], $selDataArray)) { $sel = "selected"; } $Data[] = array( "id" => $item['id'], "name" => $item['name'], "selected" => $sel ); } return $Data; } Use: TPL: <div class="col-md-3"> <div class="form-group"> <label class="pull-left">Reward Item?</label> <select class="form-control" name="itemID"> <option value="0">None</option> {#each items} <option value="{id}" {selected}>{name}</option> {/each} </select> </div> </div> PHP: //on submitting form data $crime['itemID'] = $this->helper->getSelectedListBoxData($this->helper->getItemType(), $this->methodData->itemID); //submitting form data EDIT $crime = $this->getCrime($this->methodData->id); $crime['itemType'] = $this->getSelectedListBoxData($this->helper->getItems() , (array)$crime["itemID"]); Quote Quote
AdamHull Posted February 23, 2021 Posted February 23, 2021 3 hours ago, Sim said: Here's a function i use to select drop down or multiselected drop downs. That other devs may find useful. I monthly use it in ACP for crying new entries amd editing entries. //first parameter is data being stored in combo/multiselected //second paramater is the data that was selected Both paramaters are arrays so if data not being parsed is not an array, you can cast as an array as shown in example below. But as seen, the data passed must use the ids keys id and name, but can easily be changed public function getSelectedListBoxData($listArray, $selDataArray = '') { if(empty($selDataArray)) { return $listArray; } $Data = array(); foreach($listArray as $item) { $sel = ""; if(in_array($item['id'], $selDataArray)) { $sel = "selected"; } $Data[] = array( "id" => $item['id'], "name" => $item['name'], "selected" => $sel ); } return $Data; } public function getSelectedListBoxData($listArray, $selDataArray = '') { if(empty($selDataArray)) { return $listArray; } $Data = array(); foreach($listArray as $item) { $sel = ""; if(in_array($item['id'], $selDataArray)) { $sel = "selected"; } $Data[] = array( "id" => $item['id'], "name" => $item['name'], "selected" => $sel ); } return $Data; } Use: TPL: <div class="col-md-3"> <div class="form-group"> <label class="pull-left">Reward Item?</label> <select class="form-control" name="itemID"> <option value="0">None</option> {#each items} <option value="{id}" {selected}>{name}</option> {/each} </select> </div> </div> PHP: //on submitting form data $crime['itemID'] = $this->helper->getSelectedListBoxData($this->helper->getItemType(), $this->methodData->itemID); //submitting form data EDIT $crime = $this->getCrime($this->methodData->id); $crime['itemType'] = $this->getSelectedListBoxData($this->helper->getItems() , (array)$crime["itemID"]); TPL: <div class="col-md-3"> <div class="form-group"> <label class="pull-left">Reward Item?</label> <select class="form-control" name="itemID"> <option value="0">None</option> {#each items} <option value="{id}" {selected}>{name}</option> {/each} </select> </div> </div> PHP: //on submitting form data $crime['itemID'] = $this->helper->getSelectedListBoxData($this->helper->getItemType(), $this->methodData->itemID); //submitting form data EDIT $crime = $this->getCrime($this->methodData->id); $crime['itemType'] = $this->getSelectedListBoxData($this->helper->getItems() , (array)$crime["itemID"]); Im interested to see how you use this monthly for crying 😂 1 Quote
Sim Posted February 23, 2021 Author Posted February 23, 2021 25 minutes ago, AdamHull said: Im interested to see how you use this monthly for crying 😂 Is that the weirdest typo ever or am i missing something? Edit: i posted this in wrong thread. Should have went in GL thread: @Dayo @Dave @Magictallguy Quote
Dayo Posted February 23, 2021 Posted February 23, 2021 You may want to look at the data-value attribute that i added to the ACP i know having to rely on JS isnt great but im lazy 😛 check this out: https://github.com/ChristopherDay/Gangster-Legends-V2/commit/16523c84a05be9e527f1ef16492fe089dc0ad697#diff-d1ef260dd29f18ce2583194d431fbad8e5f84b3b649c404bb08ba6098f1abbca Topic moved. Quote
Sim Posted February 23, 2021 Author Posted February 23, 2021 Im just as lazy. How i read these commits? Can you explain the colors to me? And i can never seem to download the new source from github so i always rely on the MWG marketplace to download GL. Quote
sniko Posted February 23, 2021 Posted February 23, 2021 18 minutes ago, Sim said: Can you explain the colors to me? Light Red = code removed Dark Red = code replaced Light Green = code added Dark Green = code replaced with 21 minutes ago, Sim said: And i can never seem to download the new source from github If you are not familiar with the command-line and git, I would highly recommend you take a day or so to learn: https://git-scm.com/ https://git-scm.com/docs/git-clone (download code) https://git-scm.com/docs/git-add (add code to current working branch) https://git-scm.com/docs/git-commit (commit code to current working branch) https://git-scm.com/docs/git-push (push committed code on current working branch to a remote repository) (there's a lot more including merging, rebasing, cherrypicking, tagging, branching) There's a lot of different branching models but this is a good starter read: https://nvie.com/posts/a-successful-git-branching-model/ Quote
Magictallguy Posted February 23, 2021 Posted February 23, 2021 To add onto sniko's post; if CLI scares you, I'd recommend a Git GUI, such as SourceTree or GitKraken*; Or, if you're using an editor that support Git out-of-the-box such as PhpStorm**, or an editor that supports plugins (e.g., Atom, SublimeText 3, etc.), just use that! *Note: GitKraken is free for public repos. They put accessing private repos behind a paywall. **Note: PhpStorm has a 30-day free trial, but is otherwise paid software (would strongly recommend it) 1 Quote
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.