www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Using PHP to generate tables

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    163

    resolved [RESOLVED] Using PHP to generate tables

    I am trying to use PHP to generate me some tables based on code below. The only problem is I can't get the <tr> tags to work properly...

    This is the code
    PHP Code:
    <?php
        
    // ESTABLISH VARIABLES
        
        
    $date time();
        
    $year4 2011// four digit year
        
    $year2 11// two digit year
        
    $thisyear date("Y"); // current year
        
    $month 1// first month of the year
        
    $day 1//first day of the month
        
        // GENERATE EACH MONTHLY DIRECTORY
        
        
    $tdcount 1;
        for(
    $year4 $thisyear$year4 >= 2011$year4--){
            
        
            
    // START YEAR DIV CONTAINER
            
    echo "<div id='year".$year4."'>";
            
    // INDEX TABLE HEADER
            
    echo "$year4 Monthly Directory";
            
    // GENERATE TABLE INDEX
            
    echo "<table>";
            echo 
    "<tr>";
            for(
    $month=1;$month<=12;$month++){
                
                
    $monthNum $month;
                if(
    $monthNum 10){ 
                    
    $monthNum "0".$monthNum;
                }
                echo 
    "<td>";
                echo 
    "<h2><a href='audio.php#".$monthNum."-".$year4."'>";
                switch(
    $month){
                    case 
    1: echo "January";
                    break;
                    case 
    2: echo "February";
                    break;
                    case 
    3: echo "March";
                    break;
                    case 
    4: echo "April";
                    break;
                    case 
    5: echo "May";
                    break;
                    case 
    6: echo "June";
                    break;
                    case 
    7: echo "July";
                    break;
                    case 
    8: echo "August";
                    break;
                    case 
    9: echo "September";
                    break;
                    case 
    10: echo "October";
                    break;
                    case 
    11: echo "November";
                    break;
                    case 
    12: echo "December";
                    break;
                }
                echo 
    "</a></h2>";
                echo 
    "</td>";
                
                if(
    $tdcount == || $tdcount || $tdcount == 9) {
                    echo 
    "</tr><tr>";
                }
                if(
    $tdcount == 12) {
                    echo 
    "</tr>";
                }
                
                
    $tdcount++;
                
    $monthNum++;
                
    // END MONTHLY DIRECTORY
                
                //for(){
                        
                //}
                
            
    }
            echo 
    "</table>";
            
    // END YEAR DIV CONTAINER
            
    echo "</div>";
        }
    ?>
    But it generates this...
    HTML Code:
    <html><body><div id="year2013">2013 Monthly Directory<table><tbody><tr><td><h2><a href="audio.php#01-2013">January</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#02-2013">February</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#03-2013">March</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#04-2013">April</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#05-2013">May</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#06-2013">June</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#07-2013">July</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#08-2013">August</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#09-2013">September</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#10-2013">October</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#11-2013">November</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#12-2013">December</a></h2></td></tr><tr></tr></tbody></table></div><div id="year2012">2012 Monthly Directory<table><tbody><tr><td><h2><a href="audio.php#13-2012">January</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#14-2012">February</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#15-2012">March</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#16-2012">April</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#17-2012">May</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#18-2012">June</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#19-2012">July</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#20-2012">August</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#21-2012">September</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#22-2012">October</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#23-2012">November</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#24-2012">December</a></h2></td></tr><tr></tr></tbody></table></div><div id="year2011">2011 Monthly Directory<table><tbody><tr><td><h2><a href="audio.php#13-2011">January</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#14-2011">February</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#15-2011">March</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#16-2011">April</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#17-2011">May</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#18-2011">June</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#19-2011">July</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#20-2011">August</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#21-2011">September</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#22-2011">October</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#23-2011">November</a></h2></td></tr><tr></tr><tr><td><h2><a href="audio.php#24-2011">December</a></h2></td></tr><tr></tr></tbody></table></div></body></html>
    As you can see, it auto-generating more tr elements than I want, and also generating a tbody element that I didn't even specify.

    Am I missing something here, also my $monthNum variable is not getting reset in the for loop...
    Last edited by Dragonfire2008; 01-30-2013 at 12:50 PM.

  2. #2
    Join Date
    Aug 2011
    Posts
    12
    Your logic is all wrong try this

    if (!($x % 3)) {
    echo "</tr><tr>" ;
    }
    if ($x % 3) {
    echo "<td></td>" ;
    }
    $x++;

    if your trying to do what i think u are the above code will create a new row in ur table once the row reachs 3 sets of <td> tags dont forget to set the $x = 1; above ur loop also

  3. #3
    Join Date
    Aug 2011
    Posts
    12
    so the end result will look like this

    content content content
    contentrow2 contentrow2 contentrow2
    contentrow3 contentrow3 contentrow3

  4. #4
    Join Date
    Dec 2011
    Posts
    163
    Well it works, just the second if statement generated an unneeded set of blank <td> tags, so by taking out that second bit of code, it seems to do it perfectly

    PHP Code:
    if (!($tdcount 3)) {
                    echo 
    "</tr><tr>" ;
                }
                
                
    $tdcount++; 
    Thank you very much, now just to finish the rest of the page

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles