1
echo "Associative 2-dimensional array:<br>";

$marks1=array(
    array("Maths"=>80,"Physics"=>89,"Chemistry"=>79),
    array("Maths"=>90,"Physics"=>78,"Chemistry"=>87),
    array("Maths"=>78,"Physics"=>90,"Chemistry"=>79)
);
echo "<ul>";
for($r=0;$r<count($marks1);$r++)
{
    echo "<li>";
    foreach($marks1[$r] as $key=>$value)
    {
        echo $key." = ".$value."  ";
    }
    echo "</li>";
    echo "<br><br>";
}
echo "</ul>";

How can i get the click via click button means if i click button gets Maths = 80 Physics = 89 Chemistry = 79 then again i click button gets Maths = 90 Physics = 78 Chemistry = 87 and so on pls suggest

3
  • You need JavaScript for that. PHP is just meant to send data to your web browser. Commented Feb 22, 2017 at 18:16
  • Click events are carried out on the client side, for which you will need JavaScript. Ask if you have difficulties in binding click events to fetch data from JS objects. Commented Feb 22, 2017 at 18:19
  • any example using above code Commented Feb 22, 2017 at 18:29

1 Answer 1

1

You can append the content to a div and increment a counter used like id post_fix follow this simple exemple i created for you.

<?php 
echo "Associative 2-dimensional array:<br>";

$marks1=array(
    array("Maths"=>80,"Physics"=>89,"Chemistry"=>79),
    array("Maths"=>90,"Physics"=>78,"Chemistry"=>87),
    array("Maths"=>78,"Physics"=>90,"Chemistry"=>79)
);
echo "<ul>";
for($r=0;$r<count($marks1);$r++)
{
    echo "<li>";
    foreach($marks1[$r] as $key=>$value)
    {
        echo "<span id='$key$r'>".$key." = ".$value."</span>  ";
    }
    echo "</li>";
    echo "<br><br>";
}
echo "</ul>";

?>
<button onclick="show()">Show</button>
<div id="here"></div>
<script type="text/javascript">
    var i=0;
        function show()
        {

            document.getElementById("here").innerHTML = document.getElementById("Maths"+i).innerHTML;
            i++;
        }
</script>

Hope that will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

thats cool but what i want to call Maths, Physics and Chemistry in a row i.e Maths = 80 Physics = 89 Chemistry = 79 on first click and next click Maths = 90 Physics = 78 Chemistry = 87

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.