473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 18526
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

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

Similar topics

1
by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still...
2
by: Steven | last post by:
Hi All, I am moving some php code from a Linux machine to a Windows 2000 machine with the code belowe I get the following error : Notice: Undefined offset: 1 in c:\inetpub\wwwroot\test.php on...
4
by: Richard Lawrence | last post by:
Hi there, I'm having a problem with PHP which I'm not sure how to best solve. Here is the code: $fp = fopen("comments.txt", "r"); while(!feof($fp)) { $line = fgets($fp, 1024); list($key,...
3
by: delusion7 | last post by:
I am getting this error "Undefined offset: 9 in C:\Course Technology\1687-5\Chapter.10\UpdateContactInfo.php on line 36" this is the code I am recieving the error on: if...
6
by: nicy12 | last post by:
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...
9
by: simple12 | last post by:
Hello I have a script which have the facility of entering any code to some part of a webpage. I have some problems with it. When i put some code in the script then their is no error shown. When i...
1
by: atang | last post by:
Hi, I found the code here, it's excately what i am looking for, ie8 give this error "Notice: Undefined offset: 49 in.." which is this line "if ($num_of_chars==$required)", i'm a newbie, i don't know...
1
by: Mary meer | last post by:
i am trying to switch array value but i get this error message: Undefined offset 2 $at=array( array('Title:','title',2,10), array('First Name:','fname',2,10 ), ...
6
by: tbebest | last post by:
Hi , i have a form and it has A problem: i upload my 3 image and it uploaded in definded path but in insert in db filed there are empty: Notice: Undefined offset: 0 in C:\wamp\www Notice:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.