Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Can Grav respond to Posts

Started by David Lannan 9 years ago · 4 replies · 752 views
9 years ago

Im using this brilliant table component on another website using a very simple site.
http://bootstrap-table.wenzhixin.net.cn/
It allows the use of a data source using php posts and gets to modify and fetch the data.
Can this be done with Grav? For example: how could I setup Grav to respond with a php response like:

PHP
    echo '<tbody>';
    while ($row = mysqli_fetch_array($tableResult)) { 
        echo "<tr>
            <td>" . $row[0] . "</td>
            <td>" . $row[1] . "</td>
            <td>" . $row[2] . "</td>
            <td>" . $row[3] . "</td>
            <td>" . $row[4] . "</td>
          </tr>"; 
    echo '</tbody>';
    }
    mysqli_close($db_con);

(There is other setup data too)
When the request is a simple: <domain>/gettabledata

I have tried a plugin using routes, but it only returns the current uri for the page that is being viewed, not the request.
Would it work on a page with somehow attaching php to execute it?
Any help at all would be greatly appreciated. Im really impressed with Grav overall.. and I feel there is a very simple answer to this.

9 years ago

You can certainly put data into a YAML file either in the user/config folder, or directly into the YAML frontmatter of a page. Then simply iterate over that data. Then you could simply loop over that data with Twig, so yah it's totally doable.

9 years ago

Hi rhuk, Im extremely new to Grav so forgive me if Im having troubles following.
How does the putting a file into the user/config solve the problem? Can I put PHP in that file? I need to make MySQL calls in PHP to get the data so, this needs to remain in some way.

I assume you are talking about fixed data in files - which is another issue. Here is what I can get working at the moment, but "outside" Grav.

<?php
use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;

PHP
$autoload = __DIR__ . '/../vendor/autoload.php';
if (!is_file($autoload)) {
    die("Please run: <i>bin/grav install</i>");
}

// Register the auto-loader.
$loader = require_once $autoload;

// Set timezone to default, falls back to system if php.ini not set
date_default_timezone_set(@date_default_timezone_get());

// Set internal encoding if mbstring loaded
if (!extension_loaded('mbstring')) {
    die("'mbstring' extension is not loaded.  This is required for Grav to run correctly");
}
mb_internal_encoding('UTF-8');

// Get the Grav instance
$grav = Grav::instance(
    array(
        'loader' => $loader
    )
);

//$limit = $_GET['limit'];
//$offset = $_GET['offset'];

$uri = $grav['uri'];
$config = $grav['config'];

//Connect to DB
$server = $config->get('plugins.db-table.mysql_server');
$port = $config->get('plugins.db-table.mysql_port');
$user = "root"; //$config->get('plugins.db-table.mysql_username');
$pwd = ************"; //$config->get('plugins.db-table.mysql_password');
$db = 'mydatabase'; //$params['db'];
$table = 'mytable'; //$params['table'];

// Establish MySQL Connection
$db_con = \mysqli_connect($server,$user,$pwd,$db,$port);
if(!$db_con) {
    throw new \RuntimeException($user .":" . $pwd ."@" . $server .":" . $port ."/" . $db . " | " . mysqli_connect_error());
}

//Grab the table for display
$tableSQL = "SELECT * FROM " . $db . "." . $table . " ";
if($tableResult = \mysqli_query($db_con,$tableSQL)) {
} else {
    throw new \RuntimeException(mysqli_error($db_con));
}

$rawdata = array();
$i=0;
while($row = mysqli_fetch_array($tableResult))
{
    $rawdata[$i] = $row;
    $i++;
}

mysqli_close($db_con);

$outdata = array(
    'total' => $i,
    'rows' => $rawdata
);

echo json_encode($outdata);

This was a test to make sure I can fetch the data.
I also need to write back to the database too. The bootstrap-table provides events to call urls when these events happen.

Unless Im missing something. Can I put this directly into /user/config/some file.yaml
Are there any examples that respond to "post request".

last edited 06/30/17 by David Lannan
9 years ago

Sorry I thought your DB example was just an example, Grav being flat doesn't use a DB by default, so we typically use YAML or JSON files to contain data. That said if you already have a DB that you need to get data out of, you can do so from a plugin. You don't need to access a PHP file directly, you can create a plugin that performs some actions when a particular path is accessed, or a specific param or something. Take a look at the feed plugin for example. This responds to a particular URL and outputs XML. Obviously not exactly the same but you can see how an HTML output with Twig rendering data loaded from a DB could definitely be achieved.

9 years ago

Hi rhuk - yes, that does sound much like what I need.
Thanks for the assist. I hope to turn this into a "Database Table" plugin/tool. I used the Database Forms plugin as a basic starter for the plugin, but the Feeds one does look more appropriate.
Thanks again.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago