Topic: Work with mySQL database and php pages

Motiv-8 free asked 5 years ago


Wow, i just came across your website after struggling to edit mySQL tables in php.

Before I buy, Will MB Bootstrap editor work with mySQL database and php pages? I already have my data connection to the database, etc. And have Datatables running.

I don't want to pay Datatables anything? Because i've always used bootstrap, and your implementation looks smarter too.


Piotr Glejzer staff commented 5 years ago

Do you mean MDB Table Editor, don't you? MDB Table Editor based on MIT version of DataTable and it was created for 'front pages' and we didn't have any examples to connect it from the database ( MySQL, PostgreSQL etc.) with some backend like PHP, node.js. python or something else. We worry about that but if you will buy it you probably you have to for your own (for this moment).



Hello Motiv-8!

While I'm not associated with MDBootstrap's staff, I do have a lot of experience using MDB Pro as I'm currently using it in production on my company website, control panel, all of which runs MySQL and embedded SQLite.

It's important to remember that MDBootstrap is a Design Framework. It doesn't do a lot of things automatically.

To get datatables working with server sided generated data, you need both JavaScript and PHP, since they don't speak with each other so to say, you need to convert the data that you get from MySQL to JSON, through either returning or echoing out json_decode($yourData);



Here's an example we use in production using SLIM Framework v3 for our API :

<?php
// Setup
use \Psr\Http\Message\ResponseInterface as Response;
use \Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Get transactions done by a user
 *
 * @param  \Psr\Http\Message\ServerRequestInterface $req  PSR7 request
 * @param  \Psr\Http\Message\ResponseInterface      $res  PSR7 response
 * @param  array                                    $args Route parameters
 *
 * @return \Psr\Http\Message\ResponseInterface
 */
$api->get('/v1/transactions/{id:[0-9]+}', function (Request $request, Response $response, array $args) {
    $id = $args['id'];

    if (!is_numeric($id)) {
        return $response->withStatus(400);
    }

    // Get the users transactions
    $transactions = $this->db->prepare('SELECT * FROM transactions WHERE userId = ?');
    $transactions->execute(array($id));

    // Return any response (even empty) as JSON
    return $response->withJson($transactions->fetchAll(PDO::FETCH_ASSOC));
});
?>



When ran, the returned data looks similar to this:

[{
  "id": 58,
  "ref": "6eee48f9-f88c-44f0-ba20-e337ef587a49",
  "uuid": "ec87c215-e2c2-4d46-871c-b810d8e85130",
  "status": "COMPLETED",
  "transactionDate": "2019-01-13 23:52:14",
  "transactionUpdate": "2019-02-18 10:10:33",
  "amount": 87500,
  "userId": "e5d9fbb3-8916-4b6b-8aee-030f70553de0",
  "cardNumber": "123456XXXXXX1234",
  "productId": 4,
  "env": "live",
}, {
  "id": 58,
  "ref": "6eee48f9-f88c-44f0-ba20-e337ef587a49",
  "uuid": "ec87c215-e2c2-4d46-871c-b810d8e85130",
  "status": "COMPLETED",
  "transactionDate": "2019-01-13 23:52:14",
  "transactionUpdate": "2019-02-18 10:10:33",
  "amount": 87500,
  "userId": "e5d9fbb3-8916-4b6b-8aee-030f70553de0",
  "cardNumber": "123456XXXXXX1234",
  "productId": 4,
  "env": "live",
}, ... ]

On the client side, you structure the data with dataTables. There are a lot of guides on datatables.net on how to do this, here's an example on how we inject the data into datatables. Given the following datatable example:

<div class="table-wrapper">
  <table class="table table-hover mb-0 table-responsive table-responsive-sm" cellspacing="0" width="100%" id="transactions">
    <thead>
      <tr>
        <th><a>ID</a></th>
        <th><a>Price</a></th>
        <th><a>Product ID</a></th>
        <th class="th-lg"><a>Purchase Date</a></th>
        <th class="th-lg"><a>Card Number</a></th>
        <th><a>Status</a></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

We use the following JavaScript snippet to read the returned MySQL data and structure it within the datatable. Notice how the amount of variables we're pushing to the table.row.add([]) array equals the amount of headers we have in the datatable HTML:

const $userId = ' ... ';
$.get(`https://api.example.com/v1/transactions/${$userId}`, (data) => {
  if (data && data.length > 0) {
    // Store a reference to the datatable
    const table = $('#transactions').dataTables({ ... });

    // Removes all data already in the datatable
    table.clear();

    // Parse the data into the datatable
    data.forEach((entry) => {
      table.row.add([
        entry.id,
        entry.amount,
        entry.productId,
        entry.transactionDate,
        entry.cardNumber,
        entry.status
      ]);
    });
  }
}).fail(); // Handle HTTP errors

I hope this example can help you on your way. :)


Motiv-8 free commented 5 years ago

I'm not understanding this at all: $.get(https://api.example.com/v1/transactions/${$userId}, (data) => {

I already have the data connection via PHP, the connection is $result = mysqli_query($link, $Sql); so not sure how it would link in?? At the moment there are no users, but thee will be in the future.

Thank you so far, i'm on the right track with what you have provided. Json is new to me, but looks logical enough.


Arnt Oddvar Pedersen pro commented 5 years ago

Not a problem!

The line of code you copied is basically a Ajax GET request shorthand provided by jQuery, what it basically says is:

"Resolve the provided URL and return any data", then the arrow function (ES6 version of your regular javascript function) takes all the data that is returned by the URL and assigns it to a variable called data, that you can use within the function to parse the returned content. (In this case, JSON)

Over the weekend I'll see if I can't create an example page for you to mess around with if you're not very familiar with MDB+Javascript+PHP :)


Motiv-8 free commented 5 years ago

Thank you that would be ideal. My fields are: ID, Artist, Title.


Grf free answered 5 years ago


Suggestion to look for a content management system, f.i. cms madesimple.


Motiv-8 free commented 5 years ago

Thnak you Grf, i did look in to wordpress, and a table plugin, but i'm liking datatables. Also like Flexigrid, but their search is non existant, also adding the alphabet.js plugin doesn't work either. CMS Made Simple, has only one table plugin listed on their website, which hasn't been updated in over a year.


Motiv-8 free answered 5 years ago


I have bought MD tables editor

Is it possible for it to work on server side data?


Piotr Glejzer staff commented 5 years ago

It is possible to load some JSON files to your datatables.
Here is an example:
https://datatables.net/examples/data_sources/ajax
Here is an example with async functions.
https://mdbootstrap.com/support/jquery/datatables-editor-stop-table-edit/


Motiv-8 free commented 5 years ago

Hi Piotr,

Those examples do not show Server Side data.

I would like to edit and delete the data server side (to edit the actual mysql database) and not just the local table that is on screen.


Piotr Glejzer staff commented 5 years ago

Hello, like I said before we dont have example how to connect any datatable with server side programming language.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: 4.7.3
  • Device: desktop
  • Browser: chrome
  • OS: win 10
  • Provided sample code: No
  • Provided link: No