www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Insert divider after every 6th mysql_fetch_array row

  1. #1
    Join Date
    Sep 2008
    Location
    Connecticut
    Posts
    123

    resolved [RESOLVED] Insert divider after every 6th mysql_fetch_array row

    Hey again everyone. Please keep in mind I'm new to PHP and programming as well, so this might be an obvious answer I'm missing. Any help, as always, is greatly appreciated.

    I have a mysql_fetch_array result that produces 120 or so rows. To help make the resulting table more readable, I would like to insert a <hr>, or some graphic, after every 6th row of the resulting table. Any ideas how to do so? Here's the code I've got for the mysql_fetch_array.

    Code:
    echo '<table class="db_t_res">\n';
    
    if($result) 
    {
      while($row = mysql_fetch_array($result)) 
      {
      echo '<tr>\n
            <td style="width:10%;">'.
       echo $row['year'];
    	echo '</td>\n
    	      <td style="width:30%;">';
    	echo $row['industry'];
    	echo '</td>\n
    	      <td style="width:60%;">';
    	echo $row['emissions'];
    	echo '</td>\n
    	      </tr>\n';
      }
    }
    
    echo '</table>\n';
    Which produces this result:
    Code:
    Year  	Industry  	MMTCO2 Emissions
    1990 	All 	95.36
    1990 	Commercial 	4.34
    1990 	Electric 	23.08
    1990 	Industry 	19.94
    1990 	Residential 	6.50
    1990 	Transportation 	41.50
    1991 	All 	96.42
    1991 	Commercial 	4.09
    1991 	Electric 	25.12
    1991 	Industry 	20.32
    1991 	Residential 	6.39
    1991 	Transportation 	40.50
    1992 	All 	97.68
    1992 	Commercial 	4.52
    1992 	Electric 	26.69
    1992 	Industry 	19.27
    1992 	Residential 	6.97
    1992 	Transportation 	40.24
    1993 	All 	102.25
    1993 	Commercial 	4.92
    1993 	Electric 	31.25
    1993 	Industry 	17.69
    1993 	Residential 	7.17
    1993 	Transportation 	41.22
    1994 	All 	101.21
    1994 	Commercial 	4.98
    1994 	Electric 	29.16
    1994 	Industry 	18.05
    1994 	Residential 	7.10
    1994 	Transportation 	41.91

    I want the results to look something like this:

    Code:
    Year  	Industry  	MMTCO2 Emissions
    1990 	All 	95.36
    1990 	Commercial 	4.34
    1990 	Electric 	23.08
    1990 	Industry 	19.94
    1990 	Residential 	6.50
    1990 	Transportation 	41.50
    ___________________________
    1991 	All 	96.42
    1991 	Commercial 	4.09
    1991 	Electric 	25.12
    1991 	Industry 	20.32
    1991 	Residential 	6.39
    1991 	Transportation 	40.50
    ___________________________
    1992 	All 	97.68
    1992 	Commercial 	4.52
    1992 	Electric 	26.69
    1992 	Industry 	19.27
    1992 	Residential 	6.97
    1992 	Transportation 	40.24
    ___________________________
    1993 	All 	102.25
    1993 	Commercial 	4.92
    1993 	Electric 	31.25
    1993 	Industry 	17.69
    1993 	Residential 	7.17
    1993 	Transportation 	41.22
    ___________________________
    1994 	All 	101.21
    1994 	Commercial 	4.98
    1994 	Electric 	29.16
    1994 	Industry 	18.05
    1994 	Residential 	7.10
    1994 	Transportation 	41.91

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    You can add a counter to determine when to add a <hr />.

    The following should do the trick:


    Code:
     
    if($result) 
    {
     
       $count = 0;
     
      while($row = mysql_fetch_array($result)) 
      {
      echo '<tr>\n
            <td style="width:10%;">'.
       echo $row['year'];
    echo '</td>\n
          <td style="width:30%;">';
    echo $row['industry'];
    echo '</td>\n
          <td style="width:60%;">';
    echo $row['emissions'];
    echo '</td>\n
          </tr>\n';
     
    //add <hr /> if required
      $count = $count + 1;
      if($count == 6) {
      echo '<tr><td colspan="3"><hr /></td></tr>';
      $count = 0;
     }
     
     
      }
    }

  3. #3
    Join Date
    Sep 2008
    Location
    Connecticut
    Posts
    123
    Nice. Thanks Tirna!

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