FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » General » PHP discussions » losing data in PHP array... please help!
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
losing data in PHP array... please help! [message #34598] Wed, 08 November 2006 20:56 Go to next message
jfern is currently offline  jfern   United States
Messages: 2
Registered: November 2006
Location: New York
Karma: 0
Junior Member
I have a .php file that has php, html, and javascript in it. at the very top of the file, I use PHP to pull data from my a mySQL database, and put it into a PHP array.

after some HTML code, I have some javascript to put the PHP array into a javascript array.

What is weird, is that at top, my PHP array is completely filled(about 25,000) entries. However, once I use it again(assigning it to a javascript array), and it only has 14 elements!(everything else gets lost, so when I try to assign more than 14 elements in my array, it doesn't work)

below is my code:
[PHP SECTION]
<?
$rows = array();
$cou = 0;
$test = mysql_query("SELECT title from Pamphlet");
while($row = mysql_fetch_array($test))
{
$rows[$cou] = $row[0];
$cou++;
if($cou < 30)
echo $row[0].'<br />';
}
//echo $rows[2];
?>
[THEN SOME HTML]

[JS SECTION]
<script language="Javascript">
var a = new Array();
var i = 0;

<?php
$k = 14;
for($j = 0; $j < $k; $j++)
{
?>
i = <?=$j?>;
a[i] = ('<?php echo $rows[$j] ?>');
<?php
}
?>

function bindEvents()
{
//Find all of the INPUT tags
var tags = document.getElementsByTagName('INPUT');
for (i in tags)
{
var tag = tags[i];
//If it's a text tag, attach an AutoSuggest object.
if(tag.type && tag.type.toLowerCase() == "text")
{
new AutoSuggest(tag,a);
}
}
}


window.onload = bindEvents;
</script>

This only works if my $k is 14 or less. If it is more than 14, or if I use a foreach, it doesnt work. Am I losing data from the top of my .php file to the bottom of it? I know that javascript runs on the user side and php on the browser side, is this what is causing my errors!?

Thanks for any help!
-Josh
Re: losing data in PHP array... please help! [message #34814 is a reply to message #34598] Sun, 19 November 2006 14:43 Go to previous message
1harsh789 is currently offline  1harsh789   India
Messages: 4
Registered: September 2006
Karma: 0
Junior Member
In your [JS SECTION] your are assigning $k=14 so the for loop only executes 14 times and showing only 14 records. See your code below..

<script language="Javascript">
var a = new Array();
var i = 0;

<?php 
$k = 14; 
for($j = 0; $j < $k; $j++)



  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Message Tree
Next Topic: Shopping cart malfunction?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun May 12 04:30:17 GMT 2024

Total time taken to generate the page: 0.02358 seconds