...

View Full Version : displaying php array



Zwake
11-21-2011, 09:41 PM
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>

Fou-Lu
11-22-2011, 02:21 AM
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.

XterM
11-22-2011, 02:32 AM
this script will alert you $pieces[3] when you click any cell.



<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum