Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
#1 2007-11-27 20:44:54
- simsim
- Member
- Registered: 2006-05-06
- Posts: 70
Custom PHP syntax error
I’m assuming TXP allows PHP in forms since it already allowing PHP in pages, am I correct?
If so, I have a form that contains the following PHP code:
<txp:php>
if (<txp:comments_count /> == 1) {
echo 'Some text';
} else {
if (<txp:comments_count /> == 2) {
echo 'Another text';
}
} else {
if (<txp:comments_count /> > 2 AND <txp:comments_count /> <= 10) {
echo "<txp:comments_count /> comments";
} else {
if (<txp:comments_count /> > 10) {
echo "<txp:comments_count /> comments so farً";
}
}
</txp:php>
When executing the code I got the following error:
Parse error: syntax error, unexpected '<' in C:\Program Files\xampp\htdocs\project35\textpattern\publish\taghandlers.php(2738) : eval()'d code on line 2
Any clues what’s going wrong?
(Edit: updated discussion topic. -Mary)
Last edited by Mary (2007-11-28 21:55:54)
Offline
Re: Custom PHP syntax error
I think you can’t use TXP tags like you have in your script (if statements).
Offline
Re: Custom PHP syntax error
use comments_count()
instead of <txp:comments_count>
Offline
#4 2007-11-27 21:03:16
- simsim
- Member
- Registered: 2006-05-06
- Posts: 70
Re: Custom PHP syntax error
ruud wrote:
use
comments_count()
instead of<txp:comments_count>
Thanks, I did. Now I got the following error:
Parse error: syntax error, unexpected T_ELSE in C:\Program Files\xampp\htdocs\project35\textpattern\publish\taghandlers.php(2738) : eval()'d code on line 8
Last edited by simsim (2007-11-27 21:04:38)
Offline
Re: Custom PHP syntax error
Well, you can not just use plain txp-tags like <txp:comments_count />
inside PHP-scripts. You need to use the real functions and globals that are used by those tags. Secondly you are using else
wrongly, because you don’t even need those else
. Remove them and you’re good to go.
Cheers!
Last edited by Gocom (2007-11-27 21:06:42)
Offline
#6 2007-11-27 21:18:12
- simsim
- Member
- Registered: 2006-05-06
- Posts: 70
Re: Custom PHP syntax error
Thanks Gocom.
It seems to work fine now, however it “echo“s comments_count()
instead of the actual number of comments. I think PHP doesn’t output function values in echo
statements, correct? How should I fix this? And what was wrong with my else
statements?
Btw, you’ve posted an awesome piece of code but then removed it! Why? Spread out the knowledge mate ;)
Last edited by simsim (2007-11-27 21:19:07)
Offline
Re: Custom PHP syntax error
You don’t need it, ruud already told you what to do. But I will rewrite it.
I think PHP doesn’t output function values in echo statements, correct?
No, your absolutely wrong. They can be used, but must be seperated from another content, ie. mustn’t be inside quotes.
<txp:php>
echo (comments_count() == 1) ? 'Some text' : '';
echo (comments_count() == 2) ? 'Another text' : '';
echo (comments_count() > 2 AND comments_count() <= 10) ? comments_count().' comments' : '';
echo (comments_count() > 10) ? comments_count().' comments so far' : '';
</txp:php>
And what was wrong with my else statements?
You don’t need them. Conditional ain’t outputted if it ain’t true, which makes else useless. So you’re using them wrongly.
Cheers!
PS. In your code there is different charset (ASC-II) and others are UTF-8. I wonder why… So, that will also make it non-working ;)
Last edited by Gocom (2007-11-27 21:38:04)
Offline
#8 2007-11-27 22:04:52
- simsim
- Member
- Registered: 2006-05-06
- Posts: 70
Re: Custom PHP syntax error
I meant your code snippet was awesome in the sense of its crispiness not the functional sense. I did understand rudd’s post but maybe failed to remember that I should use concatenation with functions & not enclose them within quotes.
Thanks again.
Edit: Regarding the non-ASCII characters, you’re correct. Actually I’m designing an Arabic-language based website. The whole PHP code was to deal with the complex Arabic numeral system and I just replaced Arabic phrases with English ones so as not to bother posters with confusing details. :)
However, the form is perfectly generating the output I’m looking for now. Doesn’t TXP automatically deals with non-ASCII characters within its interface?
Last edited by simsim (2007-11-27 22:10:40)
Offline
Re: Custom PHP syntax error
However, the form is perfectly generating the output I’m looking for now. Doesn’t TXP automatically deals with non-ASCII characters within its interface?
Yes it can handle any charset, but you were using two seperated charset, that made me wonder. One quote were arabic-mark – and it won’t work neither ;)
Cheers!
Offline
#10 2007-11-27 23:00:14
- simsim
- Member
- Registered: 2006-05-06
- Posts: 70
Re: Custom PHP syntax error
Oh, yes. That minor quote did generate an error & I’ve fixed it at the time. :D
Offline
Pages: 1