+ Reply to Thread
Results 1 to 2 of 2

Thread: PHP array help

  1. #1
    mash99 is offline x10Hosting Member mash99 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    10

    PHP array help

    Warning: array_unshift() [function.array-unshift]: The first argument should be an array in /home/mash99/public_html/thebestoptions.co.cc/book-store/finstall.php on line 383

    Warning: array_unshift() [function.array-unshift]: The first argument should be an array in /home/mash99/public_html/thebestoptions.co.cc/book-store/finstall.php on line 384



    // get old's spec:
    $stopat=$_SERVER['DOCUMENT_ROOT']; // e.g. /usr/www
    $running=$mypath; // to start with, e.g. /usr/www/users/ewalker/seo-toys/bookadder
    while ($running!==$stopat)
    {
    $lastslash=strrpos($running,$slash);
    if ($lastslash==NULL) break; // emergency fail-safe...
    $running=substr($running,0,$lastslash); // e.g. /usr/www/users/ewalker/seo-toys
    $target=$running.'/robots.txt';
    if (is_file($target)!==FALSE) break;
    }
    $oldrobots=bringme($running.'/robots.txt'); // may fail if no file found--that's ok
    // process:
    $robots=NULL;
    if ($oldrobots!=NULL)
    {
    $target=$localslash.$localurl; // such as: /red-cats-books/
    foreach($oldrobots as $line)
    {if (strpos($line,$target)===FALSE && strpos($line,$localslash.'freebie'.$localslash)=== FALSE) $robots[]=$line;} // don't take old package lines
    $top=count($robots);
    $top=$top-1; // convert to 0-based offset
    if (trim($oldrobots[$top])!=NULL) $robots[]=$crlf; // tag on blank separator line (if extant file)
    }

    // Add local stuff:
    array_unshift($robots,$crlf); // prepend a blank separator line...
    array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf); // ...then prepend the sitemap directive
    $robots[]='User-agent: *'.$crlf;
    $robots[]='Disallow: '.$localslash.$localurl.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'allbooks/allbooks.'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.$myshop.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'abechange.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'abes.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'book-search.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'free.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'holder.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'search.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'used-books.php'.$crlf;
    $robots[]='Allow: '.$localslash.$localurl.'Bookshop_index.xml'.$crlf ;
    $robots[]='Allow: '.$localslash.$localurl.$freebdirname.'-sitemap'.$crlf;
    Last edited by mash99; 04-25-2010 at 10:11 AM. Reason: Explaination

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP array help

    For our sake, use [php], [html] or [code] tags (as appropriate) to delineate code and make it readable.

    The error and source code comments tells you exactly what's wrong: $robots isn't an array, which can happen if no "robots.txt" file is found. If a value isn't what you expect, debug. Either use an interactive debugger (install e.g. XAMPP or WAMP, XDEBUG and an XDEBUG compatible client) or simply print out the current value with var_dump or var_export.

    Quote Originally Posted by mash99 View Post
    PHP Code:
      while ($running!==$stopat)
      {
        
    $lastslash=strrpos($running,$slash);
        if (
    $lastslash==NULL) break;  // emergency fail-safe...
        
    $running=substr($running,0,$lastslash);  // e.g. /usr/www/users/ewalker/seo-toys 
    Try dirname instead.

    Quote Originally Posted by mash99 View Post
    PHP Code:
        if (is_file($target)!==FALSE) break; 
    is_file only returns booleans, so there's no need to compare with FALSE, and there's definitely no need for strict comparison.

    Quote Originally Posted by mash99 View Post
    PHP Code:
        {if (strpos($line,$target)===FALSE && strpos($line,$localslash.'freebie'.$localslash)===FALSE$robots[]=$line;}  // don't take old package lines 
    Separate lines for readability.

    Quote Originally Posted by mash99 View Post
    PHP Code:
      }

      
    //     Add local stuff:
      
    array_unshift($robots,$crlf);                                             // prepend a blank separator line...
      
    array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf);  // ...then prepend the sitemap directive
      
    $robots[]='User-agent: *'.$crlf;
      ... 
    All these lines should go in the "if ($oldrobots!=NULL) {" block.

    PHP Code:
    // get old's spec:
    $stopat=$_SERVER['DOCUMENT_ROOT']; // e.g. /usr/www
    $running=$mypath// to start with, e.g. /usr/www/users/ewalker/seo-toys/bookadder
    while ($running!==$stopat) {
        
    $running=dirname($running); // e.g. /usr/www/users/ewalker/seo-toys
        
    $target=$running.'/robots.txt';
        if (
    is_file($target)) break;
    }

    $oldrobots=bringme($running.'/robots.txt'); // may fail if no file found--that's
    $robots=NULL;
    if (
    $oldrobots!=NULL) {
        
    $target=$localslash.$localurl// such as: /red-cats-books/
        
    foreach($oldrobots as $line) {
            if (
    strpos($line,$target)===FALSE
                
    && strpos($line,$localslash.'freebie'.$localslash)=== FALSE)
            { 
                
    $robots[]=$line;
            }
        } 
    // don't take old package lines
        
    $top=count($robots);
        
    $top=$top-1// convert to 0-based offset
        
    if (trim($oldrobots[$top])!=NULL) {
            
    $robots[]=$crlf// tag on blank separator line (if extant file)
        
    }
        
        
    // Add local stuff:
        
    array_unshift($robots,$crlf); // prepend a blank separator line...
        
    array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf); // 
    ... 
    You should consider refactoring some of that into functions or classes.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  2. Array sorting in php
    By drf1229 in forum Programming Help
    Replies: 2
    Last Post: 01-06-2010, 08:53 PM
  3. [REQ] [200] PHP create array
    By diabolo in forum The Marketplace
    Replies: 3
    Last Post: 12-15-2008, 02:39 PM
  4. Help with php array
    By RossSchVle in forum Programming Help
    Replies: 3
    Last Post: 11-09-2008, 07:36 AM
  5. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers