Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    displaying php array

    Ok so i have been trying to get this to work for awhile but i cant manage to do it. I am new to PHP and was wondering how i can alert an index of a PHP array. If im not able to alert it i at least want to print it onto the page. What is happening here is i am grabbing a random line from a file and exploding that line into parts. I put the 2nd index in the table cell. When the user clicks on a cell i want an alert to pop up giving me the 3rd index of the array $pieces. This is what i have so far, thanks.

    <html>
    <script>
    function speech(){
    alert(<? $pieces[3] ?>);
    }
    </script>
    <body>
    <table border=1>
    <?
    $f = file("/myPathGoesHere");
    for ($i=0;$i<3;$i++){
    print "<tr></tr>";
    for ($j=0;$j<6;$j++){
    $r = rand(1, 35915);
    $pieces = explode(" ", $f[$r]);
    print "<td onclick='speech()'>$pieces[2]</td>";
    }
    }
    ?>
    </table>
    </body></html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I don't know what you are trying to do, but you cannot mix javascript and PHP together. You need to provide the JS function speech with the data you want as an argument. What you have here will provide nothing for the JS function to work with.

  • #3
    New Coder
    Join Date
    Jul 2011
    Location
    Kediri - Indonesia
    Posts
    61
    Thanks
    2
    Thanked 19 Times in 19 Posts
    this script will alert you $pieces[3] when you click any cell.

    Code:
    <html>
    <body>
    <table border=1>
    <?
    $f = file("/myPathGoesHere");
    for ($i=0;$i<3;$i++){
    print "<tr></tr>";
    for ($j=0;$j<6;$j++){
    $r = rand(1, 35915);
    $pieces = explode(" ", $f[$r]);
    print "<td onclick=\"alert('".$pieces[3]."')\">$pieces[2]</td>";
    }
    }
    ?>
    </table>
    </body></html>
    hope it helps


  •  

    Posting Permissions

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