469,336 Members | 5,929 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How do i remove this error from my code:Notice: Undefined offset: 2 in C:\wamp\www\im

Hello. My name is Raymond.
My code is generating the following error: Notice: Undefined offset: 2 in C:\wamp\www\imagegallery\supportfile\include\confi g.php on line 25
How do i correct the error?
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.      // Include file for database connectivity
  4.  
  5.      $db_server = "localhost";
  6.      $db_user = "root";
  7.      $db_pass = "";
  8.      $db_name = "images";
  9.  
  10.      DEFINE("IMAGE_DISPLAY", 3);
  11.      DEFINE("IMAGE_LOC", '../photos/');
  12.      DEFINE("THUMB_LOC", '../thumbs/');
  13.  
  14.      function db_connect(){
  15.          global $db_server;
  16.          global $db_user;
  17.          global $db_pass;
  18.          global $db_name;
  19.  
  20.          $dbcnx = mysql_connect($db_server, $db_user, $db_pass) or die("Error connecting to database: " . mysql_error());
  21.          $dbsel = mysql_select_db($db_name, $dbcnx) or die("Error reading from database table: " . mysql_error());
  22.      }
  23.  
  24.      function format_date($date){
  25.          list($yyyy, $mm, $dd) = explode("-", $date);
  26.          $day[] = explode(' ', $dd);
  27.          $date = date('F jS, Y', time(0,0,0,$mm,$day[0],$yyyy));
  28.          //echo("<br><br>Now: " . $date);
  29.          return $date;
  30.  
  31.      }
  32.  
  33.      function displayPage($msg = "", $title="", $cell = true){
  34. ?>
  35.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  36.     <html>
  37.     <head>
  38.     <title><?php echo($title); ?></title>
  39.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  40.     </head>
  41.     <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  42.     <table width="100%" border="0" cellpadding="0" cellspacing="0">
  43.       <tr>
  44.         <td>
  45.             <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
  46.               <tr>
  47.                 <td width="40%" valign="top"><h1><?php echo($title); ?></h1></td>
  48.               </tr>
  49.             </table>
  50.             <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
  51.             <tr>
  52.                 <?php
  53.                     // Display opening <td> tag
  54.                     if ($cell)
  55.                         echo("<td>");
  56.  
  57.                     echo($msg);
  58.  
  59.                     // Display closing <td> tag
  60.                     if ($cell)
  61.                         echo("</td>");            
  62.                 ?>
  63.             </tr>
  64.             </table>
  65.             <table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
  66.               <tr>
  67.                 <td><a href="http://bytes.com/submit/admin/index.php">Main Menu</a>&nbsp;| <a href="http://bytes.com/submit/admin/new_album.php">Create
  68.                     New Album</a> | <a href="http://bytes.com/submit/gallery.php">View Gallery</a></td>
  69.               </tr>
  70.             </table>
  71.         </td>
  72.       </tr>
  73.     </table>
  74.     </body>
  75.  
  76. <?php
  77.      }       
  78. ?>
Feb 18 '10 #1
6 18180
Atli
5,058 Expert 4TB
Hey.

In this case, the warning is being shown because the explode call is not creating the third (offset: 2) element that your list call is expecting. That, in turn, means that your $date format is invalid.

The best way to fix this is to verify that the $date is valid by counting the elements before trying to use them (see count()). Make sure all three elements exist, and if not, either trigger a warning or use a default value.
Feb 18 '10 #2
Thanks so much for the prompt reply.
Please, what exactly should i change in the code so that it will stop displaying those errors.
Feb 18 '10 #3
Atli
5,058 Expert 4TB
You need to replace line #25 with code that explodes the $date into a variable and counts the elements in the variable. If it counts 3 elements, proceed with the code. If not, you need to either:
  1. Use a default value.
  2. Try to "guess" the real value.
  3. Bail out with an error.
I would start with #3 and work my way up to the others.

Try it; see how it goes. If you run into trouble, post your attempts here and we will try to guide you in the right direction.
Feb 18 '10 #4
i tried using the mktim() function but it generated more errors.
i just need a quick way to fix this so that the errors stop showing on the web browzer.
Expand|Select|Wrap|Line Numbers
  1. function format_date($date){
  2.          list($yyyy, $mm, $dd) = split("-", $date);
  3.          $day[] = split(' ', $dd);
  4.          $date = date('F jS, Y', mktime(0,0,0,$mm,$day[0],$yyyy));
  5.          //echo("<br><br>Now: " . $date);
  6.          return $date;
Feb 18 '10 #5
Atli
5,058 Expert 4TB
If you just want the warning to disappear then you could prefix the list call with a @ char. Note that this does not fix the problem! It just hides the error message.

i tried using the mktim() function but it generated more errors.
I didn't notice it before, but your use of the time function on line #27 of the code in your first post is incorrect. The time() function takes no parameters. - It ONLY returns the current timestamp. You can not use it to construct a timestamp from arbitrary data.

You should be using the mktime function, like you posted in your last post. It generates errors because - like with the other warning - the date data you are feeding it is invalid.

All of this can be fixed by following the instructions I posted in my last post.
Feb 18 '10 #6
Thanks so much for your assistance. i had to hide all my errors for quick deployment.
Yet i still need more assistance.
i want to display my images one after the other such that the viewer will be able to click next and previous from different albums.

the following code only displays the images in a table format on the same page.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.       // Retrieve albums from database
  3.       $sql = "SELECT photos.photo_id, photos.photo_title, photos.photo_desc, photos.photo_date, photos.photo_location, photos.thumbnail_location FROM photos WHERE photos.album_id = " . $_GET['album_id'];
  4.       $result = @mysql_query($sql) or die("Error retrieving records from the database: " . mysql_error());
  5.  
  6.       $i = 0; // Row counter
  7.  
  8.        while( $row = mysql_fetch_assoc($result))
  9.        {
  10.                $data[] = $row;
  11.        }
  12.  
  13.        $count = ( ceil( count( $data ) / IMAGE_DISPLAY ));    
  14.  
  15.  
  16.        for( $i = ( int )0; $i < $count * IMAGE_DISPLAY; $i++ )
  17.        {
  18.             if ( ( $i % IMAGE_DISPLAY ) == 0 && ($i != 0))
  19.             {
  20.                 echo("</tr>\n<tr>");
  21.             }      
  22.             @$photo_date = format_date($data[$i]['photo_date']);    
  23.             if ( @$data[$i]['photo_location'] )
  24.             {
  25.                 echo("<td valign=\"top\" width=\"" . floor(100 / IMAGE_DISPLAY) . "%\"><p><a href=\"view_photo.php?photo_id=" . $data[$i]['photo_id'] . "\"><img src=\"" . $data[$i]['thumbnail_location'] . "\" title=\"Photo Date: " . $photo_date . "\" align=\"center\"></a>");
  26.                 echo("<br /><p>Photo Details: " . $data[$i]['photo_desc'] . "<br />Date: " . $photo_date . "</p></td>\n");
  27.             }
  28.             else
  29.             {
  30.                 echo("<td valign=\"top\" width=\"" . floor(100 / IMAGE_DISPLAY) . "%\">&nbsp;");
  31.                 echo("</td>\n");
  32.             }         
  33.         }
  34. ?>
Please what do i need to do to display the images one by one?
Thanks
Feb 23 '10 #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
1 post views Thread by atang | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.