469,323 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Convert raw data to well formed rows

hsriat
1,654 Expert 1GB
I have to make a data grid, in which data is shown directly from database.
I have done it by using PHP scripting with a while loop. ie. It would keep on fetching arrays from database and generate corresponding row one by one.
But with few rows its fine. As the number of rows and size of each row will increase, it will end up making it a huge HTML.

So I was thinking of using javascript to add rows. So I made a function to which I passed the row fields as arguments and it generates corrosponding row for the table.

The function is working fine if i call it manually by typing it in the address bar, but it is not adding rows when I call it on body's onload...
Where should I call that function?
Expand|Select|Wrap|Line Numbers
  1. function insRs() {
  2. insR('a1','a2','a3','a4','a5','a6','a7');
  3. insR('b1','b2','b3','b4','b5','b6','b7');
  4. insR('c1','c2','c3','c4','c5','c6','c7');
  5. insR('d1','d2','d3','d4','d5','d6','d7');
  6. }
Feb 20 '08 #1
16 1735
hsriat
1,654 Expert 1GB
I solved the problem.
Feb 20 '08 #2
acoder
16,027 Expert Mod 8TB
Post your solution in case someone has a similar problem.
Feb 20 '08 #3
hsriat
1,654 Expert 1GB
Post your solution in case someone has a similar problem.
Just leave the table blank where you need to display the data.
In PHP code...
[php]<?php
echo "
<body onload=\"insAllRws();\">
<table class=\"grid\" id=\"table\"></table>
<script type=\"text/javascript\">
function insAllRws() {";
while($rw = mysql_fetch_array($sql))
echo "
insR('".$rw[a]."','".$rw[b]."','".$rw[c]."','".$rw[d]."','".$rw[e]."')";
echo "
}
</script>
</body>";
?>[/php]

Make a function to add rows...
Expand|Select|Wrap|Line Numbers
  1. function insR(a,b,c,d,e) {
  2.   var txt = new Array(a,b,c,d,e);
  3.   var tbl = document.getElementById('table');
  4.   var num = tbl.rows.length;
  5.   var rw = table.insertRow(num);
  6.   var flds = new Array;
  7.   for (var i=0; i<5; i++) { //for fields
  8.     flds[i] = rw.insertCell(i);
  9.     flds[i].innerHTML = txt[i];
  10.   }
  11. }
  12.  
Earlier my HTML file was 78 KB along with a JS file of 14 KB
Now, HTML is 12 KB and JS is still 14 KB
Feb 20 '08 #4
acoder
16,027 Expert Mod 8TB
Just leave the table blank where you need to display the data.
...Make a function to add rows...
You could put insAllRs in the head. Anyway, thanks for posting your solution.
Feb 20 '08 #5
hsriat
1,654 Expert 1GB
You could put insAllRs in the head. Anyway, thanks for posting your solution.
Yeh it was working fine in the head.
But I display Loading... in the center of the page while the page is loading.
And its algo is in such a way that if I'll keep insAllRs() in head, it loads the function insAllRs() before displaying Loading....Since all the data is inside the insAllRs() function, it takes time to load. And if I'll keep it inside the body, the Loading.. thing is displayed before loading the insAllRs() function. So I prefered using it inside the body.
I don't think any browser will effect its working.
If it would, then we'll see it later. ;)
Feb 20 '08 #6
hsriat
1,654 Expert 1GB
You could put insAllRs in the head. Anyway, thanks for posting your solution.
Is it harmful to put it inside the body in any case?
Feb 21 '08 #7
acoder
16,027 Expert Mod 8TB
Yeh it was working fine in the head.
But I display Loading... in the center of the page while the page is loading.
And its algo is in such a way that if I'll keep insAllRs() in head, it loads the function insAllRs() before displaying Loading....Since all the data is inside the insAllRs() function, it takes time to load. And if I'll keep it inside the body, the Loading.. thing is displayed before loading the insAllRs() function. So I prefered using it inside the body.
Since insAllRws is called on body onload, it shouldn't affect it. How do you display the loading message?
Feb 21 '08 #8
acoder
16,027 Expert Mod 8TB
Is it harmful to put it inside the body in any case?
I wouldn't say harmful, but it's cleaner to have it in the head, if possible. Even better would be to have no JavaScript in the page and just link to it (keep it unobtrusive).
Feb 21 '08 #9
hsriat
1,654 Expert 1GB
I wouldn't say harmful, but it's cleaner to have it in the head, if possible. Even better would be to have no JavaScript in the page and just link to it (keep it unobtrusive).
Yeah I always keep javascript in different file, but this time its generated by the PHP code and each time the function would contain different arguments. So I kept it in.

Thanks for your response!... :)

And tell me about this .
Feb 21 '08 #10
acoder
16,027 Expert Mod 8TB
Yeah I always keep javascript in different file, but this time its generated by the PHP code and each time the function would contain different arguments. So I kept it in.
If the PHP code is generating JavaScript, link to the PHP file.
Feb 21 '08 #11
hsriat
1,654 Expert 1GB
Since insAllRws is called on body onload, it shouldn't affect it. How do you display the loading message?
The first line of the body says Loading.... And the whole other part is in a div with style="display:none;".
As soon as the page loads it first displays loading.. while the other part is loading.
In the onload event of body, after all other function(which are needed to be called), toggle style.display for loading-div and the remaining-page-div.

If I keep the function in head, the loading.. will be displayed after loading the function, but now it displays before loading the function.
Feb 21 '08 #12
hsriat
1,654 Expert 1GB
If the PHP code is generating JavaScript, link to the PHP file.
I didn't get it.... :(
Feb 21 '08 #13
acoder
16,027 Expert Mod 8TB
The first line of the body says Loading.... And the whole other part is in a div with style="display:none;".
As soon as the page loads it first displays loading.. while the other part is loading.
In the onload event of body, after all other function(which are needed to be called), toggle style.display for loading-div and the remaining-page-div.

If I keep the function in head, the loading.. will be displayed after loading the function, but now it displays before loading the function.
Does the function take a long time to be loaded?
Feb 21 '08 #14
acoder
16,027 Expert Mod 8TB
I didn't get it.... :(
Say, the PHP file is called js.php. It's a PHP file, but it generates JavaScript code, so you could link to it, e.g.
[HTML]<script type="text/javascript" src="js.php"></script>[/HTML]
Feb 21 '08 #15
hsriat
1,654 Expert 1GB
Does the function take a long time to be loaded?
yeah, it does.

Look at the first post. Its actually fetching all the rows from database.
Rows may be more than 100, and each may have good data. So it may take time.


Thanks for js.php... I got it.
Feb 21 '08 #16
acoder
16,027 Expert Mod 8TB
yeah, it does.

Look at the first post. Its actually fetching all the rows from database.
Rows may be more than 100, and each may have good data. So it may take time.
Well, in that case, I guess it'll have to stay in the body.
Feb 21 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by googlegroups | last post: by
19 posts views Thread by jcrouse | last post: by
5 posts views Thread by manmit.walia | last post: by
5 posts views Thread by bbb | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.