Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-03-09 13:56:08

lindabb
Member
Registered: 2023-02-17
Posts: 111

basic data grid plugin

I learned the plugin in few hours with help from good people in this forum.

here is my humble dynamic data grid, just pass any query, allowedit, allowdelete .
user can customize the link to point to any page,
Someone with better knowledge can improve it to be more useful.

The call


<txp:my_datagrid query="select id, item_name, item_price , description from products limit 50",  allowedit=true,  allowdelete=true />

<--plugin start -->

<?php

\Txp::get('\Textpattern\Tag\Registry')
   ->register('my_datagrid'); 

function my_datagrid($atts, $thing = null)
{
    extract(lAtts(array(
        'query' => '',
        'allowedit' => true,
        'allowdelete' => true,

    ), $atts));

       $siteurl = parse('<txp:site_url />');

       $rs = safe_query($query);
       $colNames = array_keys($rs->fetch_assoc());


echo '<br><br><br><div class="container"> <br><br><br><br><table class="table table-hover">';
echo '<thead>';
echo '<tr>';

foreach(array_keys($rs->fetch_assoc() ) as $paramName)
      echo '<th>'.  $paramName .'</th>' ;

echo    ' <th>Action</th>';

echo '</tr>';   
echo '      </thead>';
echo '      <tbody>';

 foreach ($rs as $row) 
   {
    echo '<tr>';
    foreach ($row as $item) {
      echo "<td>{$item}</td>";
     }

   echo    ' <td>';

          if ($allowedit)
          {
              echo'<span><a href="'.$siteurl .'"><img style=" padding-right: 15px;" src="images/edit.png" ></a></span> ';
           }

          if ($allowdelete)
          {
            echo' <span><a href="'.$siteurl .'"><img src="images/cancel.png" ></a></span>';
           }

   echo ' </td>';

    echo '</tr>';



  }

echo'  </tbody>';
echo'    </table>';
echo '</div>';

    return ;    
}

<--plugin end -->

Offline

#2 2023-03-09 14:13:13

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: basic data grid plugin

Nice. If all you want is the ability to query the database and control its output, there are a few plugins you might like to investigate:

smd_query: build either simple or complex queries and pass the results through a form to format the data. Tries to sanitise the data as much as possible but care is needed.

etc_query: the bigger brother to smd_query which uses xpath to extract stuff and display it. Phenomenally powerful.

smd_xml: if you want to grab external data in XML format, slice and dice it and display stuff through Textpattern forms, this can do it. Might need updating as it’s been a while since I checked it. But you can also read the XML data in and then shove it through either of the above plugins to insert it into the Textpattern database. Sometimes very handy for translating content from one system to another.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2023-03-10 12:58:17

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: basic data grid plugin

Your plugin is straightforward to use, but not to customize (needs editing plugins code), though some customization (like wraptag and class) can be provided by txp core:

<txp:my_datagrid ... wraptag="div" class="container" />

And yes, we have few data-querying public-side plugins. For example, in etc_query it would be

<!-- retrieve and save data -->
<txp:etc_query data="SELECT * FROM txp_users" save="users" />
<table>
<!-- table head -->
<txp:etc_query data="users" markup="data" limit="1" wraptag="thead" break="tr">
    {*?=<th>{#}</th>}<th>Actions</th>
</txp:etc_query>
<!-- table body -->
<txp:etc_query data="users" markup="data" wraptag="tbody" break="tr">
    {*?=<td>{?}</td>}<td>Edit | Delete</td>
</txp:etc_query>
</table>

Offline

#4 2023-03-10 18:51:53

lindabb
Member
Registered: 2023-02-17
Posts: 111

Re: basic data grid plugin

Thank you both,
Yes just simple fixed table, I use that table in many places (without edit or delete) , so I had to do find or do something to make it easy for me by passing simple query to build table. I posted it here if anyone like me looking for basic table.
Thank you again

Offline

Board footer

Powered by FluxBB