SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    table from 1 to 100 php nested in html

    Hello,

    i have a simple problem but it is giving me a hard time to find out the mistake am doing

    i am trying to a create a table in php code implemented in an html table, using a for loop with if statement nested in it.

    my code is :

    HTML Code:
    <html>
    	<head>
    		<title>Ahmed</title>
    	</head>
    	<body>
    		<table border="1">
    			<?php
    				for ($x=1; $x <= 100; $x++){
     				   if($x%2 ==0.5) {
    					   echo "<tr><td>" . "<br>" . "</tr></td>";
     				  			}
    								echo "<tc><td>" . $x . "</td></tc>" ;
    									}
    			?>
    		</table>
    	</body>
    </html>
    the table should break to a new row at 10, and 20 and 30 and so on, each number in a cell, my code got several flaws, first am getting a free empty cell after each row, and the breaking is happening in the wrong place ( which i assume is a mistake in the "if" statement ) i've tried the "if ($x%2 ==0.5) which i am sure should by right mathematically, but it gave me a a one row table without a break ?

    could someone point out the problem and help me out here please ?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Try echoing $x%2 to see what values it returns

    And if you want to break to a new row every 10 elements, shouldn't you do something like

    if ($x%10 == 0)

    ?

  3. #3
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will i didnt try echoing $x%2 , but doing in a calculator it would always give me a number with a .5 except 2, 22, 24 .... and the 10,20,30 and so so i thought it could work, but not really
    plus originally what you suggested was my first choice which is ($x%10 ==0) but i kept getting this table in the picture i attached,
    Screen Shot 2013-09-27 at 2.38.13 PM.png

    and it didnt break after 10 elements in the first row, it didn that in the following rows which is fine but not organized since it missed one cell in the first row and that missed up the whole serial of the table which should be something like this :

    | 1 | 2 | ...... |10|
    |11|12| .......|20|
    .
    .
    .
    |91|92|.......|100|

    thank you for reply, i appreciate your help, but i hope my explanation gave you a better insight on what am trying to do ? whats the mistakes am doing though ?

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi madnomad, welcome to the forums!

    The code within your IF statement is the issue - you're adding a row around a single table cell, when what you really want to be doing is wrapping every 10 cells in a row. Also, I'm not sure what the <tc> tags are for, as these aren't valid tags as far as I know.

    You can get the result you want by changing this section of your code:
    PHP Code:
    <table border="1">
    <?php
        
    foreach (range(1100) as $x)
        {
            if (
    $x 10 == 1) {
                echo 
    "<tr>";
            }
            
            echo 
    "<td>$x</td>";
            
            if (
    $x 10 == 0) {
                echo 
    "</tr>";
            }
        }
    ?>
    </table>
    Every iteration it checks if dividing $x by 10 leaves a remainder of 1 (making it the first number in a new row) and if so, adds a <tr> tag before outputting the cell.

  5. #5
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, i got it right, with the help of your code, but using it as it is, it helped me to understand my mistake, my code is :
    HTML Code:
    <html>
    	<head>
    		<title>Ahmed</title>
    	</head>
    	<body>
    		<table border="1">
    			<?php
    				for ($x=1; $x <= 100; $x++){
     				   if($x%10 ==1) {
    					   echo "<tr>";
     				  			}
    								echo "<td>" . $x . "</td>" ;
    									}
    			?>
    		</table>
    	</body>
    </html>
    this code will give you the same result as the code you posted, the difference is that i am using a "for" loop and an "if" statement, and the mistake was first the "<tc>" which is for creating a new column , but apparently i didnt need it, and since i was using that my first try with the if($x%10 ==1) didnt work i had to change up a bit and add the "<tc>" and got everything complected.

    now i have a clear understanding of my code when i trace it, thanks to you explanation.

    Thank you again.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by madnomad10011 View Post
    this code will give you the same result as the code you posted
    No it doesn't. Your code doesn't have any </tr> tag

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by guido2004 View Post
    No it doesn't. Your code doesn't have any </tr> tag
    Some (all?) browsers will automatically add the closing row tag, but personally I'd rather explicitly add the tag myself.

  8. #8
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, my code is missing the </tr> closing tag, but apparently as "@fretburner" mentioned some browser will still accept the code as functional, and since it worked for me i guess that it true, but i appreciate the note.

    Am using a Firefox btw and i tried it again without the closing tag and it is working, but fixed the code with the right construction, the missing closing tag, thanx again for the reminder.

    i need to work on my tracing skills and not just depends on the browser view.


Tags for this Thread

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
  •