Drupal 7 include php file error - php

l am working on a Drupal 7 site with a custom video block called a leaf. It allows the admin the ability to add Text, Photos and Video. When I add video, it functions properly, but I get the below error.
Strict warning: Only variables should be passed by reference in include() (line 27 of /var/www/vhosts/xxxxxxxxxxx.com/sites/default/themes/custom_theme/templates/views-view-field--leaf-block--nid.tpl.php)
I researched the error and came across similar instances, but the php is a bit over my head.
The code currently reads as:
<?php endif; ?>
<?php
$node_to_load = node_load($row->nid);
//line 27 below
print drupal_render(node_view($node_to_load));
?>
I believe that render needs a reference so as is it is invalid, but I am not sure how to accurately assign the return value or the correct syntax. My attempt is below, but is incorrect.
<?php
$node_to_load = node_load($row->nid);
{
nid++;
}
$reference=$something
node_to_load($reference);
// $something to reference????
?>
Does anyone have any suggestions or advise that would help me? This error doesn’t appear to interrupt the functionality of the videos once added but I would love to clean up the code if possible and understand the situation more.
Thanks,

try this:
<?php
$node_to_load = node_load($row->nid);
$node_view = node_view($node_to_load);
print drupal_render($node_view);
?>
As you can see in the docs, drupal_render uses a reference.

Related

php main file not seeing included file variable

OK most the answers on S.O. are BACKWARDS from what I am having an issue with. My variable is not seen FROM the included file. I have never ran into this, and it is messing everything up. I was using smarty, but since it cant see the var, I went back to old fashion php templating, and still cannot see the var.. here is the scripts in question (a gaming site ofc).
INCLUDED FILE: prep_feature_game.php
foreach($games->gamexml->game as $game){
$d = parseDate($game['releasedate']);
$game['date_month'] = $d['month'];
$game['date_day'] = $d['day'];
$game['date_year'] = $d['year'];
$game['image_60x40'] = showImage($game['foldername'],'60x40');
$game['genrelist'] = displayGenreList($game['gameid']);
//debugPrint($game);die();
$game_list[] = $game;
}
CONTROL PAGE:
switch ($page) {
default:
include('includes/prep_feature_game.php');
// NOTE $GAME_LIST HAS THE ARRAY ^
display('feature_game');
break;
AND THE DISPLAY FILE: FEATURE_GAME.PHP
<h1><?=SITE_NAME;?>'s Featured Games!</h1>
<table>
<?PHP
// TEST FOR $GAME_LIST
foreach($game_list as $field=>$data){
echo $data['gamename'];
}
/*
this produces the error:
Notice: Undefined variable: game_list in C:\xampp\htdocs\ix2\themes\missingpiece\feature_game.php on line 7
*/
}// end switch page
I have many variations over the years of these templates, and the "included" files have never produced this kind of error. I know that included files act as though the script contained is inserted where the include is at.
why is the receiving script unable to see the variable produced from the included script. These are all included, so it should not be an issue
PS
I noticed this started happening when I upgraded from php v5 - php v7, doubt it has anything to do with that, I havent checked that yet since include files are pretty standard.
Your question is not showing the definition of the display function, but unless you declare $game_list as a global within that function it is out of scope. If you include the template it'll work.
include('includes/prep_feature_game.php');
include('feature_game.php');
Or add parameters to your display function.
display('feature_game', $game_list);
Or if you're using smarty you must assign the variable first.
$smarty->assign('game_list', $game_list);
$smarty->display('feature_game.tpl');

First timer PHP edit to update html, some errors

this is my first time using PHP in a real project environment. The project is pretty simple, take an existing, working PHP site and update the HTML to be consistent with HTML5. After designing the HTML, I am inserting the PHP from the previous site. This works most of the time, but I get a few errors. For instance:
<?
$sec = $_GET['sec'];
if ($sec == "1") {
echo ('Success!');
}
?>
Is causing the error:
Notice: Undefined index: sec in /file_that_holds_site_build.
Of course that is only if the url doesn't include the append tag (=1) that alerts the message.
So the question is this, what am I missing that causes the $GET when there is no $sec? How do I eliminate this error on first page load?
You're getting that notice because you're trying to access an array index that doesn't exist in some scenarios. Here's how you should be getting the data out of the request.
$sec = array_key_exists('sec', $_GET) ? $_GET['sec'] : null;
Thanks to everyone who provided possible answers to this question. It was Daniel that came up with the easiest fix. Again, I am just adjusting someone else's code to work, so a universal solve would involve too much of my own writing. To the point, the final code looks like this:
<?
if (isset($_GET["sec"])){
$sec = $_GET['sec'];
if ($sec == "1") {
echo ('Success! Your username and password have been sent via email.');
}}
?>
Notice the added if statement. As I said in a comment to Daniel, SO SIMPLE!
Thanks again for everyone's help. I hope to be likewise of service to you all soon.
Simple just use isset($_GET['sec']) to check for the parameter 'sec' before using it in the php code. That should eliminate the error. This is quite trivial I suppose.
I often simply extract() the wohle $_GET super global and then either get the desired variable or not. As a kind of "declaration" I initialize each expected variable first with false. This way I find it much easier to handle than individually doing a check like if(isset($_GET['element'])) ...
$var1=$var2=$var3=false; // my desired variables
extract($_GET); // everything I get from the form
// or: extract($_REQUEST);
if ($var1) { /* do something with it */ }
Possible security risk:
Of course you should be aware that everybody could simply include their own variable as an argument to he page ...

Understanding PHP code includes

I am pretty new to PHP. I am going through a website and am trying to find a source of some of the content and have come across what looks like where the source is, but I cannot decipher the code. Can someone help me understand what this means?
<?php
if (is_file($office_file))
include($office_file);
echo do_shortcode(ob_get_clean());
?>
Thanks!
I have copied your code and added comments that (hopefully) explain what is going on.
<?php
if (is_file($office_file)) // check if $office_file is a valid file
include($office_file); // the check passes, so now include the contents of that file inline
echo do_shortcode(ob_get_clean()); // not enough context to know really what this does, but it looks like its printing the result of parsing 'shortcode' from an output buffer
?>

Linking to files in different directory in PHP

My other question about getting help with the programming side of wordpress was labeled off topic for some reason so I'm asking a different way. I'm trying to embed my wordpress posts. I'm using this tutorial:
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
The problem is with this code:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('./wordpress/wp-load.php');
query_posts('showposts=1');
?>`
When I try to run the page that this is inserted into, I get the error saying that file doesn't exist. Pretending my domain is blah.com, the file is in www.blah.com/wordpress/wp-load.php and the page that includes this PHP code is in www.blah.com/other/page.php.
How do I change the syntax of the link on the require line to make sure it's pointing to the right place since right now it doesn't seem to be working?
have you tried with
require('../wordpress/wp-load.php');
or anyway something like
require('../../wordpress/wp-load.php');
?
(depending on the depth of your file position)
Just like the above answer
This worked for me
require('../wordpress/wp-load.php');

RSS Parser for Twitter is Slow - why?

I am making use of this script to get the first item of my Twitter feed. However, it is slow (it takes 3 to 4 seconds to load page now). Why is it so slow?
Here is how I use it.
require_once 'rss_php.php'; //see link above
$rss = new rss_php;
$rss->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$feed = $rss->getItems(false, 1);
echo $feed[0]['title'];
echo $feed[1]['title'];
I do get this PHP notice:
Notice: Undefined variable: tempNode
in
C:\wamp\www\rss_php.php
on line 137
I don't know why since this works, line 137 is this line:
return $tempNode;
Thanks all for any help. I appreciate any advice on speeding this up.
Fetching content from a remote location can potentially introduce some rather ugly loading issues.
Try saving the contents of the RSS feed in a local file and see if the problem persists when loading from a local drive.
If this fixes the issue, you should look into caching the contents of the feed every once in a while.
Firstly, line 110 of your pastbin is assigning a variable that was never declared. As such, any requests or assignments to an undeclared variable will do this. From what I see it should be as simple as adding $tempNode = Array(); just below the function call of the extractDOM method.
Next, since this is a script from someone else I would recommend asking them what you can do to improve performance. From what is in the pastbin I don't see anything elaborate going on, nor do I see you using the library incorrectly, but ultimately they would know better.
After line 138 in rss_php.php (v.1 Free version) file paste this:
...
if (!isset($tempNode)){
$tempNode = null;
}
return $tempNode;
...
Enjoy
;)
Getting rid of that return $tempNode; notice is easy, but not your problem, it just needs to be defined out of the forloop in that extractDOM function.
Optimizing your php code is a big task. I'm assuming that the api call is the majority of the time, but if you want to try and speed your code up a bit I would look into tutorials on how to do that:
http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
http://progtuts.info/55/php-optimization-tips/
http://hungred.com/useful-information/php-micro-optimization-tips/

Categories