[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Adding a function to php code

There are two sets of coding in the Code window. The Old Coding works, the New Coding doesn't - it gives a blank screen.

Can anyone tell me why?

New coding:
<?php
// $Id: semanticviews-view-unformatted.tpl.php,v 1.1.2.3 2009/09/19 22:33:48 bangpound Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php
function convert_title($old_title){ 
  
$month = date('M',$old_title);

switch($month)
{
    case "Jan": $month="Jan-Feb"; break;
    case "Feb": $month="Jan-Feb"; break;
    case "Mar": $month="Mar-Apl"; break;
    case "Apr": $month="Mar-Apl"; break;
    case "May": $month="May-Jun"; break;
    case "Jul": $month="Jul-Aug"; break;
    case "Aug": $month="May-Jun"; break;
    case "Sep": $month="Sep-Oct"; break;
    case "Oct": $month="Sep-Oct"; break;
    case "Nov": $month="Nov-Dec"; break;
    case "Dec": $month="Nov-Dec"; break;
    default: $month = "None"; break;
}

$new_title = $month.' '.date('Y',$old_title);
 
  return ($new_title); 
} 



?>
<?php if (!empty($title)): ?>
$title = convert_title($title);
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print $title; ?>
  </<?php print $group_element; ?>>
  <br>
<?php endif; ?>
<?php if (!empty($list_element)): ?>
  <<?php print $list_element; ?><?php print drupal_attributes($list_attributes); ?>>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!empty($row_element)): ?>
  <<?php print $row_element; ?><?php print drupal_attributes($row_attributes[$id]); ?>>
  <?php endif; ?>
    <?php print $row; ?>
  <?php if (!empty($row_element)): ?>
  </<?php print $row_element; ?>>
  <?php endif; ?>
<?php endforeach; ?>
<br><br>
<?php if (!empty($list_element)): ?>
  </<?php print $list_element; ?>>
<?php endif; ?>


Old coding:
<?php
// $Id: semanticviews-view-unformatted.tpl.php,v 1.1.2.3 2009/09/19 22:33:48 bangpound Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>

<?php if (!empty($title)): ?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print $title; ?>
  </<?php print $group_element; ?>>
  <br>
<?php endif; ?>
<?php if (!empty($list_element)): ?>
  <<?php print $list_element; ?><?php print drupal_attributes($list_attributes); ?>>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!empty($row_element)): ?>
  <<?php print $row_element; ?><?php print drupal_attributes($row_attributes[$id]); ?>>
  <?php endif; ?>
    <?php print $row; ?>
  <?php if (!empty($row_element)): ?>
  </<?php print $row_element; ?>>
  <?php endif; ?>
<?php endforeach; ?>
<br><br>
<?php if (!empty($list_element)): ?>
  </<?php print $list_element; ?>>
<?php endif; ?>

Open in new window

0
bogorman
Asked:
bogorman
  • 8
  • 8
  • 4
1 Solution
 
RobinSoftware EngineerCommented:
Should the line

$title = convert_title($title);

not be :

<?php
   $title = convert_title($title);
?>
0
 
bogormanAuthor Commented:
Thanks for your suggestion. I am sure you are correct but it still gives a blank page.
I have even replaced the upper part of the coding with:
<?php
// $Id: semanticviews-view-unformatted.tpl.php,v 1.1.2.3 2009/09/19 22:33:48 bangpound Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php
function convert_title($old_title){
 

  return ($old_title);
}



?>
<?php if (!empty($title)): ?>
<?php
   $title = convert_title($title);
?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print $title; ?>
  </<?php print $group_element; ?>>

....

and it still doesn't work.
0
 
RobinSoftware EngineerCommented:
I see it's a template. Does it allow to define functions in it?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Beverley PortlockCommented:
Getting a blank screen is usually an indication that error reporting is disabled and so you do not see the error message which causes the script to halt. Add these lines to the start of the script just after the very first <?php

ini_set('display_errors',1);
error_reporting(E_ALL);

You many see a lot of messages that start with something like "Notice: ..... error message". Notices are not that worrying and will not usually cause a script to stop executing. Look fir anything starting with ERROR or FATAL
0
 
Beverley PortlockCommented:
When I run the fragment given, it does not like the foreach on line 50 of the fragment

<?php foreach ($rows as $id => $row): ?>

because "$rows" is not an array value. In fact, it is not defined at all in the fragment so unless it is defined elsewhere then that will need to be corrected and "$rows" created and populated.
0
 
bogormanAuthor Commented:
Thanks, both of you, for your help so far.
Yes, robinu, you are right - it is a drupal template and I am now wondering if it is because I am trying to define a function in it.
Anyway, I have followed your suggestion, bportlock, and have enabled error reporting. By the way, I think $rows must be defined elsewhere as I have not altered that part of the coding.
I now get the error:
Fatal error: Cannot redeclare convert_title() (previously declared in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php:14) in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php on line 37
Perhaps it is due to trying to use a function in this file?
0
 
RobinSoftware EngineerCommented:
It seems that functions also exists in semanticviews-view-unformatted--magazine-articles--default.tpl.php

Can you rename either one, so the conflict is solved?
0
 
Beverley PortlockCommented:
If the two functions are absolutely identical then comment one of them out between /* and */

If they are different then you need to decide which one is correct and remove the other or, as bogorman says rename one of them.
0
 
Beverley PortlockCommented:
Oops! I meant to say "If they are different then you need to decide which one is correct and remove the other or, as robinu says rename one of them."
0
 
bogormanAuthor Commented:
Strange thing is that the redeclaration is stated to occur in the same file! Had thought for a moment that I might have repeated the function in it but I haven't (see attached coding  - the whole of Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

// $Id: semanticviews-view-unformatted.tpl.php,v 1.1.2.3 2009/09/19 22:33:48 bangpound Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php
function convert_title($old_title){ 
  
$month = date('M',strtotime($old_title));

switch($month)
{
    case "Jan": $month="Jan-Feb"; break;
    case "Feb": $month="Jan-Feb"; break;
    case "Mar": $month="Mar-Apl"; break;
    case "Apr": $month="Mar-Apl"; break;
    case "May": $month="May-Jun"; break;
    case "Jul": $month="Jul-Aug"; break;
    case "Aug": $month="May-Jun"; break;
    case "Sep": $month="Sep-Oct"; break;
    case "Oct": $month="Sep-Oct"; break;
    case "Nov": $month="Nov-Dec"; break;
    case "Dec": $month="Nov-Dec"; break;
    default: $month = "None"; break;
}

$new_title = $month.' '.date('Y',strtotime($old_title));
 
  return ($new_title); 
} 



?>
<?php if (!empty($title)): ?>
<?php
   $title = convert_title($title);
?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print $title; ?>
  </<?php print $group_element; ?>>
  <br>
<?php endif; ?>
<?php if (!empty($list_element)): ?>
  <<?php print $list_element; ?><?php print drupal_attributes($list_attributes); ?>>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!empty($row_element)): ?>
  <<?php print $row_element; ?><?php print drupal_attributes($row_attributes[$id]); ?>>
  <?php endif; ?>
    <?php print $row; ?>
  <?php if (!empty($row_element)): ?>
  </<?php print $row_element; ?>>
  <?php endif; ?>
<?php endforeach; ?>
<br><br>
<?php if (!empty($list_element)): ?>
  </<?php print $list_element; ?>>
<?php endif; ?>

Open in new window

0
 
RobinSoftware EngineerCommented:
Does it give any errors if you don't call the function, but leave the function in it?
0
 
bogormanAuthor Commented:
Yes.

Fatal error: Cannot redeclare convert_title() (previously declared in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php:14) in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php  on line 37
0
 
RobinSoftware EngineerCommented:
The file you edit is

semanticviews-view-unformatted.tpl.php

, but it is complaining about

semanticviews-view-unformatted--magazine-articles--default.tpl.php

These are different files. Could you check for this?
0
 
bogormanAuthor Commented:
Hi robinu,
I think you are mistaken.
As I understand it in Drupal if you want to overwrite a theme file you copy it to your theme directory and rename it using one of the suggested names in the views Theming Information section. The name I have used is semanticviews-view-unformatted--magazine-articles--default.tpl.php
You can then modify it to achieve the result you want.
I am wondering if your original suggestion is correct and you are not allowed to define a function in this file?
The other idea I had was to use:

 <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    $date_range_string = "Jan-Feb Mar-Apl May-June Jul-Aug Sep-Oct Nov-Dec";
    $month_number=date("m",$title);
    $title=substr($title,(9*$month_number)-1,8).date("y",$title);  //should have the form May-Jun 2008
    <?php print $title; ?>
  </<?php print $group_element; ?>>

Haven't tried it yet.


0
 
RobinSoftware EngineerCommented:
Could you try that suggestion then. We will know if defining a function is allowed (I don't know)..
0
 
bogormanAuthor Commented:
Have changed the coding to:

<?php if (!empty($title)): ?>
   <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php
    $date_range_string = "Jan-Feb Mar-Apl May-June Jul-Aug Sep-Oct Nov-Dec";
    $month_number=date("m",strtotime($title));
    $title=substr($title,(9*$month_number)-1,8).date("y",$title);  //should have the form May-Jun 2008
    ?>
    <?php print $title; ?>
  </<?php print $group_element; ?>>
  <br>
<?php endif; ?>


 I get the errors:


# warning: date() expects parameter 2 to be long, string given in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php on line 19.
# warning: date() expects parameter 2 to be long, string given in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php on line 19.
# warning: date() expects parameter 2 to be long, string given in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php on line 19.
# warning: date() expects parameter 2 to be long, string given in /Applications/MAMP/htdocs/drupal/sites/all/themes/faith_home/semanticviews-view-unformatted--magazine-articles--default.tpl.php on line 19.
# warning: date() expects parameter 2 to be long, string given in /Applications/MAMP/htdocs/drupal/sites/all/themes

.....



I suspect that the $title variable holds the date as a string. It appears as (e.g.)
 
01/11/2010

(for 1st Nov 2010 - European format) before I modify the coding.

What function should I use to convert $title into a long date (if I am right that this is what is required)


0
 
RobinSoftware EngineerCommented:
Why don't you use

$dp=explode("/",$title);

and $dp[0] will be the day,
$dp[1] the month, and $dp[2] the year..
0
 
bogormanAuthor Commented:
Thanks so much, robinu. You have solved it.

I now use:

$dp=explode("/",$title);
 $date_range_string = "Jan-Feb Mar-Apl May-Jun Jul-Aug Sep-Oct Nov-Dec ";
    $month_number=$dp[1];
    $title=substr($date_range_string,(4*$month_number)-4,8).substr($dp[2],0,4);  //should have the form May-Jun 2008
 
?>  

For some reason $dp[2] contains (e.g.)  2009< so I have to use substr on it. Do you happen to know why?

Again, thanks.
0
 
bogormanAuthor Commented:
Thanks again. You solved a problem which has puzzled me for weeks!
Brian
0
 
RobinSoftware EngineerCommented:
Than the title contains more than just the date. The explode function seperates it on the / character.
Substr will fix it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now