Home > Software engineering >  using $_session to store variables with dynamic data retrieved from an api with json format to use i
using $_session to store variables with dynamic data retrieved from an api with json format to use i

Time:10-27

hey guys i'm new here and currently working on my first project which is an ERP kindof i'm using google spreadsheets api to retrieve data from a spreadsheet and echo it in a html table but i need to re-use some of that data in multiple pages with multiple tables i retrieve data from a json file as rows and i echo them in a table respectively for a loop the problem is to use that data in other php files i store the variables in a session but when i echo the session in the other file it prints just the first row over all that column is there ana other way or a fix to do this please help !!! note that in the other page i also retrieve some data from different sheets

here is the code i'm using and the first page to retrieve data and print it in the table

<?php
define("API_KEY", "XXXXXXXXXXXX");

$url = sprintf('https://sheets.googleapis.com/v4/spreadsheets/1R8cETVG5XT08iCgZjWOJXSw-HmC_MDOXkjZntEVys40/values/RECAP!AF4:AI?key=AIzaSyD6Mrl9C700rrEKutzvmZa2_43KLEd1sQE', API_KEY);
$json = json_decode(file_get_contents($url));
$rows = $json->values;
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th class="text-center">Nom</th>
                    <th class="text-center">Prénom</th>
                    <th class="text-center">Zone</th>
                    <th class="text-center">RIB</th>

                </tr>
                </thead>
                <tbody>
                <?php foreach($rows as $row){
                    echo '<tr>';
                    $_SESSION['nom_employe'] = $row[3];
                    $_SESSION['prenom_employe'] = $prenom_employe;
                    $prenom_employe = $row[3];
                    $zone_employe = $row[2];
                    $_SESSION['calc'] = (($row[3]) ($row[3]));
                    echo "<td class='text-center'>" . $_SESSION['nom_employe'] . '</td>';
                    echo "<td class='text-center'>" . $prenom_employe . '</td>';
                    echo "<td class='text-center'>" . $zone . '</td>';
                    echo "<td class='text-center'>" . $_SESSION['calc'] . '</td>';
                    echo '</tr>';
                }?>
                </tbody>
            </table>
        </div>
    </div>
</div>

and here is the code i'm using in the page i'm having trouble with

<!-- container start -->
<?php
define("API_KEY", "XXXXXXXXXXXX");

$url = sprintf('https://sheets.googleapis.com/v4/spreadsheets/13I_NndK33-AgnfqELjDn--6pIBLz10dOmH83_KoLqNg/values/SuiviCA!B5:P?key=AIzaSyD6Mrl9C700rrEKutzvmZa2_43KLEd1sQE', API_KEY);
$json = json_decode(file_get_contents($url));
$rows = $json->values;
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th class="text-center">DATE FACRURE</th>
                    <th class="text-center">NOM DE CLIENT</th>
                    <th class="text-center">N° FACTURE</th>
                    <th class="text-center">N° BL</th>
                    <th class="text-center">SEMAINE N°</th>
                    <th class="text-center">MOIS</th>
                    <th class="text-center">SEMAINE</th>
                    <th class="text-center">MT EN EURO</th>
                    <th class="text-center">MT $</th>
                    <th class="text-center">MT MAD</th>
                    <th class="text-center">N° DUM</th>
                    <th class="text-center">AVIS D'EXPORT</th>
                    <th class="text-center">DATE DE PAIE</th>
                    <th class="text-center">MODE DE PAIMENT</th>
                    <th class="text-center">REMARQUE</th>


                </tr>
                </thead>
                <tbody>
                <?php

                    foreach($rows as $row){
                    echo '<tr>';
                    $date_facture_suivi_ca = $row[0];
                    $nom_client_suivi_ca = $row[1];
                    $_SESSION['facture'] = $row[2];
                    $num_bl_suivi_ca = $_SESSION['prenom_employe'];
                    $sem_num_suivi_ca = $row[4];
                    $mois_suivi_ca = $row[5];
                    $sem_suivi_ca = $row[6];
                    $mt_euro_suivi_ca = $row[7];
                    $mt_dollar_suivi_ca = $row[8];
                    $mt_mad_suivi_ca = $row[9];
                    $num_dum_suivi_ca = $row[10];
                    $avis_export_suivi_ca = $row[11];
                    $date_paie_suivi_ca = $row[12];
                    $mode_paiment_suivi_ca = $row[13];
                    $remarque_suivi_ca = $row[14];

                    echo "<td class='text-center'>" . $date_facture_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $nom_client_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $_SESSION['prenom_employe'] . '</td>';
                    echo "<td class='text-center'>" . $num_bl_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $sem_num_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $mois_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $sem_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $mt_euro_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $mt_dollar_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $mt_mad_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $num_dum_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $avis_export_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $date_paie_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $mode_paiment_suivi_ca . '</td>';
                    echo "<td class='text-center'>" . $remarque_suivi_ca . '</td>';

                    echo '</tr>';
                }?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- container end -->

CodePudding user response:

Do not you have a Warning in your code? I got this using code from your example - "Undefined variable $prenom_employe on line 6..." Because it is initialized bellow of real usage;

CodePudding user response:

To start using session cookies you must ad "session_start();" at the very top of your page above the headers

session_start();

  • Related