Web Hosting Talk







View Full Version : Can you help me figure out this one line of PHP code?


kreativ
11-14-2003, 01:30 PM
I need a little help deciphering one line of PHP code:
$prune_all = ($prune_all) ? '' : 'AND t.topic_vote = 0 AND t.topic_type <> ' . POST_ANNOUNCE;
This is the line of code that tells the prune function in phpBB to exclude polls and announcements from being pruned.

My friend, however, wants to include polls in the prune function, and exclude stickies as well as announcements in the prune function.

Removing "AND t.topic_vote = 0" does the trick for including polls in the prune function:
$prune_all = ($prune_all) ? '' : 'AND t.topic_type <> ' . POST_ANNOUNCE;
But I don't know the proper syntax to add stickies (POST_STICKY) to this line to have it excluded from the prune function.

Can anyone help?

Burhan
11-14-2003, 02:39 PM
Try



$prune_all = ($prune_all) ? '' : 'AND t.topic_type <> ' . POST_ANNOUNCE. 'AND t.topic_type = '. POST_STICKY;



The Ternary Operator (http://www.php.net/manual/en/language.operators.comparison.php) works like this :



(condition) ? true : false;



If (condition) is true, do the stuff after the ? else to the stuff after the :

kreativ
11-15-2003, 11:00 PM
Thanks fyrestrtr :)

Mind answering some noobie learning questions?

$prune_all = ($prune_all) ? '' : 'AND t.topic_type <> ' . POST_ANNOUNCE. 'AND t.topic_type ='. POST_STICKY;

Why is <> used for one while = for the other?
What purpose do the dots (.) serve?
Any idea why a condition was used if the "true" field is empty?

digitok
11-16-2003, 02:58 AM
It just means if $prune_all holds a value set it to nothing ('') else set it to whats after the :

<> means NOT equal to in MySQL, = means equal to

. is used for concatination , eg.

$var = 'hi';
echo 'test '.$var.' test';

This would echo test hi test

Hope this helps.

kreativ
11-17-2003, 01:52 AM
Thanks for the explanation digitok. :)

Since he wants to prune all topics except for Announcements and Stickies, shouldn't the code be like this then?
$prune_all = ($prune_all) ? '' : 'AND t.topic_type <> ' . POST_ANNOUNCE. 'AND t.topic_type <>'. POST_STICKY.;
Would this work as well?
$prune_all = ($prune_all) ? '' : 'AND t.topic_type <> ' . POST_ANNOUNCE. OR .POST_STICKY;

digitok
11-17-2003, 04:28 AM
Try this...

$prune_all = ($prune_all) ? '' : 'AND t.topic_type NOT IN('.POST_ANNOUNCE.','.POST_STICKY.')';

kreativ
11-20-2003, 01:02 PM
digitok,

Works great! Thanks :)