Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Insert divider after every 6th mysql_fetch_array row

Featured Replies

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.

 

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:

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
1995 	All 	103.32
1995 	Commercial 	5.20
1995 	Electric 	30.36
1995 	Industry 	18.02
1995 	Residential 	7.20
1995 	Transportation 	42.54
1996 	All 	107.93
1996 	Commercial 	5.94
1996 	Electric 	33.06
1996 	Industry 	17.96
1996 	Residential 	8.13
1996 	Transportation 	42.84
1997 	All 	110.25
1997 	Commercial 	5.47
1997 	Electric 	34.35
1997 	Industry 	18.41
1997 	Residential 	7.79
1997 	Transportation 	44.23
1998 	All 	111.67
1998 	Commercial 	5.31
1998 	Electric 	36.22
1998 	Industry 	18.14
1998 	Residential 	7.12
1998 	Transportation 	44.89
1999 	All 	113.80
1999 	Commercial 	5.27
1999 	Electric 	37.80
1999 	Industry 	17.67
1999 	Residential 	7.25
1999 	Transportation 	45.81
2000 	All 	122.55
2000 	Commercial 	5.73
2000 	Electric 	42.30
2000 	Industry 	17.69
2000 	Residential 	8.28
2000 	Transportation 	48.54
2001 	All 	120.53
2001 	Commercial 	5.23
2001 	Electric 	41.84
2001 	Industry 	17.46
2001 	Residential 	7.52
2001 	Transportation 	48.49
2002 	All 	119.03
2002 	Commercial 	4.89
2002 	Electric 	40.89
2002 	Industry 	17.28
2002 	Residential 	7.33
2002 	Transportation 	48.63
2003 	All 	122.88
2003 	Commercial 	5.55
2003 	Electric 	40.56
2003 	Industry 	18.35
2003 	Residential 	8.31
2003 	Transportation 	50.11
2004 	All 	127.37
2004 	Commercial 	5.46
2004 	Electric 	40.27
2004 	Industry 	18.83
2004 	Residential 	8.41
2004 	Transportation 	54.41
2005 	All 	129.14
2005 	Commercial 	5.49
2005 	Electric 	41.03
2005 	Industry 	18.72
2005 	Residential 	8.49
2005 	Transportation 	55.41
2006 	All 	122.56
2006 	Commercial 	4.88
2006 	Electric 	36.48
2006 	Industry 	17.64
2006 	Residential 	7.05
2006 	Transportation 	56.50
2007 	All 	127.95
2007 	Commercial 	4.98
2007 	Electric 	41.03
2007 	Industry 	17.95
2007 	Residential 	7.44
2007 	Transportation 	56.56

You could try this - although I suspect there might be a better way...

 

echo '<table class="db_t_res">\n';

if($result) 
{
 $count=1;
 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'];
             if ($count==6){
                           echo '</td>\n
                                 </tr>\n
                                 <hr/>\n';
                           $count=1;}
             else {
             echo '</td>\n
                   </tr>\n';
             $count++;
             }
 }
}

echo '</table>\n';

^ thats the approach I would take as well, although when doing this kind of thing I don't call the variable $count as I sometimes have an array count to work with and then a separate variable that counts up as well. So I reserve $count for the actual referenced count of an array & use something like $n as the variable.

You could also remove that large if structure and just put

if($breaker++ %6) echo '<hr />';

which is a lot cleaner IMO

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.