SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    printing data in a row of a table

    hey folks,
    i m trying to create a loop which is going good and printing in a table. but i want to print it all in just one row.(horizontal) for now this is my code
    Code JavaScript:
    <html>
    	<head>
    		<title>For:Loop</title>
    	</head>
     
    	<body>
    	<script type="text/javascript">
    	var i = 0;
    	for (i=0;i<=10;i++){
    	document.write("<table width=50 border=1><tr><td>" +i+ "</td><tr></table>")
    	}
    	</script>
    	</body>
    </html>
    All those who wander aren't lost.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will this do it for you

    Code HTML4Strict:
    <html>
        <head>
            <title>For:Loop</title>
        </head>
     
        <body>
        <script type="text/javascript">
        var i = 0;
        var newhtml = "<table width=50 border=1><tr>";
        for (i=0;i<=10;i++){
            newhtml = newhtml + "<td>" +i+ "</td>"
        }
        newhtml = newhtml + "</tr></table>"
        document.write(newhtml);
        </script>
        </body>
    </html>

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    its very confusing with that variable name. can u explain me. wht happens or better yet have a variable name more good
    All those who wander aren't lost.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    newhtml is simply a javascript variable which I am using in forming a string of html which is eventually written.

    The javascript is forming html for a table, <table>, with one row, <tr>, and one cell, <td>, for each value of the loop.

    so newhtml will end up with them string

    <table width=50 border=1><tr><td>0</td><td>0</td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td></tr></table>

    Before the loop starts we put in "<table width=50 border=1><tr>" which starts the table and the row.

    Then we do the loop which adds each of the cells.

    Finally after the loop we close the row and table "</tr></table>"

    Hope that makes things clearer.

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    wel for wht i can see is u have re declare variable from <table><tr> to </tr></table>. but when i name it any other variable. say i make newhtml to n or t. it doesn't work.

    Edit: i was able to fix it. i was printing the old newhtml doc. :P sorry my bad.
    thanks though!
    All those who wander aren't lost.


Bookmarks

Posting Permissions

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