Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

why doesnt this work, stange!

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Giga
Smarty Rookie


Joined: 28 Oct 2005
Posts: 9

PostPosted: Mon Dec 12, 2005 7:09 pm    Post subject: why doesnt this work, stange! Reply with quote

I placed this in my index.php: require 'includes/header.php';

I made header.php look like this:

Code:
<?php

$template->assign(

"TF_BOT", $random_tf );

random_tf =('

$total = "2";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".gif";

// Change to the location of the folder containing the images
$image_folder = "./images/bots";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />"');


?>


This php scripts should add a random picture to the variable {$TF_BOT} on my header.tpl.

All I get is the this error:

Code:
Parse error: parse error, unexpected '=' in /var/www/html/transformers/includes/header.php on line 7


I can't make it work. So can someone help me?

Thanks!
Back to top
View user's profile Send private message MSN Messenger
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Tue Dec 13, 2005 11:10 am    Post subject: Reply with quote

Hi,

try writing cleaner code, and be aware that PHP usually runs top down, initializing variables and calling functions as they fly-by:
Error 1: you're using assing() to add $random_tf to your template, but it's defined after that.
If you want to assign first but define later, use assign_by_ref()
[php:1:89a3cac27e]// keep PHP happy by declaring an empty variable PRIOR its first usage
$random_tf = '';
// assign the (declared) variable by reference (see manual)
$template->assign_by_ref('TF_BOT', $random_tf);
// change the value of the "reference"
$random_tf = 'foo bar'; [/php:1:89a3cac27e]

Error 2: if you want to set a variable value, use a variable Smile
$random_tf = ('

Error 3: what's the purpose of the huge block in paranthesis: ('
....
')
Do you want to eval() that code in your Tempate??
Why not assign the final IMG code to your bot instead of injecting PHP code?
If you want to cache your template, and header.php will never be executed in that case, take a look at the {insert} function, or write a Smarty plugin -- though, I think {insert} is your fried, and will be easier to implement.
Back to top
View user's profile Send private message
Giga
Smarty Rookie


Joined: 28 Oct 2005
Posts: 9

PostPosted: Wed Dec 21, 2005 4:32 pm    Post subject: Reply with quote

Sorry for my late response.

I a new to php so I'm trying to make the best out of it!

<?php

// assign the (declared) variable by reference (see manual)
$template->assign_by_ref('TF_BOT', $random_tf);

// keep PHP happy by declaring an empty variable PRIOR its first usage
$random_tf = '';

// change the value of the "reference"
$random_tf = 'foo bar';

$total = "2";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".gif";

// Change to the location of the folder containing the images
$image_folder = "./images/bots";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";


?>


I got this error:
Code:
Fatal error: Call to a member function on a non-object in /var/www/html/cybertron/includes/header.php on line 4


What do I do? Thanks in advance!
Back to top
View user's profile Send private message MSN Messenger
thiebo
Smarty Pro


Joined: 16 Jan 2005
Posts: 144
Location: Paris

PostPosted: Wed Dec 21, 2005 5:43 pm    Post subject: Reply with quote

I think the error is here :

Code:
$template->assign_by_ref('TF_BOT', $random_tf);


which implies that you are using a class called $template (your way of saying $smarty, no problem with that) which contains a function assign_by_reference();

I have the impression you should take the manual from the very first pages again and implement smarty the way it is explained there, because in your code, you are using smarty functions without declaring your class. Youe should have something in the beginning for your code saying :

[php:1:7f9e48936d]<?php
require ('setup.php');
//this file contains the code calling smarty.class.php and creates your class see manual
$template = new template ;
//that is important and missing in your code.
$ boo = 'this is variable boo' ;
$template->assign ('boo', $boo);
$template->display('mytemplate.tpl');
?>
[/php:1:7f9e48936d]
Back to top
View user's profile Send private message Visit poster's website
Giga
Smarty Rookie


Joined: 28 Oct 2005
Posts: 9

PostPosted: Wed Dec 21, 2005 6:46 pm    Post subject: Reply with quote

I have, this is just the header witch I require in my index.php.
Back to top
View user's profile Send private message MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Dec 21, 2005 7:15 pm    Post subject: Reply with quote

Hi.

First off, this isn't a Smarty problem in any way but rather one concerning PHP usage. In PHP -- and other languages -- you must instance your objects before you use them. This is not a general PHP support forum so if you are having questions in this regard, you are probably better off to ask in a more appropriate place such as the php-general mailing list.
Back to top
View user's profile Send private message
Giga
Smarty Rookie


Joined: 28 Oct 2005
Posts: 9

PostPosted: Thu Dec 22, 2005 6:03 pm    Post subject: Reply with quote

Thanks but the support was quite good so far! Very Happy
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP