0

I am writing a php function which appends content to a $results variable and will return all the results appended to it.

I have a variable $myArray which holds many separate arrays and looks like the below

Array(
[0] => ABC
[1] => DEF
[2] => GHI
[3] => JKL
 )
Array(
[0] => MNO
[1] => 123A
[2] => 123B
[3] => 123C
 )
Array(
[0] => orange
[1] => yellow
[2] => green
[3] => blue
 )

In my code i loop though content to generate buttons and data tot he screen. i would ideally like if the first button is pressed the contents of the first array (ABC,DEF,GHI,JKL), the second button (MNO, 123A,123B,123C) etc etc.

Here is my loop to generate content

for($index=0; $index < count($exampleArray); index++){
  $myArray = array_values(array_filter($exampleArray[$index]["Column2"]));
  $returnVariable .= '<td>
    <button type="button" class="open-my-modal btn btn-primary" 
        data-numbers="'.htmlspecialchars(json_encode($myArray), ENT_QUOTES, 'UTF-8').'" </td>';

The above code generates different buttons depending on the data that is being passed through, and to be sent to a modal. The part of the modal code where i create an id is below

$returnVariable .= '<tr>
                        <td><span id ="value1"></span></td>
                        <td><span id ="value2"></span></td>
                        <td><span id ="value3"></span></td>
                        <td><span id ="value4"></span></td>
                    </tr>

Then i have a script where when the modal is opened i link the data, to the html id.

$returnVariable .= '<script>

$(document).ready(function () {             
$(".open-my-modal").click(function(){
  $("#value1").html($(this).data("numbers")[0]);

The problem is no matter what button the same information is being displayed. And the only data that is being passed though on any button is ABC,DEF,GHI,JKL.

I i call

$("#value1").html($(this).data("numbers")[0]); Only ABC will show.

if i call

$("#value1").html($(this).data("numbers")); It shows ABC up to JKL

The data is supposed to change dynamically depending on which button is clicked. MNO etc, or orange, yellow never shows up. it only is taking the first array, for every button.

The content will be looped though dynamically for example

<td><span id ="value1"></span></td> = ABC
                    <td><span id ="value2"></span></td>= DEF
                    <td><span id ="value3"></span></td>= GHI
                    <td><span id ="value4"></span></td>= HIJ

Next Button clicked values should =

<td><span id ="value1"></span></td> = MNO
                    <td><span id ="value2"></span></td>= 123A
                    <td><span id ="value3"></span></td>= 123B
                    <td><span id ="value4"></span></td>= 123C

etc, etc

4
  • Can you show the relevant parts of the generated HTML (not the PHP, the actual HTML and <script> that are seen in the browser)? Commented Jul 26, 2017 at 14:37
  • The script and html code for the modal is displayed above. What other code do you need to see? Commented Jul 26, 2017 at 14:43
  • Again, the actual HTML for the buttons, data-... attributes and all. Not the PHP. View Source in the browser, copy those parts, add to the question. Commented Jul 26, 2017 at 14:51
  • The first part of your code, $myArray should be a multi-dimensional array where the key could correspond to the button. For example, how does it know to display MNO, 123A... on pressing the second button? It needs something (a key) in $myArray to identify that... Commented Jul 26, 2017 at 14:53

1 Answer 1

1

The reason this is happening is because $myArray is being overwritten. You posted:

Array(
[0] => ABC
[1] => DEF
[2] => GHI
[3] => JKL
 )
Array(
[0] => MNO
[1] => 123A
[2] => 123B
[3] => 123C
 )
Array(
[0] => orange
[1] => yellow
[2] => green
[3] => blue
 )

Well, the array keys [0],[1],[2],[3] are the same in each instance and there is nothing to "contain" each array.

You really need a multidimensional array, more like this:

[0] => [
    [0] => ABC
    [1] => DEF
    [2] => GHI
    [3] => JKL
],
[1] => [
    [0] => MNO
    [1] => 123A
    [2] => 123B
    [3] => 123C
],
// ...

If you use var_dump($myArray[0]); you will get the "first" set (arrays are 0-indexed):

[0] => ABC
[1] => DEF
[2] => GHI
[3] => JKL

And var_dump($myArray[1]); gives you the second set:

[0] => MNO
[1] => 123A
[2] => 123B
[3] => 123C

etc...

The key in the multi dimensional array can correspond to the button pressed, e.g.

  • Button 1: $myArray[0]
  • Button 2: $myArray[1]
  • Button 3: $myArray[2]
  • Button 4: $myArray[3]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.