SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple script running out of memmory, do you see a way to optimize it?

    This thing is basically just a helper function to page results, but even setting the php.ini to have 32mb of memmory, it's running out.

    PHP Code:
        public function pageResults($base_url$page$total_results$per_page)
        {
            
    $page_results "";
            
    $total_pages $total_results $per_page;
            if((
    $total_pages $per_page) < $total_results)
                
    $total_pages++;
                
            if(
    $page 11)
            {
                
    $i 1;
                while(
    $i 11 && $i <= $total_pages)
                {
                    
    $page_results .= " <a href=\"".$base_url."/page/".$i."\"";
                    if(
    $i == $page)
                        
    $page_results .= " style=\"text-decoration:none;\" ";
                        
                    
    $page_results .= ">".$i."</a>";                
                }
                if(
    $total_pages 10)
                {
                    
    $page_results .= " <a href=\"".$base_url."/page/".($page 1)."\">></a>";
                    
    $page_results .= " <a href=\"".$base_url."/page/11\">>></a>";
                }
            }
            else 
            {
                if(
    strlen($page) == 2)
                    
    $start_page substr($page,0,1)."0";
                else if(
    strlen($page) == 3)
                    
    $start_page substr($page,0,2)."0";
                else if(
    strlen($page) == 4)
                    
    $start_page substr($page,0,3)."0";
                    
                
    $i $start_page;
                
    $end_page $start_page 10;
                while(
    $i $end_page && $i <= $total_pages)
                {
                    
    $page_results .= " <a href=\"".$base_url."/page/".$i."\"";
                    if(
    $i == $page)
                        
    $page_results .= " style=\"text-decoration:none;\" ";
                        
                    
    $page_results .= ">".$i."</a>";                
                }
                if(
    $total_pages $end_page)
                {
                    
    $page_results .= " <a href=\"".$base_url."/page/".($page 1)."\">></a>";
                    
    $page_results .= " <a href=\"".$base_url."/page/".$end_page."\">>></a>";
                }
            }
            return 
    $page_results;
        } 

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how big do $end_page and $total_pages get?

  3. #3
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As of right now only about 200 or so.

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is the error message? What line in your script does it relate to. Certainly not in the code posted.

  5. #5
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the specific error: Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 12845034 bytes) in /path/PageResults.php on line 18


    This is line 18: $page_results .= " style=\"text-decoration:none;\" ";


    Also, it has to be with this, because prior to adding this custom paging, it display fine, and even after doubling the memory allowed to 32mb it still gives the error.

  6. #6
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you even increasing the value of $i anywhere within the while statements? If not, it will be running an infinite loop...

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tmapm View Post
    Are you even increasing the value of $i anywhere within the while statements? If not, it will be running an infinite loop...
    didnt notice that, theres the problem

  8. #8
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops. Originally it was a for loop until I thought a while loop would work better. That's definitely it.

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I'll give you something to play with. This doesn't do the same thing your method does cause really I don't know what your method is doing completely.

    PHP Code:
    public function pageResults ($total$per$current 1) {

        
    $return '';
        
    $pages range(1$total $per);

        foreach (
    $pages as $page) {

            
    $format  ' <a href="%s/page/%s"%s>%2$s</a>';
            
    $curlink = ($page == $current) ? ' style="text-decoration: none;"' '';
            
    $return .= sprintf($format$this->baseurl$page$curlink);

        }

        
    $format  '<a href="%s%s">&gt;</a> <a href="%1$s11">&gt;&gt;</a>';
        
    $return .= sprintf($format$this->baseurl '/page/'$current 1);

        return 
    $return;


    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Essentially I'm trying to create a central area to handle paging. It generates page results in groups of 10.

  11. #11
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the site is still not loading past this point, but now gives no error message.

  12. #12
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another note, I set up a little test environment on my machine here, and the pages display correctly.


Bookmarks

Posting Permissions

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