What's new

Help How to prevent looping again the tbody

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,927
Reaction
472
Points
658
Age
24
Im using data stored from session and when I refresh the page the tbody and the td is looping and printing again.
How to prevent looping again the tbody when refreshing the page without unsetting the session. Here's my code

$html.="<table border='1' width='100%' style='padding-top: 8px; border-collapse: collapse; font-size: 10px;'>";
$html.="<thead><tr>";
$html.= "<th>"._('ID')."</th>
<th>"._('FIRSTNAME')."</th>
<th>"._('LASTNAME')."</th>
<th>"._('ADDRESS')."</th>";
$html.= "<tbody>";
for($x = 0; $x < sizeof($data2); $x++){
$html.= "<tr>";
$html.= "<td>". $data2[$x]->ID."</td>
<td>". $data2[$x]->FIRSTNAME."</td>
<td>". $data2[$x]->LASTNAME."</td>
<td>". $data2[$x]->ADDRESS."</td>
$html.= "</tr>";
}
$html.= "</tbody>";
$html.= "</table>
 
To prevent the tbody from looping and printing again when refreshing the page, you can check if the data stored in the session is already set before generating the table. Here's an example of how you can modify your code to achieve this:

PHP:
// Check if the session data is already set
if (!isset($_SESSION['data2'])) {
    // Generate the data and store it in the session
    // YOUR CODE TO GENERATE DATA HERE

    $_SESSION['data2'] = $data2;
} else {
    // Retrieve the data from the session
    $data2 = $_SESSION['data2'];
}

// Generate the table using the data from the session
$html .= "<table border='1' width='100%' style='padding-top: 8px; border-collapse: collapse; font-size: 10px;'>";
$html .= "<thead><tr>";
$html .= "<th>"._('ID')."</th>
            <th>"._('FIRSTNAME')."</th>
            <th>"._('LASTNAME')."</th>
            <th>"._('ADDRESS')."</th>";
$html .= "</tr></thead>";
$html .= "<tbody>";
for ($x = 0; $x < sizeof($data2); $x++) {
    $html .= "<tr>";
    $html .= "<td>". $data2[$x]->ID."</td>
                <td>". $data2[$x]->FIRSTNAME."</td>
                <td>". $data2[$x]->LASTNAME."</td>
                <td>". $data2[$x]->ADDRESS."</td>";
    $html .= "</tr>";
}
$html .= "</tbody>";
$html .= "</table>";

This code first checks if the $_SESSION['data2'] variable is set. If it is not set, it generates the data and stores it in the session. If it is already set, it retrieves the data from the session. This way, even if the page is refreshed, the data will not be regenerated, preventing the tbody from looping again.
 
To prevent the tbody from looping and printing again when refreshing the page, you can check if the data stored in the session is already set before generating the table. Here's an example of how you can modify your code to achieve this:

PHP:
// Check if the session data is already set
if (!isset($_SESSION['data2'])) {
    // Generate the data and store it in the session
    // YOUR CODE TO GENERATE DATA HERE

    $_SESSION['data2'] = $data2;
} else {
    // Retrieve the data from the session
    $data2 = $_SESSION['data2'];
}

// Generate the table using the data from the session
$html .= "<table border='1' width='100%' style='padding-top: 8px; border-collapse: collapse; font-size: 10px;'>";
$html .= "<thead><tr>";
$html .= "<th>"._('ID')."</th>
            <th>"._('FIRSTNAME')."</th>
            <th>"._('LASTNAME')."</th>
            <th>"._('ADDRESS')."</th>";
$html .= "</tr></thead>";
$html .= "<tbody>";
for ($x = 0; $x < sizeof($data2); $x++) {
    $html .= "<tr>";
    $html .= "<td>". $data2[$x]->ID."</td>
                <td>". $data2[$x]->FIRSTNAME."</td>
                <td>". $data2[$x]->LASTNAME."</td>
                <td>". $data2[$x]->ADDRESS."</td>";
    $html .= "</tr>";
}
$html .= "</tbody>";
$html .= "</table>";

This code first checks if the $_SESSION['data2'] variable is set. If it is not set, it generates the data and stores it in the session. If it is already set, it retrieves the data from the session. This way, even if the page is refreshed, the data will not be regenerated, preventing the tbody from looping again.
Its still the same looping the td data twice when I load the page
 
Its still the same looping the td data twice when I load the page
Apologies for the confusion. It seems that my previous code did not address the issue correctly. In order to prevent the tbody from looping again when refreshing the page, you can use a flag variable to keep track of whether the data has already been displayed or not. Here's an updated version of the code:

PHP:
// Check if the data has already been displayed
if (!isset($_SESSION['isDataDisplayed'])) {
    // Generate the data and store it in the session
    // YOUR CODE TO GENERATE DATA HERE

    $_SESSION['data2'] = $data2;
    $_SESSION['isDataDisplayed'] = true;
} else {
    // Retrieve the data from the session
    $data2 = $_SESSION['data2'];
}

// Generate the table using the data from the session
$html .= "<table border='1' width='100%' style='padding-top: 8px; border-collapse: collapse; font-size: 10px;'>";
$html .= "<thead><tr>";
$html .= "<th>"._('ID')."</th>
            <th>"._('FIRSTNAME')."</th>
            <th>"._('LASTNAME')."</th>
            <th>"._('ADDRESS')."</th>";
$html .= "</tr></thead>";
$html .= "<tbody>";
for ($x = 0; $x < sizeof($data2); $x++) {
    $html .= "<tr>";
    $html .= "<td>". $data2[$x]->ID."</td>
                <td>". $data2[$x]->FIRSTNAME."</td>
                <td>". $data2[$x]->LASTNAME."</td>
                <td>". $data2[$x]->ADDRESS."</td>";
    $html .= "</tr>";
}
$html .= "</tbody>";
$html .= "</table>";

In this code, we're using the $_SESSION['isDataDisplayed'] variable as a flag to determine whether the data has already been displayed. If it is not set, we generate the data, store it in the session, and set the flag to true. This ensures that the data is only generated and displayed once. On subsequent page refreshes, the existing data from the session will be used, preventing the tbody from looping again.

Please give this updated code a try and let me know if it resolves the issue.
 

Similar threads

Back
Top