Undefined offset error in php code.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicy12
    New Member
    • Dec 2006
    • 3

    Undefined offset error in php code.

    Hi! my name is Peter. iam working on the php platform.
    while trying to run and compile a program i get the undefined offset error
    iam nto much familiar with this error . Please help me . Thanks in advance.
    Error: Undefined offset: 0 in /es/ePrintsStats/includes/inc.html.cumula tive_usage.es.p hp on line 16
    The code:
    [code=php]
    <?php
    $current_year = date("Y");
    $last_year = date("Y")-1;

    ?>
    <p>Most viewed eprints:
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_ detail_date;ran ge=4w">Past four weeks</a>]
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_ detail_date;yea r=<?php print $current_year; ?>">This year</a>]
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_ detail_date;yea r=<?php print $last_year; ?>">Last year</a>]
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_ detail_date">Al l years</a>]
    <br />Repository-wide statistics:
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=cumul ative_usage;ran ge=all">by Year/month</a>]
    [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=cumul ative_usage_cou ntry">by Country</a>]
    </p>
    <?php
    $max_count = $GLOBALS["db_values"][0]["abstracts"];
    for ($rs=0;$rs<coun t($GLOBALS["db_values"]);$rs++)
    {
    if($max_count < $GLOBALS["db_values"][$rs]["abstracts"])
    $max_count = $GLOBALS["db_values"][$rs]["abstracts"];
    }
    $max_width = '500'; // Max pixel width of barchart.

    @
    print '<font face="Arial" size="-1">Click on a month to see document downloads for that month.</font>';
    print "<table>\n" ;
    print '<tr><th bgcolor="#ccccc c">Periods</th>
    <th bgcolor="#66dde e">Abstracts </th>
    <th bgcolor="#4477d d"><font color="#ffffff" >Downloads</th>
    <th bgcolor="#ccccc c">&nbsp;</th></tr>';
    for ($rs=0;$rs<coun t($GLOBALS["db_values"]);$rs++)
    {
    $monthnum = (int) strpos('JanFebM arAprMayJunJulA ugSepOctNovDec' , $GLOBALS["db_values"][$rs]["month"])/3 + 1;
    print '<tr><td><font face="Arial" size="-1"><a href="'.
    $_SERVER['PHP_SELF'].
    '?action=show_d etail_date;year ='.
    $GLOBALS["db_values"][$rs]["year"].
    ';month=' .
    $monthnum .
    '">' .
    $GLOBALS["db_values"][$rs]["year"].
    ' '.
    $GLOBALS["db_values"][$rs]["month"].
    '</a></font></td><td align="right">< font face="Arial" size="-1">'.
    $GLOBALS["db_values"][$rs]["abstracts"].
    '</font></td><td align="right">< font face="Arial" size="-1">'.
    $GLOBALS["db_values"][$rs]["downloads"].
    '</font></td>';
    $cur_count = $GLOBALS["db_values"][$rs]["abstracts"];
    $col_width = (int) ($cur_count/$max_count * $max_width);
    $col_width = max($col_width, 1);
    $col_width .= "px";
    print '<td align="left"><i mg src="bars/hh.png" alt="abstracts" height="9" width="'.
    $col_width .
    '"><br />';
    $cur_count = $GLOBALS["db_values"][$rs]["downloads"];
    $col_width = (int) ($cur_count/$max_count * $max_width);
    $col_width = max($col_width, 1);
    $col_width .= "px";
    print '<img src="bars/hp.png" alt="downloads" height="9" width="'.
    $col_width .
    '"></td></tr>';
    }
    print "</table>\n";
    ?>
    [/code]
    Last edited by Atli; Aug 11 '07, 11:06 AM. Reason: Added code tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please read the Posting Guidelines before you post in this forum! Especially the part about enclosing code within code or php tags!

    And what is line 16?

    Ronald :cool:

    Comment

    • nicy12
      New Member
      • Dec 2006
      • 3

      #3
      Originally posted by ronverdonk
      Please read the Posting Guidelines before you post in this forum! Especially the part about enclosing code within code or php tags!

      And what is line 16?

      Ronald :cool:
      Thanks for the Reply,

      line 16 is $max_count = $GLOBALS["db_values"][0]["abstracts"];
      Undefined offset: 0 is the error that i come accross in the above line (line 16).

      Comment

      • bolo
        New Member
        • Aug 2007
        • 2

        #4
        Okay .. think I'm just staring at this too long .. I have a random banner script that works just fine .. except one line is generating Undefined offset: 1 errors ...

        The full script is ..

        Code:
        <?php
        
        
        $banner_File = "754.txt" ; 
        $total_Bannersize = 8 ;		
        
        
        
        $rnd_Value = Get754(0, $total_Bannersize-1);
        $banner_Data = Get754Info($banner_File, $rnd_Value) ;
        
        $Banner = split("::",$banner_Data);
        
        $banner_Content = <<<content
        
        <!-- Banner Ads Start -->
        <a title="$Banner[2]" href="$Banner[1]" target="_blank"> <img name="Banner" src="$Banner[0]" alt = "$Banner[2]" border=0>
        </a>
        <!-- Banner Ads Ends -->
        
        content;
        
        echo  $banner_Content  ;
        
        
        function Get754Info($filename, $line_number) {
                 
        		 $fp = @fopen($filename, "r")
                        or die ("Can't open file $filename on Line: " . __LINE__ . " in File: " . __FILE__);
        
                 $lnCounter = 0 ;
        
        		 while (!feof ($fp)) {
        			  $data = fgets($fp, 1024);
                 
        			   If ($lnCounter == $line_number){
        				       $right_data = $data ;
        					   return $right_data ;
        				       break;
        				 }
        
                         $lnCounter++ ;
                }
        }
        
        
        function Get754($min, $max){
                 $mtime = ((double)microtime()* 100000) ;
                 $mtime = explode(" ", $mtime);
                 $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;
                 $randval = mt_rand($min, $max);
                 return ($randval) ;
        }
        
        ?>
        The line returning the error is Line 50 ..

        Code:
        $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;
        What am I not seeing?

        Oh .. PHP 5.0.4 ...

        Thanks.

        -bolo

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi, bolo, and welcome to TSDN!

          You are trying to split a double value by using a space character. A double value has no space characters so it will not be split, but the entire value ends up in array index 0.

          [code=php]
          function Get754($min, $max){
          // This line will return something like 12345.54321
          $mtime = ((double)microt ime()* 100000) ;

          // And this line tries to split on " " char, which doesn't exist
          $mtime = explode(" ", $mtime);

          // So $mtime[1] doesnt exist, ergo PHP returns a warning.
          $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;

          // These two lines apear to be the only relevent lines.
          // The other lines are redundant, from what I can tell.
          $randval = mt_rand($min, $max);
          return ($randval) ;
          }
          [/code]

          Comment

          • bolo
            New Member
            • Aug 2007
            • 2

            #6
            Hi Atli ..

            YOU are a freakin' genius. Took the lines out. Everything still works. No error.

            Thanks.

            -bolo

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by bolo
              Hi Atli ..

              YOU are a freakin' genius. Took the lines out. Everything still works. No error.

              Thanks.

              -bolo
              Glad I could help :)

              Comment

              Working...