Who knows PHP?
Results 1 to 10 of 10

Thread: Who knows PHP?

  1. #1
    Frazzled
    Dracolich (Lvl 29)

    Morrus's Avatar

    Join Date
    Jan 2002
    Location
    Intrawebs
    Posts
    40,718

    Who knows PHP?

    I've been teaching myself PHP this last week so that I can write my own web apps (character builders and the like). It's coming along well, but I've run into a block. I'm hoping that one of the many clever people on EN World can help me!

    So....

    I have an array:

    skill[1]
    skill[2]
    skill[3]
    ... all the way to skill[13]

    In each slot, there is the name of a skill, such as running, or stealth, or computers, or herbalism, etc.

    The problem is that some slots will be duplicated. For example, the user may have selected herbalism three items during the character creation process, so three of those 13 slots are "herbalism".

    What I need is a list of skills, and the number of times they've been taken. So rather than:

    1. running
    2. herbalism
    3. jumping
    4. computers
    5. running
    6. running
    7. chess
    8. woodworking
    9. dodging
    10. dodging
    11. computers
    12. swords
    13. bows

    I need:

    1. running 3
    2. dodging 2
    3. computers 2
    4. herbalism 1
    5. chess 1
    6. woodworking 1
    7. swords 1
    8. bows 1
    9. jumping 1

    So I need some kind of function to take that first list and turn it into the second list.

    I know there's an array_count_values function in PHP, and I can get it to split out those values on the screen, but I don't know how to "work" with those values to put them into some kind of new array I can use.

    Anybody got any ideas? Does that even make sense?

  2. #2
    Frazzled
    Dracolich (Lvl 29)

    Morrus's Avatar

    Join Date
    Jan 2002
    Location
    Intrawebs
    Posts
    40,718
    I suppose another way to ask the same question

    ‪Is there an easy way to take the output of array_count_values and put that into another, new array which stores the values and their number of occurrences?‬

  3. #3
    Member
    Superhero (Lvl 15)

    Mustrum_Ridcully's Avatar

    Join Date
    Jan 2002
    Location
    Oldenburg
    Posts
    15,384
    I am not familiar with PHP, unfortunately.
    I am however familiar with programming in general. I can tell you that this is probably not how you want to implement the skills in the long run, since it will be error-prone and hard to work with in the long run.

    Use Key Value pairs. I am not familiar with PHP, as mentioned, but a brief look here: http://php.net/manual/en/language.types.array.php
    suggests that arrays are innately capable of working as such.

    As key, you could use the skill name, and the value is the skill rating.

    When the player selects a skill during character creation, you can check (with array_key_exists) if it already has that skill. If not, you just add the new skill with rating 1 (or whatever the base rating is for the first time picking a skill.)
    When the map already contains the kjey, you can instead set the value to the new skill to the one before +1.

    Pseudo Code (Again, I don't actually program PHP, my current work is mostly C++ and a tiny bit of Python) Except that I forget semicolons and use double quotes where single quotes would be typical or whatever.)

    if (array_key_exists($selectedSkill, $skills) {
    skills($selectedSkill)=skills($selectedSkill)+1
    } else {
    skills($selectedSkill)=1
    }

    If you just want to know which skill the players has at all, you can use array_keys.
    If you wanted to know what the players Herbalism skill is, you could write skills("Herbalism");


    (It could be that the above code could actually omit the whole checking if the key even exists and be condensed to
    skills($selectedSkill)=skills($selectedSkill)+1
    It could be that PHP by defaults treats keys in an array that have never been set as a 0. In C++, this almost certainly wouldn't work, because an uninitialized variable will likely hold some basically random value. But interpreted language like PHP, JavaScript, Perl and Pyhton are usually simpler)


    In the longer run, you are probably better off creating some objects, instead of using just raw arrays and strings and numbers. But to go into that, you will need to dig a bit deeper into fundamental programming topics like algorithms and data structures (and deeper into PHP as well.)

  4. #4
    Member
    Novice (Lvl 1)



    Join Date
    Jun 2014
    Location
    NashVegas
    Posts
    13
    Hi Morrus. Can I recommend you take your question to https://www.phphelp.com? You'll find some friendly help there.
    XP trappedslider gave XP for this post

  5. #5
    Frazzled
    Dracolich (Lvl 29)

    Morrus's Avatar

    Join Date
    Jan 2002
    Location
    Intrawebs
    Posts
    40,718
    I figured it out eventually! Took a few hours, but I got there.

  6. #6
    Member
    Novice (Lvl 1)



    Join Date
    Jun 2014
    Location
    NashVegas
    Posts
    13
    @Morrus Will you post a link to your completed tool?

    I do PHP as well, this is a tool of mine. I'm sure you'll recognize this Fantasy Coin Calculator from an old Dragon Magazine article.

    Which brings up a question of my own, have there been any newer articles like Coins in a Coffer? I wouldn't mind adding a new feature or two.

  7. #7
    Member
    The Great Druid (Lvl 17)



    Join Date
    Nov 2002
    Posts
    6,468
    yeah, what Mustrum is talking about is Associative Arrays in PHP. Instead of an integer index, you can use a string:
    $arrSkills["Herbalism"]++;

    that little snippet would mean every time somebody picks herbalism, it increments by one. Later, you can output all the skills and their scores:
    Code:
    for($i=0;$i<len($arrSkills);$i++) 
    {
      echo($arrSkills($i);
      echo("BRTAGGHERE"); //won't let me post html tag on the forum :)
    }
    Last edited by Janx; Monday, 23rd July, 2018 at 02:11 PM. Reason: fixing format

  8. #8
    Member
    Superhero (Lvl 15)

    Mustrum_Ridcully's Avatar

    Join Date
    Jan 2002
    Location
    Oldenburg
    Posts
    15,384
    Quote Originally Posted by Morrus View Post
    I figured it out eventually! Took a few hours, but I got there.
    It's not so important on EN World, since it's likely not frequented by desperate PHP beginners, but anywhere else you post programming questions like this: If you find the answer yourself, be so kind to present the answer, or at least some valuable hint.

    It's a common programmer fate: Searching the internet for a question, you find someone having exactly the same question, hope that someone replied... And the only reply you find is: "Figured it out myself. It works now!".
    XP trappedslider gave XP for this post

  9. #9
    Member
    Novice (Lvl 1)



    Join Date
    Jun 2014
    Location
    NashVegas
    Posts
    13
    Quote Originally Posted by Mustrum_Ridcully View Post
    If you find the answer yourself, be so kind to present the answer, or at least some valuable hint.

    It's a common programmer fate: Searching the internet for a question, you find someone having exactly the same question, hope that someone replied... And the only reply you find is: "Figured it out myself. It works now!".
    I'd like to second this! It's the most frustrating thing in the world to have finally found someone with your exact problem - but they didn't post a solution.

    Too bad there aren't more programming related places to hang out for developers specific to tabletop gaming. I only know of one Discord channel and a tiny group on Facebook.

  10. #10
    Frazzled
    Dracolich (Lvl 29)

    Morrus's Avatar

    Join Date
    Jan 2002
    Location
    Intrawebs
    Posts
    40,718
    The answer was simple: the count function already does exactly that, and gives me the array I needed. I didnt realise it created the array rather than just spat out some numbers to read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •