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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raydiamond4u
    New Member
    • Feb 2010
    • 4

    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\ima gegallery\suppo rtfile\include\ config.php on line 25
    How do i correct the error?
    Here is the code:

    Code:
    <?php
    
         // Include file for database connectivity
    
         $db_server = "localhost";
         $db_user = "root";
         $db_pass = "";
         $db_name = "images";
    
         DEFINE("IMAGE_DISPLAY", 3);
         DEFINE("IMAGE_LOC", '../photos/');
    	 DEFINE("THUMB_LOC", '../thumbs/');
         
         function db_connect(){
             global $db_server;
             global $db_user;
             global $db_pass;
             global $db_name;
    
             $dbcnx = mysql_connect($db_server, $db_user, $db_pass) or die("Error connecting to database: " . mysql_error());
             $dbsel = mysql_select_db($db_name, $dbcnx) or die("Error reading from database table: " . mysql_error());
         }
    
         function format_date($date){
             list($yyyy, $mm, $dd) = explode("-", $date);
             $day[] = explode(' ', $dd);
             $date = date('F jS, Y', time(0,0,0,$mm,$day[0],$yyyy));
             //echo("<br><br>Now: " . $date);
             return $date;
             
         }
         
    	 function displayPage($msg = "", $title="", $cell = true){
    ?>
    	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    	<html>
    	<head>
    	<title><?php echo($title); ?></title>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    	</head>
    	<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    	<table width="100%" border="0" cellpadding="0" cellspacing="0">
    	  <tr>
    		<td>
    			<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
    			  <tr>
    				<td width="40%" valign="top"><h1><?php echo($title); ?></h1></td>
    			  </tr>
    			</table>
    			<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">
    			<tr>
    				<?php
    					// Display opening <td> tag
    					if ($cell)
    						echo("<td>");
    						
    					echo($msg);
    					
    					// Display closing <td> tag
    					if ($cell)
    						echo("</td>");			
    				?>
    			</tr>
    			</table>
    			<table width="60%" border="0" align="center" cellpadding="3" cellspacing="0">
                  <tr>
                    <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
                        New Album</a> | <a href="http://bytes.com/submit/gallery.php">View Gallery</a></td>
                  </tr>
    			</table>
    		</td>
    	  </tr>
    	</table>
    	</body>
    	
    <?php
    	 }       
    ?>
    Last edited by Atli; Feb 18 '10, 08:37 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • raydiamond4u
      New Member
      • Feb 2010
      • 4

      #3
      Thanks so much for the prompt reply.
      Please, what exactly should i change in the code so that it will stop displaying those errors.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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.

        Comment

        • raydiamond4u
          New Member
          • Feb 2010
          • 4

          #5
          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.
          Code:
          function format_date($date){
                   list($yyyy, $mm, $dd) = split("-", $date);
                   $day[] = split(' ', $dd);
                   $date = date('F jS, Y', mktime(0,0,0,$mm,$day[0],$yyyy));
                   //echo("<br><br>Now: " . $date);
                   return $date;
          Last edited by Atli; Feb 18 '10, 04:29 PM. Reason: Please use [code] tags!

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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.

            Comment

            • raydiamond4u
              New Member
              • Feb 2010
              • 4

              #7
              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.

              Code:
              <?php
                    // Retrieve albums from database
                    $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'];
                    $result = @mysql_query($sql) or die("Error retrieving records from the database: " . mysql_error());
              
                    $i = 0; // Row counter
              
              	   while( $row = mysql_fetch_assoc($result))
              	   {
              	   		$data[] = $row;
              	   }
              	
              	   $count = ( ceil( count( $data ) / IMAGE_DISPLAY ));	
              
              	
              	   for( $i = ( int )0; $i < $count * IMAGE_DISPLAY; $i++ )
              	   {
                      	if ( ( $i % IMAGE_DISPLAY ) == 0 && ($i != 0))
                      	{
                      		echo("</tr>\n<tr>");
                      	}  	
                      	@$photo_date = format_date($data[$i]['photo_date']);	
                      	if ( @$data[$i]['photo_location'] )
                      	{
                      		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>");
                              echo("<br /><p>Photo Details: " . $data[$i]['photo_desc'] . "<br />Date: " . $photo_date . "</p></td>\n");
                      	}
                      	else
                      	{
                      		echo("<td valign=\"top\" width=\"" . floor(100 / IMAGE_DISPLAY) . "%\">&nbsp;");
                      		echo("</td>\n");
                      	}     	
                      }
              ?>
              Please what do i need to do to display the images one by one?
              Thanks
              Last edited by Dormilich; Feb 23 '10, 11:42 AM. Reason: Please use [code] tags when posting code

              Comment

              Working...