[Solved] Memory Exceeded in loop hell

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
juliekate
Registered User
Posts: 779
Joined: Sun May 05, 2002 5:32 pm
Location: Philadelphia
Contact:

[Solved] Memory Exceeded in loop hell

Post by juliekate » Wed Mar 30, 2005 4:39 pm

Hi all,

I've been raking through support on this and found a few things that have helped the error but I'm still getting it, although the header now parses.

In a nutshell, my code looked like this:

Code: Select all

header info
IF statement
- SWITCH mode
-- Bunch of modes
---case 'default':
----$bandrow = array();
----while ( $row->blabla)
----{ $bandrow[] = $row}
-----Bunch of Ifs and 2 iterations for search_band and search_band.search_detail
- END switch
parse/footer info
I was getting duplicate songs listed in the search_band_search_detail so I made a few adjustments and the code looked like:

Code: Select all

header info
IF statement
- SWITCH mode
-- Bunch of modes
---case 'default':
----while ( $row->blabla)
----{ 
------Bunch of Ifs and 2 iterations for search_band and search_band.search_detail
-----}
- END switch
parse/footer info
Now I'm getting Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 12 bytes) in /var/www/html/forums/includes/template.php on line 180.

Is it something with now having $row have a SLEW of code to go through before looping?
Last edited by juliekate on Wed Mar 30, 2005 7:30 pm, edited 1 time in total.

User avatar
drathbun
Former Team Member
Posts: 12204
Joined: Thu Jun 06, 2002 3:51 pm
Location: TOPICS_TABLE
Contact:

Post by drathbun » Wed Mar 30, 2005 6:41 pm

You probably have an infinite loop. Put some "echo" statements inside your loop for debug info, see if you can find where it's getting stuck. Be sure to increment or eventually change your loop test or you'll never get out. And you will use up your memory. :-)
I blog about phpBB: phpBBDoctor blog
Still using phpbb2? So am I! Click below for details
Image

juliekate
Registered User
Posts: 779
Joined: Sun May 05, 2002 5:32 pm
Location: Philadelphia
Contact:

Post by juliekate » Wed Mar 30, 2005 7:29 pm

Thank you for the reply!

I actually took a break, came back, and made the code much smaller and sensible. No need for all those nested iterations...I love making things more complicated than they need to be.

For posterity

Code: Select all

				if( $db->sql_numrows($result) < 1 )
				{
					//
					// No songs Found
					//
					$template->assign_block_vars('no_songs', array());
				}
				else
				{
					$playlist_root_path = 'bandster/upload/playlists/';
					$upload_path = "bandster/upload/";
					
					// Generate playlist for band while we're here
					$file = $playlist_root_path.$band_id."-bandpage.m3u";
					$fp = fopen ($file, "w");
					$check_id = '';
					while( $row = $db->sql_fetchrow($result) )
					{
						if ($row['song_id'] != $check_id)
						{
							$template->assign_block_vars('search_band', array());
							$check_id = $row['song_id'];
						}
						
						if(!$row['rating'])
						{
							$row['rating'] = $lang['Not_rated'];
						}
						else
						{
							$row['rating'] = round($row['rating'], 2);
						}

						$template->assign_block_vars('search_band.search_detail', array(
							'TITLE' => '<a href="'. append_sid("music_page.$phpEx?song_id=". $row['song_id']) . '">' . $row['song_title'] . '</a>',					
							'VIEW' => $row['song_view_count'],
							'RATING' => ($music_config['rate'] == 1) ? $row['rating'] : '',
							'COMMENTS' => ($music_config['comment'] == 1) ? $row['comments'] : '')
						);
						
						if ( empty($row['song_url']) )
						{
							$song_url = "http://".$board_config['server_name'] . "/". $board_config['script_path'] . $upload_path . $row['song_filename'];
						}
						else
						{
							$song_url = $row['song_url'];
						}

						$song  = $song_url."\n";
						fwrite ($fp, $song);
					}			
					fclose($fp);
					$filepath .= "http://".$board_config['server_name'] . $board_config['script_path'] . $file;
					$pl_data = $filepath;
					$template->assign_vars(array(
						'L_PLAYLIST' => 'Stream all songs',
						'PL_DATA' => $pl_data)
						);	
				}

Post Reply

Return to “[2.0.x] MOD Writers Discussion”