sort() expects parameter 1 to be array, null given

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
STHayden
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2003 12:38 am

sort() expects parameter 1 to be array, null given

Post by STHayden »

sort() expects parameter 1 to be array, null given
Variable passed to each() is not an array or object

I can't figure out what it is talking about since both have an array in them!

Code: Select all

Function getEvent ($theyear, $themonth, $theday)
{
$data_file = "../data/cd.dat";
    if(file_exists($data_file))
       {
       $data_lines = file($data_file);
       } 
     $Now = date(Ymd);
     sort($data_lines);         //it's angry here

     $ts =  mktime(0,0,0,substr($ts,4,2),substr($ts,6,2),substr($ts,0,4));
     while (list($Index, $Data) = each($data_lines))                       //And angry here as well
     {
      list($Date, $Type, $Title, $Time, $Location, $Price, $Description) = explode("|", $Data);
      
      if (($Date) && (substr($ts,0,4) == $theyear) && (substr($ts,4,2) == $themonth) && (substr($ts,6,2) == $theday))
      {
        if ($Title != "")
          {
          return "<br>$Title";
          }
      }
     }
ayron
Forum Newbie
Posts: 14
Joined: Tue Jun 03, 2003 11:18 pm
Location: Perth, Australia

Post by ayron »

maybe the file didn't exist?
STHayden
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2003 12:38 am

Post by STHayden »

no it's definetly there.

I acualy use this same code in a different file and it works just fine


\/ this works.

Code: Select all

<?php
    $data_file = "../data/cd.dat";
    if(file_exists($data_file))
       {
       $data_lines = file($data_file);
       } 
     $Now = date(Ymd); 
     sort($data_lines);                                 //HERE
     function ts8totime ($ts) {return mktime(0,0,0,substr($ts,4,2),substr($ts,6,2),substr($ts,0,4));}
     while (list($Index, $Data) = each($data_lines))           //HERE
     {
      list($Date, $Type, $Title, $Time, $Location, $Price, $Description) = explode("|", $Data);
      if ($Type == "CUB")
      {
        if (($Date) && ($Date != $LastDate))
          echo "    <BR><FONT SIZE="+1"><DIV CLASS="date">" . date("l, F j, Y",ts8totime($Date)) . "</DIV></FONT>\r\n";
        if ($Title != "")
          {
          echo "    <FONT SIZE="+1"><SPAN CLASS="event-title">$Title</SPAN></FONT>\r\n";
          echo "    <FONT SIZE="-1"><SPAN CLASS="event-data">($Time, $Location, $Price)</SPAN></FONT><BR>\r\n";
          echo "    <SPAN CLASS="event-description">$Description</SPAN><BR>\r\n";
          }
        $LastDate = $Date;
        }
      } 
     ?>
ayron
Forum Newbie
Posts: 14
Joined: Tue Jun 03, 2003 11:18 pm
Location: Perth, Australia

Post by ayron »

have you checked the paths are correct? the logic of the code you're showing suggests that the file hasn't been read - try an echo inside the if statement to see if it has been opened
STHayden
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2003 12:38 am

Post by STHayden »

your right. thanks for the help
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I usually put a line just before the file read that forces the var to be an array, in your case

Code: Select all

...   
    $data_file = "../data/cd.dat"; 
    $data_lines = array();
    if(file_exists($data_file)) &#123; 
      $data_lines = file($data_file); 
    &#125; 
    $Now = date(Ymd); 
    sort($data_lines);
...
Post Reply