Jump to content
MakeWebGames

Select dropdown/multiselected drop down item(s) function


Recommended Posts

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

 

 

Link to post
Share on other sites
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 😂

  • Confused 1
Link to post
Share on other sites

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.

Link to post
Share on other sites
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

954357563_Screenshot2021-02-23at21_42_53.thumb.png.960d8434135dae1196c73c104d190ff0.png

 

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:

Link to post
Share on other sites

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)

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...