Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/Confusing.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Confusing

Posted by Garrie (Garrie), 20 November 2002
I am getting the following error message, but I don't understand how it is being caused. Can anyone help?
- Garrie

Notice: Uninitialized string offset: 7 in E:\CMIS\HTML\KCL\PHP\TTGrid.php on line 30

Code:
<?php
function generateHeader($array)
{
  reset ($array);
  while ( list ($key) = each ($array) )
  {
    echo "   <th>$key</th>\n";
  }      
}

function generateTable($array)
{
  $weekDay=array( 1=>'SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT' );

  reset ($array);
  for ($hour=7; $hour<=20; $hour++ )
  {
     for ($min=0; $min<60; $min=$min+15)
     {
        if ($min==0)
           {$tr = sprintf("<tr><th rowspan=\"4\">%02d:00</th>",$hour);}
        else
           {$tr = '<tr><td></td>';}

        $time = sprintf("%02d:%02d",$hour,$min);
        for ($weekDayNum=1; $weekDayNum<=7; $weekDayNum++)
        {
           $weekDayName=$weekDay[$weekDayNum];
           $tr .= '<td>';
           if ( isset($array[$weekDayName][$time]))
              {

                 $desc='<table border="1" ><tr><th>Code</th><th>Name</th></tr>';
                 for ($i=0; $i<count($array[$weekDayName][$time]); $i++)
                 {
                    $desc .='<tr><td>'.$array[$weekDayName][$time][$i]['CODE'].'</td><td>'.$array[$weekDayName][$time][$i]['NAME']."</td></tr>\n";
                 }
                 $desc .= '</table>';
                 $tr   .= $desc;
              }
           $tr .= '</td>';
        }
        $tr .= "</tr>\n";
        echo $tr;
     }
  }
}



  $timetable = array (
                      'SUN' => '',
                      'MON' => array (
                                        '09:30' => array(array('CODE'=>'ENG100', 'NAME'=>'English')),
                                        '14:30' => array(array('CODE'=>'FRE100', 'NAME'=>'French')),
                                        '17:30' => array(array('CODE'=>'SPA100', 'NAME'=>'Spanish'))
                                     ),
                      'TUE' => '',
                      'WED' => array (
                                        '19:30' => array(array('CODE'=>'BIO200', 'NAME'=>'Biology')),
                                        '14:30' => array(
                                                         array('CODE'=>'CHE200', 'NAME'=>'Chemistry'),
                                                         array('CODE'=>'FRE100', 'NAME'=>'French')
                                                        ),
                                        '17:30' => array(array('CODE'=>'PSY200', 'NAME'=>'Psychology'))
                                     ),
                      'THU' => '',
                      'FRI' => '',
                      'SAT' => ''
                     );

?>


<html>
  <head>
     <title>Timetable Grid</title>
  </head>
  <body>
     <table border="1" cellspacing="3" cellpadding="0">
     <tr>
        <th>Day</th>
<?php
  generateHeader($timetable);
?>
     </tr>
     <tr><td colspan="8"><hr /></td></tr>
     <tr>
     <th>Time</th>
<?php
  generateTable ($timetable);
?>
     </table>
  <?php //echo '<pre>'; print_r($timetable); echo '</pre>'; ?>
  </body>
</html>


Posted by admin (Graham Ellis), 21 November 2002
Well ... I cut and pasted the code, added a <?php onto the start, and ran it (from the command line).   Came up with a whole swathe of HTML table tags bit - dorry - no error or warning messages.  I also tried to cut out a few lines of code where I anticipated the error occurred (around line 30) and run just that snippet of code - again, no problems ...

The error message looks as if you are using a string as if it was an array to get at certain characters in that string - a valid thing to do in PHP.  It would indicate to me that you're looking for position 7 of something that only has 7 positions, which would be numbered 0 to 6.  Suspisions fell on $weekDay and on $hour, both of which have a constart "7" in there.

I'm really only rolling the evidence here; can't see anything obvious and since I can't reporduce the problem I have nothing to solve - it's like a murder without a body.   Garrie - have a look at the thoughts above and see if they help.  If not, see if you can tie the problem down to a few lines of code anr repost, also letting me know the platform and PHP version that you're using, and I'll have a further more targetted look.

Posted by Garrie (Garrie), 21 November 2002
on 11/21/02 at 06:47:02, Graham Ellis wrote:
The error message looks as if you are using a string as if it was an array to get at certain characters in that string - a valid thing to do in PHP.  It would indicate to me that you're looking for position 7 of something that only has 7 positions, which would be numbered 0 to 6.  Suspisions fell on $weekDay and on $hour, both of which have a constart "7" in there.

The thing is I'm not! I'm using an array as an array.

Webserver = BadBlue 1.7 (http://www.badblue.com/)

PHP= 4.2.2

- Garrie


Posted by admin (Graham Ellis), 21 November 2002
Yes, I know you're not!  Odd one at first glance!!

I'm kinda stuck on this one as I don't get the error message - but then I'm running 4.1.2 on most of my systems to maintain compatability for older scripts and to mirror the ISP we use.

I would suggest that you take a copy of the whole script, then keep knocking out chunks so that you can reproduce it in a smaller bit of code - I can see nothing that's obviusly wrong at about line 30, which is where it's reporting the problem, but it could be some other effect / way that a variable is initialised or something.  

I would also be tempted to switch $weekDay to have elements 0 to 6 rather than 1 to 7, adjust the rest of the code accordingly, and see if the error report goes away (IMHO there's a slim but worthwhile chance that it will), changes to report "6" or stays reporting "7".

Sorry that it's back to research but that's how it goes!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho