www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Using PHP to generate tables

  1. #1
    Join Date
    Dec 2011
    Posts
    181

    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
    181
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17410 seconds
  • Memory Usage 2,945KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_html
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates