What's new

PHP Help php

itsmeaze

Honorary Poster
Established
Joined
Apr 2, 2020
Posts
254
Solutions
2
Reaction
26
Points
199
Age
23
pahelp naman po sa system namin. ang kailangan po kase pag sinelect yung month, yung data ng month na naselect ang maeexport into excel. example kung january ang naselect, dapat data lang ng january ang maexport into excel. ang problema po eh hindi ko alam kung paano gagawin yun. eto po code ko

creditors.php
<?php
session_start();
require("../../database.php");
require_once('../../crgmclass.php');
$users = $crgm->getUsers();

$userdetails = $crgm->get_userdata();
if (isset($userdetails)) {
if ($userdetails['access'] != "crgm") {
header("Location: ../../login.php");
}
} else {
header("Location: ../../login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Monthly Reports</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="You do not have permission to view the full content of this post. Log in or register now.">
<link rel="stylesheet" href="../crgm2024.css">
<script src="../crgm2024.js"></script>

<!-- Bootstrap JS (jQuery is required) -->
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet" />
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>

</head>
<body>
<div id="container">
<button id="toggleBtn" onclick="toggleNav()">☰</button>
<img id="logo" src="../logo.png" alt="Logo">
<div id="header">
<h2 a href="">CRGM Monitoring and Management System</a></h2>

</div>
</div>
<div id="content-container" style="margin-left: 0;">
<div id="sidebar">
<a href="../crgmmain.php">Home</a>
<a href="../dashboard/dashboard.php">Dashboard</a>
<a href="../expenses/expenses.php">Expenses</a>
<a href="../purchase_request/purchase_request.php">Purchase Request</a>

<a href="#">Collections</a>
<div class="submenu">
<a href="../sales/sales.php">Sales</a>
<a href="../ar_collections/account_receivables.php">Account Receivables</a>
</div>
<a href="#">Reports</a>
<div class="submenu">
<a href="creditors.php">Monthly Creditors</a>
<a href="monthly.php">Monthly</a>
<a href="quarterly.php">Quarterly</a>
<a href="annually.php">Annually</a>
</div>
<a href="../../logout.php">Log Out</a>
</div>

<div class="container" style="margin-top: -15px; ">
<center>
<h1><strong>MONTHLY CREDITORS</strong></h1>
</center>
<br>
<div class="row">

<a href="print_pdf.php" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span>
PDF</a>
<a href="../reports/export_excel_creditors.php?month=1" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span>
Excel</a>
</div>
<br>
<select id="selectHatcheryMonth" class="form-select form-control" onchange="filterDataHatchery()">
<option disabled selected>All Months</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<table class="table table-bordered table-striped table-hover">
<thead class="table ">
<tr>


<th>Date</th>
<th>Name of Creditor</th>
<th>Particulars</th>
<th>Purpose</th>
<th>QTY/kg</th>
<th>Total</th>
</tr>
</thead>
<tbody id="monthlyDataHatchery">
<?php
// Example SQL to fetch data for a particular month and year from cash orders
// SELECT * FROM cash_order WHERE MONTH(created_at) = 11 AND YEAR(created_at) = 2023;

$query = "SELECT * FROM credit_order";
$query_run = mysqli_query($conn, $query);
if (mysqli_num_rows($query_run) > 0) {
foreach ($query_run as $data) {
?>
<tr>
<td>
<?= $data['created_at']; ?>
</td>
<td>
<?= $data['fullname']; ?>
</td>
<td>
<?= $data['product_name']; ?>
</td>
<td>
<!-- <?= $data['business_enterprise']; ?> -->
</td>

<td>
<?= $data['quantity']; ?>
</td>

<td>
<?= number_format($data['totalAmount'], 2, '.', ','); ?>
</td>

</tr>
<?php
}
} else {
// echo "<h5> No Record Found </h5>";
}
?>
</tbody>
</table>
</div>
<script>
function filterDataHatchery() {
var selectedHatcheryMonth = document.getElementById("selectHatcheryMonth").value;
$.ajax({
type: "POST",
url: "fetch_monthly_creditors_data.php", // Replace with the actual filename where you put the PHP code
data: { selectedHatcheryMonth: selectedHatcheryMonth },
success: function (response) {
$("#monthlyDataHatchery").html(response);
}
});
}
</script>
<script src="../../script.js"></script>
</body>
</html>

eto po yung kung nasaan yung export button. kung mapapansin nyo po eh may month=1 dun sa button ng excel kase di ko po alam kung pano. pag tinanggal naman po yung month=1, lahat ng data sa lahat ng month ang maeexport.


eto naman po yung code ko sa export_excel_creditors.php na nakalink sa excel button ko
<?php
require_once '../../phpspreadsheet/vendor/autoload.php'; // Adjust the path as needed
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Database connection
include_once '../../database.php';
$selectedMonth = isset($_GET['month']) ? intval($_GET['month']) : 0;
// Set the default month to the current month if no month is selected
if ($selectedMonth == 0) {
$selectedMonth = date('n');
}
$query = "SELECT * FROM credit_order WHERE MONTH(created_at) = $selectedMonth";
$query_run = mysqli_query($conn, $query);
// Create the SQL query with the selected month

// $query = "SELECT * FROM credit_order WHERE business_enterprise='Rice Production' AND MONTH(created_at) = $month AND YEAR(created_at) = $year";
// $query_run = mysqli_query($conn, $query);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Date');
$sheet->setCellValue('B1', 'Name of Creditor');
$sheet->setCellValue('C1', 'Particulars');
$sheet->setCellValue('D1', 'Purpose');
$sheet->setCellValue('E1', 'QTY/kg');
$sheet->setCellValue('F1', 'Total');
$row = 2;
if (mysqli_num_rows($query_run) > 0) {
foreach ($query_run as $data) {
$sheet->setCellValue('A' . $row, $data['created_at']);
$sheet->setCellValue('B' . $row, $data['fullname']);
$sheet->setCellValue('C' . $row, $data['product_name']);
$sheet->setCellValue('D' . $row, $data['business_enterprise']);
$sheet->setCellValue('E' . $row, $data['quantity']);
$sheet->setCellValue('F' . $row, number_format($data['totalAmount'], 2, '.', ','));
$row++;
}
}
$writer = new Xlsx($spreadsheet);
$filename = 'monthly_creditors_' . date('Y-m-d') . '.xlsx';
$writer->save($filename);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
readfile($filename);
unlink($filename);
exit;
?>


sana po matulungan nyo ako
 
Ang iyong code ay halos tama na, ang kulang lang ay ang pagpasa ng buwan (month) sa fetch_monthly_creditors_data.php at pag-update ng query doon batay sa napiling buwan. Maaari mong subukan ang mga sumusunod na hakbang:

1. Sa creditors.php, baguhin ang export_excel button link upang hindi na magpasa ng month=1. Ito ay para hindi na sya mag-rely sa URL parameter para sa pagpili ng buwan.

Code:
html
    <a href="../reports/export_excel_creditors.php" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span>
        Excel</a>

2. Baguhin ang filterDataHatchery() function para magpadala ng napiling buwan sa fetch_monthly_creditors_data.php gamit ang AJAX request.

Code:
javascript
    function filterDataHatchery() {
        var selectedHatcheryMonth = document.getElementById("selectHatcheryMonth").value;
        $.ajax({
            type: "POST",
            url: "fetch_monthly_creditors_data.php",
            data: { selectedHatcheryMonth: selectedHatcheryMonth },
            success: function (response) {
                $("#monthlyDataHatchery").html(response);
                window.location.href = "../reports/export_excel_creditors.php"; // Redirect to the export page
            }
        });
    }

3. Sa fetch_monthly_creditors_data.php, tanggapin ang buwan na ipinapasa mula sa AJAX request at baguhin ang query batay sa napiling buwan.

Code:
php
    <?php
    require("../../database.php");
    $selectedMonth = isset($_POST['selectedHatcheryMonth']) ? intval($_POST['selectedHatcheryMonth']) : 0;
    $query = "SELECT * FROM credit_order WHERE MONTH(created_at) = $selectedMonth";
    $query_run = mysqli_query($conn, $query);
    
    if (mysqli_num_rows($query_run) > 0) {
        foreach ($query_run as $data) {
            // Output your data here as appropriate
            echo "<tr>";
            echo "<td>" . $data['created_at'] . "</td>";
            // Add other table cells accordingly
            echo "</tr>";
        }
    } else {
        echo "<tr><td colspan='6'>No records found.</td></tr>";
    }
    ?>

Sa ganitong paraan, ang iyong sistema ay dapat na magre-redirect sa export page na may tamang query para sa napiling buwan matapos pumili sa dropdown menu. Mangyaring i-update mo rin ang export page upang magamit ang tamang query para sa pag-export ng data batay sa napiling buwan.

Nawa'y makatulong ito sa iyong sistema! Kung may iba kang katanungan o pangangailangan ng karagdagang tulong, huwag mag-atubiling itanong.
 
Ang iyong code ay halos tama na, ang kulang lang ay ang pagpasa ng buwan (month) sa fetch_monthly_creditors_data.php at pag-update ng query doon batay sa napiling buwan. Maaari mong subukan ang mga sumusunod na hakbang:

1. Sa creditors.php, baguhin ang export_excel button link upang hindi na magpasa ng month=1. Ito ay para hindi na sya mag-rely sa URL parameter para sa pagpili ng buwan.

Code:
html
    <a href="../reports/export_excel_creditors.php" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span>
        Excel</a>

2. Baguhin ang filterDataHatchery() function para magpadala ng napiling buwan sa fetch_monthly_creditors_data.php gamit ang AJAX request.

Code:
javascript
    function filterDataHatchery() {
        var selectedHatcheryMonth = document.getElementById("selectHatcheryMonth").value;
        $.ajax({
            type: "POST",
            url: "fetch_monthly_creditors_data.php",
            data: { selectedHatcheryMonth: selectedHatcheryMonth },
            success: function (response) {
                $("#monthlyDataHatchery").html(response);
                window.location.href = "../reports/export_excel_creditors.php"; // Redirect to the export page
            }
        });
    }

3. Sa fetch_monthly_creditors_data.php, tanggapin ang buwan na ipinapasa mula sa AJAX request at baguhin ang query batay sa napiling buwan.

Code:
php
    <?php
    require("../../database.php");
    $selectedMonth = isset($_POST['selectedHatcheryMonth']) ? intval($_POST['selectedHatcheryMonth']) : 0;
    $query = "SELECT * FROM credit_order WHERE MONTH(created_at) = $selectedMonth";
    $query_run = mysqli_query($conn, $query);
  
    if (mysqli_num_rows($query_run) > 0) {
        foreach ($query_run as $data) {
            // Output your data here as appropriate
            echo "<tr>";
            echo "<td>" . $data['created_at'] . "</td>";
            // Add other table cells accordingly
            echo "</tr>";
        }
    } else {
        echo "<tr><td colspan='6'>No records found.</td></tr>";
    }
    ?>

Sa ganitong paraan, ang iyong sistema ay dapat na magre-redirect sa export page na may tamang query para sa napiling buwan matapos pumili sa dropdown menu. Mangyaring i-update mo rin ang export page upang magamit ang tamang query para sa pag-export ng data batay sa napiling buwan.

Nawa'y makatulong ito sa iyong sistema! Kung may iba kang katanungan o pangangailangan ng karagdagang tulong, huwag mag-atubiling itanong.
<?php
// fetch_monthly_data.php
include('../../database.php');
try {
$selectedHatcheryMonth = $_POST['selectedHatcheryMonth'];
// Establish a connection to the database using PDO
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Customize your queries based on the selected month
// Example: Query to fetch data for January
$query = "SELECT * FROM credit_order WHERE MONTH(created_at) = :selectedHatcheryMonth";
// Prepare and execute the query
$stmt = $conn->prepare($query);
$stmt->bindParam(':selectedHatcheryMonth', $selectedHatcheryMonth);
$stmt->execute();
// Fetch data
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0) {
foreach ($result as $data) {
?>
<tr>
<td><?= $data['fullname']; ?></td>
<td><?= $data['created_at']; ?></td>
<td><?= $data['product_name']; ?></td>
<td><?= $data['business_enterprise']; ?></td>
<td><?= $data['code']; ?></td>
<td><?= $data['quantity']; ?></td>
<td><?= number_format($data['amount'], 2, '.', ','); ?></td>
<td><?= number_format($data['totalAmount'], 2, '.', ','); ?></td>
</tr>
<?php
}
} else {
// echo "<h5> No Record Found </h5>";
}
} catch (PDOException $e) {
// Display an error message if connection or query fails
echo "Connection failed: " . $e->getMessage();
} finally {
// Close the database connection
$conn = null;
}
?>


eto yung fetch_monthly_creditors_data.php ko. ano gagawin ko?
 
Last edited:

Similar threads

Back
Top