Arrays in JavaScript [SOLVED]

Well this thread is pretty far off its title… but before an admin throws me off just one question… inflation since when?

You’re taking it too literal. He is joking around. The “0.02 cents” thing is a common phrase, but he tried being funny by making it a bigger number. Then made a joke that it’s a different number due to inflation.

tl;dr, just stop overthinking it.

1 Like

Ha, of course, what the hell is wrong with me? :blush:
I’ll just get back to reading your links. :slight_smile:

Hi,
After learning a little JS arrays and object, I got this to work!
Here’s the JS script,

  <script>
       var newTopics = <?php echo json_encode($topics, JSON_PRETTY_PRINT) ?>;//converts php arrays to JS object
     var newSubTopics = <?php echo json_encode($subtopics, JSON_PRETTY_PRINT) ?>;
         function populate(s1, s2){
        var s1 = document.getElementById(s1);
        var s2 = document.getElementById(s2);
        var optionArray = [];
        s2.innerHTML = "";
        for (i = 0;  i < newTopics.length; i++) {
            for (j = 0;  j < newSubTopics.length; j++) {
                if(newTopics[i].id == newSubTopics[j].topicid){
                    if(s1.value == newTopics[i].topic){            
                    optionArray.push(newSubTopics[j].subtopic);
                        }
                    }
                }
        } 
        for(var option in optionArray){
                var newOption = document.createElement("option");
                newOption.value = optionArray[option];
                newOption.innerHTML = optionArray[option];
                s2.options.add(newOption);
            }
        }
        </script>

This takes two php arrays $topics and $subtopics and converts them to JS objects.
I really didn’t think I’d be able to figure this out, but now I am delighted.
I bought and started working through ‘Eloquent Javascript’ But this book is not for beginners. It is a bit too confusing.
However the Sitepoint book, ‘Javascript Novice to Ninja’ was great. Also the code here gave me something to work with.
Thanks for all your help,
Shane

1 Like

And the HTML form is,

  <td>
            <select name="topic" id="topic" onchange="populate('topic', 'subtopic')">
             <option value=""></option>
                 <?php foreach($topics as $topical):
                       $value = $topical['topic'];
                       $selected = ($topic == $value) ? 'selected' : '';
                       echo "<option value='$value' $selected>$value</option>";
                  endforeach; ?>
            </select>
  </td>
        <td>
            <select name="subtopic" id="subtopic">
             <option value=""></option>
                 <!-- <?php foreach($subtopics as $subtopical):
                       $value = $subtopical['subtopic'];
                       $selected = ($subtopic == $value) ? 'selected' : '';
                       echo "<option value='$value' $selected>$value</option>";
                  endforeach; ?> -->
            </select>
            
  </td>

Thanks,
Shane

1 Like

I take that back. ‘Eloquent Javascript’ is a great book!
Thanks

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.