Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with a php & Mysql #1428

Open
RicardoSemDedao opened this issue Aug 3, 2023 · 0 comments
Open

Help with a php & Mysql #1428

RicardoSemDedao opened this issue Aug 3, 2023 · 0 comments

Comments

@RicardoSemDedao
Copy link

RicardoSemDedao commented Aug 3, 2023

Hello Team,

I want to express my gratitude for providing us with this excellent grid. It has proven to be extremely user-friendly and efficient to work with. Great job on creating such a useful tool!

I must admit that I'm not a professional programmer, and my experience mostly revolves around small projects for work or immediate necessities. Occasionally, I encounter more complex projects, but they are rare for me. Due to my limited experience, it's possible that the solution to my current problem might be right in front of me, but I could be overlooking it.

With that in mind, I apologize if my question seems obvious or I fail to recognize a straightforward solution. I greatly appreciate your patience and support as I seek to resolve my issue. Thank you for your understanding.

I'm having problems saving my updates or adding new records in the database.

Here is the grid code:
?php
require_once('includes/load.php');

// Checking what level user has permission to view this page
page_require_level(2);

// Header HTML
include_once('layouts/header.php');

// Fetch data from the database table (limit to 40 records)
$db = new MySqli_DB();
$sql = "SELECT * FROM commodity";
$result = $db->query($sql);
$records = $db->while_loop($result);

?
?php
// Enable error reporting
error_reporting(E_ALL);

// Display errors on the screen
ini_set('display_errors', 1);
?

<title>Commodity</title> <style> /* Add any custom styles for the table and scroll container here */ .jsgrid { width: 100%; }
    .search-box {
        margin-bottom: 10px;
    }

    /* Customize the appearance of the edit and duplicate buttons */
    .edit-button, .duplicate-button {
        padding: 2px 3px; /* Smaller padding for buttons */
        font-size: 7px; /* Smaller font size for buttons */
        border-radius: 4px;
        margin-right: 4px;
    }

    .edit-button {
        background-color: #007bff;
        color: white;
        border: none;
    }

    .duplicate-button {
        background-color: #28a745;
        color: white;
        border: none;
    }

    .delete-button {
        background-color: #dc3545;
        color: white;
        border: none;
    }
</style>
Search
<script src="jsgrid/jsgrid.min.js"></script>
<script>
$(document).ready(function() {
console.log(control);

});
// Your PHP records data
var records = ;

    // JavaScript for sorting and searching
    var sortField = 'ID';
    var sortOrder = 'asc';

    function sortData(items, field, order) {
        return items.sort(function(a, b) {
            var aValue = a[field];
            var bValue = b[field];
            return order === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
        });
    }

    function searchTable() {
        var input = document.getElementById('searchInput').value.toLowerCase();
        var filteredRecords = records.filter(function(record) {
            return (
                record['part_number'].toLowerCase().includes(input) ||
                record['haz_un_number'].toLowerCase().includes(input) ||
                record['Description'].toLowerCase().includes(input)
            );
        });

        $("#jsGrid").jsGrid("option", "data", filteredRecords);
    }

    // This section sets up the JSGrid
    $("#jsGrid").jsGrid({
        height: "40%",
        width: "100%",
        filtering: false,
        inserting: true,
        editing: true,
        sorting: true,
        paging: true,
        autoload: true,
        pageSize: 15,
        pageButtonCount: 5,
            controller: {
        loadData: function(filter) {
            return $.ajax({
                type: "GET",
                url: "/data.php",
                data: filter,
                success: function(data) {
                    console.log('Load Data:', data);
                }
            });
        },
        insertItem: function(item) {
            return $.ajax({
                type: "POST",
                url: "/data.php",
                data: item,
                success: function(data) {
                    console.log('Insert Item:', data);
                }
            });
        },
        updateItem: function(item) {
            return $.ajax({
                type: "PUT",
                url: "/data.php",
                data: item,
                success: function(data) {
                    console.log('Update Item:', data);
                }
            });
        },
        deleteItem: function(item) {
            return $.ajax({
                type: "DELETE",
                url: "/data.php",
                data: item,
                success: function(data) {
                    console.log('Delete Item:', data);
                }
            });
        },
    },
        data: sortData(records, sortField, sortOrder),
        fields: [
            { name: "ID", type: "number", title: "ID", width: 30, editing: false },
            { name: "part_number", type: "text", title: "Part Number", width: 70, editing: true },
            { name: "haz_un_number", type: "text", title: "UN Number", width: 70, editing: true },
            { name: "Description", type: "text", title: "Description", width: 300, editing: true },
            { name: "HazPckGroup", type: "text", title: "HPG", width: 30, editing: true },
            { name: "HazClass", type: "text", title: "Class", width: 50, editing: true },
            { name: "Hazmat", type: "text", title: "Hazmat", width: 50, editing: true },
            { name: "PackageType", type: "text", title: "Package", width: 100, editing: true },
            { name: "Size", type: "text", title: "Size", width: 60, editing: true },
            { name: "NetWt", type: "number", title: "Net", width: 30, editing: true },
            { name: "Weight", type: "number", title: "Weight", width: 30, editing: true },
            { name: "Hazard_INHALATION", type: "text", title: "INHALATION", width: 100, editing: true },
            { name: "Molecule", type: "text", title: "Molecule", width: 60, editing: true },
            {
                type: "control",
                width: 40,
                editButton: true,
                deleteButton: true,
                modeSwitchButton: true,
                editButtonTooltip: "Edit",
                deleteButtonTooltip: "Delete",
            }
        ],
        onRefreshed: function(args) {
            $(".jsgrid-table").find("th").each(function(index, element) {
                $(element).off("click").on("click", function() {
                    var fieldName = $(element).data("field");
                    if (sortField === fieldName) {
                        sortOrder = sortOrder === "asc" ? "desc" : "asc";
                    } else {
                        sortField = fieldName;
                        sortOrder = "asc";
                    }
                    $("#jsGrid").jsGrid("option", "data", sortData(records, sortField, sortOrder));
                });
                var arrow = $(element).find(".arrow");
                if (arrow.length === 0) {
                    arrow = $("<span>").addClass("arrow");
                    $(element).append(arrow);
                }
                arrow.removeClass("arrow-up arrow-down");
                if (fieldName === sortField) {
                    arrow.addClass(sortOrder === "asc" ? "arrow-up" : "arrow-down");
                }
            });
        }
    }); // Close the jsGrid initialization function
</script> php include_once('layouts/footer.php'); ============================================= Here is the database action =========================================== php var_dump($_REQUEST); header("Content-Type: application/json"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); header("Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With");

ini_set('display_errors', 'On');
error_reporting(E_ALL);

try {
require_once('includes/load.php');

$input = json_decode(file_get_contents('php://input'),true);
var_dump($input); // Print out data you receive

switch ($_SERVER["REQUEST_METHOD"]) {
    case "GET":
        $result = $db->query("SELECT * FROM commodity");
        header("Content-Type: application/json");
        echo json_encode($db->while_loop($result));
        break;

    case "POST":
        $db->query("INSERT INTO commodity (part_number, haz_un_number, Description, HazPckGroup, HazClass, Hazmat, PackageType, Size, NetWt, Weight, Hazard_INHALATION, Molecule)
            VALUES ('{$input['part_number']}', '{$input['haz_un_number']}', '{$input['Description']}', '{$input['HazPckGroup']}', '{$input['HazClass']}', '{$input['Hazmat']}', '{$input['PackageType']}', '{$input['Size']}', {$input['NetWt']}, {$input['Weight']}, '{$input['Hazard_INHALATION']}', '{$input['Molecule']}')");
        header("Content-Type: application/json");
        echo json_encode($input);
        break;

    case "PUT":
        $db->query("UPDATE commodity SET part_number = '{$input['part_number']}', haz_un_number = '{$input['haz_un_number']}', Description = '{$input['Description']}', HazPckGroup = '{$input['HazPckGroup']}', HazClass = '{$input['HazClass']}', Hazmat = '{$input['Hazmat']}', PackageType = '{$input['PackageType']}', Size = '{$input['Size']}', NetWt = {$input['NetWt']}, Weight = {$input['Weight']}, Hazard_INHALATION = '{$input['Hazard_INHALATION']}', Molecule = '{$input['Molecule']}' WHERE ID = {$input['ID']}");
        header("Content-Type: application/json");
        echo json_encode($input);
        break;

    case "DELETE":
        $db->query("DELETE FROM commodity WHERE ID = {$input['ID']}");
        header("Content-Type: application/json");
        echo json_encode($input);
        break;
}

} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant