• Welcome to Simple Machines Community Forum. Please login or sign up.
December 06, 2021, 05:11:37 AM

News:

Follow SMF on Twitter.


Table From CSV

Started by Anguz, August 19, 2007, 05:16:11 PM

Previous topic - Next topic

Anguz

Hi guys,

I need help with the new bbc parsing function. I haven't touched this code since the old version.

I want to update my Table From CSV tag mod and can't figure something out.

I chose to use type unparsed_equals because the format that the tag uses is

[table=,]
a,b,c
1,2,3
[/table]


I found that $data is the value after the = sign, but what variable has what's between the tags?

I can't use the before and after to enclose the content because I need to give an id to every table generated to make it unique for the javascript that lets you sort the tables clicking on the headers.

This is what I have so far

<?php
array(
'tag' => 'table',
'type' => 'unparsed_equals',
'validate' => create_function('&$tag, &$data, $disabled''
static $table_id = 1;
$ths = $tds = \'\';
$comma = empty($data) ? \',\' : $1;
$table = trim(strtr($table, array(\'<br />\' => "\n")));
if (strpos($table, "\n") !== false)
{
$table = explode("\n", $table);
$ths = \'<tr><th>\' . strtr($table[0], array($comma => \'</th><th>\')) . \'</th></tr>\';
unset($table[0]);
$table = implode(\'</td></tr><tr><td>\', $table);
}
$tds = \'<tr><td valign="top">\' . strtr($table, array($comma => \'</td><td valign="top">\')) . \'</td></tr>\';
$table = \'<table id="table\' . $table_id++ . \'"\' . (!empty($ths) ? \' class="sortable"\' : \'\') . \' style="font: inherit; color: inherit;">\' . $ths . $tds . \'</table>\';'
),
'block_level' => true,
),


Of course, $table is where I need to use the comma separated values between the tags.

Thanks in advance for any help you can give.
Cristián Lávaque http://cristianlavaque.com

imrie

this kinda a guess but if you look at:

$tds = \'<tr><td valign="top">\' . strtr($table, array($comma => \'</td><td valign="top">\')) . \'</td></tr>\';


look between the td's and you will see the value $table i think it is being used on the lower line to define the setup of the table. So by the process of elimnation i think it would be $comma

winrules

I'm glad to see you're updating your mods :)

I'm pretty sure you want to use the "unparsed_equals_content" type. If you use that $data[0] should be the data in between the tags and $data[1] should be the "equals" value.


winrules
SMF Developer
               
My Mods
Please do not PM me for support.


karlbenson

August 19, 2007, 10:51:16 PM #3 Last Edit: August 19, 2007, 10:53:20 PM by karlbenson
Always helps me to visualise.

[table= $data[1] , $data[2] ] $data[0] [/table]


Anguz

Thank you guys, you're a lot of help.

I toyed with it a bit now and I get the stuff into the function now. The result is not showing in the message yet, though, but I'll keep working on it tomorrow.
Cristián Lávaque http://cristianlavaque.com

Anguz

Found I needed to set 'content' for that type of tag. Working now :).

In order to parse the content of the table I ended up using the parse_bbc function inside the validate code. Not very clean to use that tag type and then do this, but it worked. What would be a better tag type to do it?

<?php
// Table From CSV
$codes[] = array(
'tag' => 'table',
'type' => 'unparsed_equals_content',
'validate' => create_function('&$tag, &$data, $disabled''
static $csvtable_id = 1;
$ths = $tds = \'\';
if (empty($data[1]))
$data[1] = \',\';
$data[0] = trim(strtr(parse_bbc($data[0]), array(\'<br />\' => "\n")));
if (strpos($data[0], "\n") !== false)
{
$data[0] = explode("\n", $data[0]);
$ths = \'<tr><th>\' . strtr($data[0][0], array($data[1] => \'</th><th>\')) . \'</th></tr>\';
unset($data[0][0]);
$data[0] = implode(\'</td></tr><tr><td>\', $data[0]);
}
$tds = \'<tr><td>\' . strtr($data[0], array($data[1] => \'</td><td>\')) . \'</td></tr>\';
$data[0] = \'<table id="csvtable\' . $csvtable_id++ . \'" class="csvtable\' . (!empty($ths) ? \' sortable\' : \'\') . \'">\' . $ths . $tds . \'</table>\';'
),
'content' => '$1',
'block_level' => true,
);
Cristián Lávaque http://cristianlavaque.com

Advertisement: