469,086 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

Undefined offset error in php code.

3
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.cumulative_usage.es.php on line 16
The code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         $current_year = date("Y");
  3.         $last_year = date("Y")-1;
  4.  
  5. ?>
  6. <p>Most viewed eprints:
  7. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_detail_date;range=4w">Past four weeks</a>]
  8. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_detail_date;year=<?php print $current_year; ?>">This year</a>]
  9. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_detail_date;year=<?php print $last_year; ?>">Last year</a>]
  10. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=show_detail_date">All years</a>]
  11. <br />Repository-wide statistics:
  12. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=cumulative_usage;range=all">by Year/month</a>]
  13. [<a href="<?php print $_SERVER['PHP_SELF']; ?>?action=cumulative_usage_country">by Country</a>]
  14. </p>
  15. <?php
  16.             $max_count = $GLOBALS["db_values"][0]["abstracts"];
  17.             for ($rs=0;$rs<count($GLOBALS["db_values"]);$rs++)
  18.         {
  19.                 if($max_count < $GLOBALS["db_values"][$rs]["abstracts"])
  20.                         $max_count = $GLOBALS["db_values"][$rs]["abstracts"];
  21.         }
  22.         $max_width = '500'; // Max pixel width of barchart.
  23.  
  24. print '<font face="Arial" size="-1">Click on a month to see document downloads for that month.</font>';
  25. print "<table>\n";
  26.         print '<tr><th bgcolor="#cccccc">Periods</th>
  27.                 <th bgcolor="#66ddee">Abstracts</th>
  28.                 <th bgcolor="#4477dd"><font color="#ffffff">Downloads</th>
  29.                 <th bgcolor="#cccccc">&nbsp;</th></tr>';
  30.         for ($rs=0;$rs<count($GLOBALS["db_values"]);$rs++)
  31.         {
  32.                 $monthnum = (int) strpos('JanFebMarAprMayJunJulAugSepOctNovDec', $GLOBALS["db_values"][$rs]["month"])/3 + 1;
  33.                 print '<tr><td><font face="Arial" size="-1"><a href="'.
  34.                         $_SERVER['PHP_SELF'].
  35.                         '?action=show_detail_date;year='.
  36.                         $GLOBALS["db_values"][$rs]["year"].
  37.                         ';month=' .
  38.                         $monthnum .
  39.                         '">' .
  40.                         $GLOBALS["db_values"][$rs]["year"].
  41.                         ' '.
  42.                         $GLOBALS["db_values"][$rs]["month"].
  43.                         '</a></font></td><td align="right"><font face="Arial" size="-1">'.
  44.                         $GLOBALS["db_values"][$rs]["abstracts"].
  45.                         '</font></td><td align="right"><font face="Arial" size="-1">'.
  46.                         $GLOBALS["db_values"][$rs]["downloads"].
  47.                         '</font></td>';
  48.                 $cur_count = $GLOBALS["db_values"][$rs]["abstracts"];
  49.  $col_width = (int) ($cur_count/$max_count * $max_width);
  50. $col_width = max($col_width, 1);
  51.                 $col_width .= "px";
  52.                 print '<td align="left"><img src="bars/hh.png" alt="abstracts" height="9" width="'.
  53.                         $col_width .
  54.                         '"><br />';
  55.                 $cur_count = $GLOBALS["db_values"][$rs]["downloads"];
  56.                 $col_width = (int) ($cur_count/$max_count * $max_width);
  57.                 $col_width = max($col_width, 1);
  58.                 $col_width .= "px";
  59. print '<img src="bars/hp.png" alt="downloads" height="9" width="'.
  60. $col_width .
  61.                         '"></td></tr>';
  62.         }
  63.         print "</table>\n";
  64. ?>
  65.  
Jan 22 '07 #1
6 4202
ronverdonk
4,258 Expert 4TB
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:
Jan 22 '07 #2
nicy12
3
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).
Jan 22 '07 #3
bolo
2
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 ..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $banner_File = "754.txt" ; 
  5. $total_Bannersize = 8 ;        
  6.  
  7.  
  8.  
  9. $rnd_Value = Get754(0, $total_Bannersize-1);
  10. $banner_Data = Get754Info($banner_File, $rnd_Value) ;
  11.  
  12. $Banner = split("::",$banner_Data);
  13.  
  14. $banner_Content = <<<content
  15.  
  16. <!-- Banner Ads Start -->
  17. <a title="$Banner[2]" href="$Banner[1]" target="_blank"> <img name="Banner" src="$Banner[0]" alt = "$Banner[2]" border=0>
  18. </a>
  19. <!-- Banner Ads Ends -->
  20.  
  21. content;
  22.  
  23. echo  $banner_Content  ;
  24.  
  25.  
  26. function Get754Info($filename, $line_number) {
  27.  
  28.          $fp = @fopen($filename, "r")
  29.                 or die ("Can't open file $filename on Line: " . __LINE__ . " in File: " . __FILE__);
  30.  
  31.          $lnCounter = 0 ;
  32.  
  33.          while (!feof ($fp)) {
  34.               $data = fgets($fp, 1024);
  35.  
  36.                If ($lnCounter == $line_number){
  37.                        $right_data = $data ;
  38.                        return $right_data ;
  39.                        break;
  40.                  }
  41.  
  42.                  $lnCounter++ ;
  43.         }
  44. }
  45.  
  46.  
  47. function Get754($min, $max){
  48.          $mtime = ((double)microtime()* 100000) ;
  49.          $mtime = explode(" ", $mtime);
  50.          $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;
  51.          $randval = mt_rand($min, $max);
  52.          return ($randval) ;
  53. }
  54.  
  55. ?>
  56.  
The line returning the error is Line 50 ..

Expand|Select|Wrap|Line Numbers
  1. $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;
What am I not seeing?

Oh .. PHP 5.0.4 ...

Thanks.

-bolo
Aug 11 '07 #4
Atli
5,058 Expert 4TB
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.

Expand|Select|Wrap|Line Numbers
  1. function Get754($min, $max){
  2.          // This line will return something like 12345.54321
  3.          $mtime = ((double)microtime()* 100000) ;
  4.  
  5.          // And this line tries to split on " " char, which doesn't exist
  6.          $mtime = explode(" ", $mtime);
  7.  
  8.          // So $mtime[1] doesnt exist, ergo PHP returns a warning.
  9.          $mtime = floatval($mtime[1]) + floatval($mtime[0]) ;
  10.  
  11.          // These two lines apear to be the only relevent lines.
  12.          // The other lines are redundant, from what I can tell.
  13.          $randval = mt_rand($min, $max);
  14.          return ($randval) ;
  15. }
  16.  
Aug 11 '07 #5
bolo
2
Hi Atli ..

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

Thanks.

-bolo
Aug 11 '07 #6
Atli
5,058 Expert 4TB
Hi Atli ..

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

Thanks.

-bolo
Glad I could help :)
Aug 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by lawrence | last post: by
2 posts views Thread by Steven | last post: by
4 posts views Thread by Richard Lawrence | last post: by
3 posts views Thread by delusion7 | last post: by
9 posts views Thread by simple12 | last post: by
2 posts views Thread by neridaj | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.