Thottbot > Forums > Thottbot > Powered by Thottbot > Create Thott...
  NEW! Project Lore - Soloing - Dorkins Does Dailies Episode 25 - The Wyrmrest Accord - Part 3
  Report  Quote Reply Create Thott links dynamically
Score 15     Vote: [-] [+] by Magnamus_eu, 1.8 years ago
Hi,

I love the Thot links, which work great (ignoring minor js errors ofc!)
However, what I want to do is create links dynamically on my guild forum when people enter items a bit like they can in this forum, or with ItemStats which I find unreliable, e.g.
[ item ]Hammer of Butt Kicking[ /item ].
For this I need to query the Thott database to get the item id, so I can construct the link correctly. Is there a webservice type of thingy I can use for this?
If successful I will share the code.

Thanks...
  Report  Quote Reply Re: Create Thott links dynamically
Score 7.7     Vote: [-] [+] by herenvardo, 1.7 years ago
I'm using links in the form http://www.thottbot.com/?i=Dawnstone on a dinamyc page, and I've noticed that the Powered by Thottbot script doesn't work with them. I'm posting this here beacause:
1) If the script is upgraded to support these links, Magnamus_eu's will be able to use this kind of links on the forums.
or 2) If a mechanism to query item's id is posted in answer to Magnamus_eu's request, I could use it on the code that generates my pages to use id-based links.

So, despite being slightly different issues, any answer to either would actually ansert both of our issues.
  Report  Quote Reply Re: Create Thott links dynamically
Score 7.8     Vote: [-] [+] by Risue, 1.7 years ago
Dunno how various boards work, but here's my code that I made to search and store the ID codes and quality of items (in PHP.) Anyone tech savvy should be able to figure out how to convert it. Note that "$innards" contains the value that was given inside the tag. So if someone put in [tag]Primal Mana[/tag], then $innards should be "Primal Mana". You'll have to figure out how to declare a new tag on your board and how to get its contents. And of course you'll need to figure out how to add the thottbot script to the page.

$item_code = -1;
$quality = 0;

$db = get_db();
$db->prepare("check_thott", "SELECT code, quality FROM thott_items WHERE item_name=$1");
$db->executeQuery("check_thott", Array($innards));

if ($db->rowCount() > 0) {
$item_code = $db->result(0, "code");
$quality = $db->result(0, "quality");
}
else {
$ch = curl_init("http://thottbot.com/?s=" . urlencode($innards));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$page = curl_exec($ch);

if (curl_errno($ch) != 0) { // Thottbot errored
curl_close($ch);
}
else {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ( // We were redirected to the specific item page
$http_code == 302
&& preg_match("/.*http:\/\/thottbot.com\/i(\d+).*/", $page, $matches) !=0
) {
$item_code = $matches[1];

$ch = curl_init("http://thottbot.com/i" . $item_code);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$page = curl_exec($ch);

if (curl_errno($ch) != 0) { // Thottbot errored
curl_close($ch);
}
else {
curl_close($ch);

if (preg_match("/.*quality(\d).*/", $page, $matches) != 0) {
$quality = $matches[1];
$db->prepare("insert_thott", "INSERT INTO thott_items (item_name, code, quality) VALUES ($1, $2, $3)");
$db->executeCommand("insert_thott", Array($innards, $item_code, $quality));
}
}
}
else {
$res = preg_match("/.*<span class=quality(\d)><a href='i(\d+)'>" . $innards . "<\/a><\/span>.*/", $page, $matches);
if ($res == 0) {
if (preg_match("/.*No search results found.*/", $page, $matches) != 0) {
$db->prepare("insert_thott", "INSERT INTO thott_items (item_name, code, quality) VALUES ($1, $2, $3)");
$db->executeCommand("insert_thott", Array($innards, "-1", 0));
}
}
else {
$quality = $matches[1];
$item_code = $matches[2];
$db->prepare("insert_thott", "INSERT INTO thott_items (item_name, code, quality) VALUES ($1, $2, $3)");
$db->executeCommand("insert_thott", Array($innards, $item_code, $quality));
}
}
}
}

if ($item_code == -1) {
$self = new HTMLObject();
$self->setTag("span");
$self->setStyle("text-decoration: underline;");
$self->set($innards);
}
else {
$self = new Link(
"http://thottbot.com/i" . $item_code,
"[" . htmlspecialchars($innards, ENT_QUOTES, "UTF-8") . "]"
);
$self->setClass("quality" . $quality);
}

---------
And here are my class definitions:

a.quality0{
color: grey;
text-decoration: none;
font-weight: bold;
}
a.quality1{
color: black;
text-decoration: none;
font-weight: bold;
}
a.quality2{
color: green;
text-decoration: none;
font-weight: bold;
}
a.quality3{
color: blue;
text-decoration: none;
font-weight: bold;
}
a.quality4{
color: purple;
text-decoration: none;
font-weight: bold;
}
a.quality5{
color: orange;
text-decoration: none;
font-weight: bold;
}
a.quality6{
color: red;
text-decoration: none;
font-weight: bold;
}
  Report  Quote Reply Re: Create Thott links dynamically
Score 7.9     Vote: [-] [+] by Risue, 1.7 years ago
Forgot...

And of course you need to add a table to your database to store the saved data. Mine is defined like so (PostgreSQL):

CREATE TABLE thott_items (
item_name varchar(50) PRIMARY KEY,
code varchar(10) UNIQUE,
quality int
)
  Report  Quote Reply Re: Create Thott links dynamically
Score 9.6     Vote: [-] [+] by Affe83, 11.7 months ago
Is there a better way to do this?  Perhaps there is a way to do this with javascript or some other method that doesn't require building my own database?

BackTop
 
Created by Thott - Feedback - Thottbot 3.71 /