I am using $wpdb to do a mysql query and get the data from a php file.
$results = $wpdb->get_results("SELECT * FROM table_name");
Now, I have registered a Gutenberg block with the wordpress api..
the component is a .js file, which uses (Wordpress React) under the hood:
import { registerBlockType } from "@wordpress/blocks";
import { __ } from "@wordpress/i18n";
import edit from "./pruefungenEdit";
registerBlockType("mytheme-blocks/pruefungen", {
title: __("Pruefungen Block", "mytheme-blocks"),
description: __("Display a Table of exams", "mytheme-blocks"),
category: "mytheme-category",
icon: {
background: "#f03",
foreground: "#fff",
src: "dashicons-database",
},
keywords: [__("pruefungenBlock", "mytheme-blocks")],
edit: edit,
save: () => {
return <p>Test</p>;
},
});
This is my React component that I use for above edit() function:
import { Component } from "@wordpress/element";
import { withSelect } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
class PruefungenEdit extends Component {
render() {
//where to get the data from the query in .php file??
return <div>Data...</div>;
}
}
export default PruefungenEdit;
I can't use php code obviously inside my .js react file.. I thought about setting up a small nodejs server that queries my localhost mysql table, and send that data via custom api route. THen I query that route on my react frontend and display the data... but that looks like too much workaround to me, I mean php is itself some kind of backend, does Wordpress not offer that possibility to query mysql data and send it to the frontend somehow?
My question now is, how do I get the data from the php file into my gutenberg Block that I registered for the block editor frontend?
EDIT - SOLUTION: It was easy to do with registering a custom endpoint and just querying that endpoint inside react frontend...
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/employees', array(
'methods' => 'GET',
'callback' => 'get_employees'
));
});
function get_employees() {
global $wpdb;
$xmlArr = getparsedXMLArray();
$uniqueIDArr = getUniqueID($xmlArr);
$kurs_db = $wpdb->get_results(" SELECT DISTINCT kurs_ort_id, kurs_ort FROM ".$wpdb->prefix . "bsa_location ");
$objJsonDocument = json_encode($kurs_db);
$pruefungen = json_decode($objJsonDocument, TRUE);
$foundPlaces = getAssociatedPlaces($pruefungen, $uniqueIDArr);
$finishedData = addPlace($xmlArr, $foundPlaces);
return $finishedData;
}
Frontend stuff:
import { Component } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import apiFetch from "@wordpress/api-fetch";
class PruefungenEdit extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
employees: [],
};
}
componentDidMount() {
try {
apiFetch({
path: "/wp/v1/employees",
}).then((employees) => {
this.setState({ employees, isLoaded: true });
});
} catch (error) {
console.log(error);
this.setState({
error: error,
isLoaded: true,
});
}
}
render() {
const { error, isLoaded, employees } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<div className={"container"}>
<div className="row">
<div className="col-xs-12">
<table
id="pruefungentable"
className="table striped dark"
style={{ width: "100%" }}
>
<thead className="thead-dark ">
<tr scope="row" className="table-danger">
<th>#</th>
<th>{__("KursID")}</th>
<th>{__("KursOrt")}</th>
<th>{__("Modul")}</th>
<th>{__("Datum")}</th>
</tr>
</thead>
<tbody>
{employees.map((employee, index) => (
<tr key={index}>
<td>{index}</td>
<td>{employee.KursID}</td>
<td>{employee.KursOrt}</td>
<td>{employee.Modul}</td>
<td>{employee.Datum}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
}
}
export default PruefungenEdit;