Convert raw data to well formed rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    Convert raw data to well formed rows

    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?
    [CODE=javascript]
    function insRs() {
    insR('a1','a2', 'a3','a4','a5', 'a6','a7');
    insR('b1','b2', 'b3','b4','b5', 'b6','b7');
    insR('c1','c2', 'c3','c4','c5', 'c6','c7');
    insR('d1','d2', 'd3','d4','d5', 'd6','d7');
    }[/CODE]
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    I solved the problem.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Post your solution in case someone has a similar problem.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by acoder
        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=\"insAll Rws();\">
        <table class=\"grid\" id=\"table\"></table>
        <script type=\"text/javascript\">
        function insAllRws() {";
        while($rw = mysql_fetch_arr ay($sql))
        echo "
        insR('".$rw[a]."','".$rw[b]."','".$rw[c]."','".$rw[d]."','".$rw[e]."')";
        echo "
        }
        </script>
        </body>";
        ?>[/php]

        Make a function to add rows...
        [CODE=javascript]function insR(a,b,c,d,e) {
        var txt = new Array(a,b,c,d,e );
        var tbl = document.getEle mentById('table ');
        var num = tbl.rows.length ;
        var rw = table.insertRow (num);
        var flds = new Array;
        for (var i=0; i<5; i++) { //for fields
        flds[i] = rw.insertCell(i );
        flds[i].innerHTML = txt[i];
        }
        }
        [/CODE]

        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

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by hsriat
          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.

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by acoder
            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. ;)

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by acoder
              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?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by hsriat
                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?

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by hsriat
                  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).

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by acoder
                    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 .

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by hsriat
                      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.

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Originally posted by acoder
                        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.

                        Comment

                        • hsriat
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1653

                          #13
                          Originally posted by acoder
                          If the PHP code is generating JavaScript, link to the PHP file.
                          I didn't get it.... :(

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by hsriat
                            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?

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by hsriat
                              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]

                              Comment

                              Working...