TinyPortal

Development => Resources => Topic started by: handoyo on December 08, 2009, 03:44:19 PM

Title: Custom php
Post by: handoyo on December 08, 2009, 03:44:19 PM
Hi all,i'm new to tiny portal..I'm wondering suppose for eample i want to display random words of the day on the left block.What should i do?I know a little php mysql..Where can i start?Thanks a lot guys...
Title: Re: Custom php
Post by: Lesmond on December 08, 2009, 03:58:58 PM
Welcome to Tinyportal handoyo

You could search for "Random Word Generator"
Title: Re: Custom php
Post by: handoyo on December 08, 2009, 04:22:57 PM
Is it on http://www.tinyportal.net/index.php?topic=23876.msg192556#msg192556 ? Should i just paste the code on the php block?Thanks Lesmond
Title: Re: Custom php
Post by: Lesmond on December 08, 2009, 04:32:02 PM
yeah thats the one, php block is correct, there is about 3 versions in that topic :)
Title: Re: Custom php
Post by: handoyo on December 08, 2009, 04:39:58 PM
Ok,by the way,what if i'm intending to use database to hold the data an get the words from it?Are there any tutorial about it with tp/smf?Thanks...
Title: Re: Custom php
Post by: Lesmond on December 08, 2009, 04:42:57 PM
Will have to wait for someone else to answer that one, me and databases dont get on   :o :tickedoff:
Title: Re: Custom php
Post by: handoyo on December 08, 2009, 04:55:53 PM
Ok,thanks Lesmond.God Bless You..  ;D
Title: Re: Custom php
Post by: JPDeni on December 08, 2009, 05:37:35 PM
You'd have to give more information about how you want your database set up to be. But it would be easier to just keep it in the code if you're just putting out a random word.

If you want to keep the same word for every person for a whole day, that's a whole different concept than displaying a random word. So I need you to fully explain what you want to do before I write any new code.
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 03:09:43 AM
Hi JPDeni,actually i'm intending to put random bible verse.. The database structre should be like i.e.

id     name       verse                                          text
1     matthew   6:10    Thy kingdom come, Thy will be done in earth, as it is in heaven.

It will be shown once in a day and shown for every person....On the next day,it will be radomized again..Thanks...


Title: Re: Custom php
Post by: JPDeni on December 09, 2009, 03:28:02 AM
Okay. I think I can do that. It would be easier if you just set which verse would be the verse of the day or if it was a new random one every time the page is reloaded, but I can probably find a place to store what the random verse is for the day. I'll take a look at where the best place would be to store it.
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 03:34:55 AM
Thanks a lot JPDeni...May God bless you for your kindness  :D
Title: Re: Custom php
Post by: Lesmond on December 09, 2009, 03:38:22 AM
wait, dosnt Crip do something like this on his site, bit late here in the UK erm 3.40 am
Title: Re: Custom php
Post by: Lesmond on December 09, 2009, 03:55:31 AM
handoyo try a search for "bible" here on tp
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 04:10:35 AM
Ok Lesmond,thanks.
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 04:25:13 AM
HI Lesmond,i've serach it here,if i'm not wrong,Crip create the bible search i suppose.Not random verse..Thanks...
Title: Re: Custom php
Post by: JPDeni on December 09, 2009, 05:10:16 AM
In case you still need it....

You need to set up your verses table just the way you described it earlier and name it verses. Be sure that your field names are the same as you indicated before, with the same lack of capitalization -- id, name, verse, text -- because that's how I've created the code.

Use this in a php block or php article:


global $db_prefix, $modSettings;

if ((date('z',$modSettings['verseDate']) <> date('z',time())) OR !isset($modSettings['verseDate'])) {
$result = db_query("
SELECT *
FROM verses
ORDER BY RAND()
LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
$modSettings['verseDisplay'] = '<b>' . $row['text'] . '</b> ' . ucfirst($row['name']) . ' ' . $row['verse'];
mysql_free_result($result);
$date = time();
$verse = $modSettings['verseDisplay'];
db_query("REPLACE INTO {$db_prefix}settings (variable, value)
VALUES ('verseDate','$date')", __FILE__, __LINE__);
db_query("REPLACE INTO {$db_prefix}settings (variable, value)
VALUES ('verseDisplay','$verse')", __FILE__, __LINE__);
}
echo $modSettings['verseDisplay'];


If you want the format any different, I can alter it for you, but I'll need to know what you want.
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 05:24:48 AM
Thanks JPDeni,but before that,how should i handle the database?Should i just enough by creating the verses table?Thanks..
Title: Re: Custom php
Post by: JPDeni on December 09, 2009, 05:39:08 AM
Quote
Should i just enough by creating the verses table?

I'm not sure I understand what you mean. I thought you had already done that. Isn't the database table already made?
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 05:53:13 AM
Not yet..Suppose i got the database table made,all i have to do is put the code that you gave me on the php block,don't i?And i don't have to modify or create a new php file to open the database connection,isn't it?Thanks...
Title: Re: Custom php
Post by: JPDeni on December 09, 2009, 06:30:00 AM
QuoteSuppose i got the database table made,all i have to do is put the code that you gave me on the php block,don't i?

Yes. Assuming that the table is called verses and the fields are set up like you said they will be -- and you put the table into the same database where the SMF and TP tables are -- you will just need to put the code into a php block and it should read the table just fine. I made a table (with just two verses in it) to test it out and it worked great.
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 06:42:51 AM
Great,thanks a lot JPDeni.. :D
Title: Re: Custom php
Post by: handoyo on December 09, 2009, 09:16:24 AM
I've tested it JPDeni...It works the way i want,by the way.What if i'm trying to use time instead of date.For example after 6 hours,the verse will change to another..Thanks..
Title: Re: Custom php
Post by: JPDeni on December 09, 2009, 04:06:47 PM
If someone else wants to work on that, fine. But it's way too much work for me to do.
Title: Re: Custom php
Post by: handoyo on December 10, 2009, 02:41:43 AM
Ok,thanks a lot by the way for the code that you gave to me.God bless you.. :)