TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 196,004
  • Total Topics: 21,330
  • Online today: 367
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 77
  • Total: 77

Undefined constants and variables

Started by JPDeni, July 24, 2006, 01:59:08 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

JPDeni

This is problem is similar to that described in another post -- Loads of pages of the same error -- but it seems to be bigger. I have a number of php articles and blocks and they all seem to be generating errors.

For example, I have a block that uses my own version of the calendar block. The code is as follows:

global $db_prefix, $user_info, $scripturl, $modSettings, $txt, $sc, $context;

require_once('UserFunctions.php');

$max_events = 7;

// Find all events which are happening in the near future that the member can see.
$request = db_query("
SELECT
        cal.ID_EVENT, cal.startDate, cal.endDate, cal.title, cal.ID_MEMBER, cal.ID_TOPIC,
cal.ID_BOARD, t.ID_FIRST_MSG
FROM {$db_prefix}calendar AS cal
LEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = cal.ID_BOARD)
LEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = cal.ID_TOPIC)
WHERE cal.endDate >= '" . strftime('%Y-%m-%d', forum_time(false)) . "'
             AND (cal.ID_BOARD = 0 OR $user_info[query_see_board])
ORDER BY cal.startDate ASC
LIMIT $max_events", __FILE__, __LINE__);
$return = array();
$duplicates = array();
while ($row = mysql_fetch_assoc($request))
{
  // Check if we've already come by an event linked to this same topic with the same title... and don't display it if we have.
  if (!empty($duplicates[$row['title'] . $row['ID_TOPIC']]))
    continue;

  // Censor the title.
  censorText($row['title']);

  if ($row['startDate'] < strftime('%Y-%m-%d', forum_time(false)))
    $date = strftime('%Y-%m-%d', forum_time(false));
  else
    $date = $row['startDate'];

  $return[$date][] = array(
        'id' => $row['ID_EVENT'],
'title' => $row['title'],
'can_edit' => allowedTo('calendar_edit_any') || ($row['ID_MEMBER'] == $ID_MEMBER && allowedTo('calendar_edit_own')),
'modify_href' => $scripturl . '?action=' . ($row['ID_BOARD'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['ID_FIRST_MSG'] . ';topic=' . $row['ID_TOPIC'] . '.0;calendar;') . 'eventid=' . $row['ID_EVENT'] . ';sesc=' . $sc,
'href' => $row['ID_BOARD'] == 0 ? '' : $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => $row['ID_BOARD'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['title'] . '</a>',
'start_date' => $row['startDate'],
'end_date' => $row['endDate'],
);

  // Let's not show this one again, huh?
  $duplicates[$row['title'] . $row['ID_TOPIC']] = true;
}
mysql_free_result($request);
if ($return)
{
$num_events = count($return);

$i =0;
foreach ($return as $mday => $array)
{
  foreach ($array as $event)
  {
   ++$i;
   if (date('Y-m-d') == $event[start_date])
     echo 'Today';
   else
     echo parse_date($event[start_date]);
   if ($event[end_date] <> $event[start_date])
     echo ' - ' . parse_date($event[end_date]);
   echo '<br />';
   if ($event[href])
    echo $event[link];
   else
    echo $event[title];
    if ($event['can_edit'])
      echo '
<a href="' . $event['modify_href'] . '" style="color: #FF0000;">*</a> ';
   if ($i < $num_events)
     echo '<br /><br />';
  }
}
}   

else
  echo 'There are no scheduled upcoming events.';


"UserFunctions.php" is a file of subroutines that I wrote that I use multiple times, including "parse_date." That's not the problem.

The error log includes the following, every time I load the main page:


Quote8: Use of undefined constant end_date - assumed 'end_date'
File: /home/.salinger/jpdenico/site.jpdeni.com/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 65

8: Use of undefined constant start_date - assumed 'start_date'
File: /home/.salinger/jpdenico/site.jpdeni.com/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 64
 
8: Use of undefined constant end_date - assumed 'end_date'
File: /home/.salinger/jpdenico/site.jpdeni.com/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 64

8: Use of undefined constant start_date - assumed 'start_date'
File: /home/.salinger/jpdenico/site.jpdeni.com/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 63

8: Use of undefined constant start_date - assumed 'start_date'
File: /home/.salinger/jpdenico/site.jpdeni.com/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 60

I'm assuming these errors are the result of the Events php code, but I don't know how to fix it.

There are other errors, too, but I thought I'd start with this one.

G6Cad

They are more likely langauge strings errors, missing langauge files or strings maby? What mods do you have installed there?

JPDeni

The only mod I have installed on SMF is TP. (SMF 1.1 RC2  TP 8.6) I'm really pretty sure that it's related to the php code that I posted above.  Of the blocks that show on the front page, the only thing that would use "start_date" and "end_date" is the event code I posted.

There are other errors that are obviously associated with other php blocks as well, but I thought if I could find out how to fix this one, I might be able to use the same logic to fix at least some of the others.

Nokonium

Errors like this
end_date - assumed 'end_date'

Are (in my experience) associated with, as it suggests, ' and ' missing from around a text string. However I cannot find one. Assumed says that it found something similar and used it, but what and where

I'm interested in knowing more about this to help me understand the problem.

IchBin

I'm no PHP guru, but it looks to me like there are several places where start_date/end_date are being called from the array but they don't have the ' and ' surrounding them.

Nokonium

I see what you mean, [endDate] should be ['endDate'] etc

I was looking for something like $txt[endDate]

JPDeni

#6
Sometimes the answer is so obvious!! Thank you, IchBinÃ,â,,¢.

I've managed to get all of them except for a reference to MSN. That's not in anything that I wrote, but it only comes up when TP is installed.
--

Edited to add:

I found the information that this MSN problem is a known bug. I'll wait until the new version comes out.

bloc

IchBin is correct..when accessing an array, you need to enclose string numbered entries with apostrophes. Only numbers can be without them.

So $var[mytext] is wrong, while $val['mytext'] is correct. $val[34] on the other hand, is valid.

JPDeni

It would make things easier if it just wouldn't work unless you put the quotes in. :-) I got into bad habits, but now I'm back on the straight and narrow.

I appreciate this forum being here and all the help that's available.

IchBin

I was just happy to know that I knew "just enough" php to actually see that lol. Bloc gave a great explanation though and I learned something with his referrence. Thanks Bloc!

Thurnok

Quote from: JPDeni on July 25, 2006, 05:33:10 PM
It would make things easier if it just wouldn't work unless you put the quotes in.

JPDeni, for the sake of your curiosity:
Unquoted strings in php are considered constants.  The reason it actually works is because the php parser takes any unquoted literal index that does not match a known symbol (or constant) and turns it into a string.  If for example however, you had a constant in your code  somewhere called end_date it would have obviously not worked in addition to providing an error.  So though you can actually get away with unquoted literal indexes, you may actually break your code in the future if say, you upgrade php on your server and the new version of php contains a new symbol that matches a literal index you have used.

You will mostly find this type of convention used in older scripts where the author(s) were shortcutting the process for expanding or not expanding array values in print() or echo'' strings.  It is definately not a good programming practice by today's standards however, so should be avoided.
Thurnok
The one, the only!

JPDeni

Thanks so much for the info. I will definitely change my lazy ways.  :laugh:

This website is proudly hosted on Crocweb Cloud Website Hosting.