user12358408
user12358408

Reputation:

Table all row show the same datetime

I have a problem to show the datetime according the upload file datetime in the table. It always show the same current datetime in my table. My problem is how to show accroding my upload file datetime to show in the table?

Below is my coding:

<?php
//$sql = "select * from promotion_list where id=" . $_GET['id'];
$sql_user = "SELECT *FROM upload_file_dms";

                $arr_user = db_conn_select($sql_user);
                foreach ($arr_user as $rs_user) {

                    $created_date = $rs_user['created'];

                }
$i = 1;
$files = scandir("uploads_media_file");
 $date = $files[$a];
for ($a = 2; $a < count($files); $a++)
{

    ?>

     <tr>
     <td> <?php echo $i++;?></td>
      <td>  <?php echo $files[$a]; ?></td>
      <td>  <?php echo $created_date; ?></td>
      <td><button  class="btn btn-sm btn-primary"><a href="uploads_media_file/<?php echo $files[$a]; ?>" download="<?php echo $files[$a]; ?>" style="color: white;">
            Download</a></button>&nbsp &nbsp
      <button  class="btn btn-sm btn-primary"><a href="delete.php?name=uploads_media_file/<?php echo $files[$a]; ?>" style="color: white;">
            Delete
        </a></button></td>
          </tr>

    <?php
}?>

This is my database table info: Output2

Below is my output info, it show me all same the current date time: Output3

Hope someone can guide me how to solve it. I want the date create column according match in the table data.

Upvotes: 0

Views: 50

Answers (1)

janisch
janisch

Reputation: 167

The problem is that your foreach loop where you define that variable $created_date is outside of the loop where you echo it. That means that php loops through the foreach and defines the variable over and over until the loop is over. Then the variable is just the last date (in this case your output)

Solution (untested):

<?php
//$sql = "select * from promotion_list where id=" . $_GET['id'];
$sql_user = "SELECT *FROM upload_file_dms";

$arr_user = db_conn_select($sql_user);

$created_date = array();

foreach ($arr_user as $rs_user) {

    $counter = 0;
    $created_date[$counter] = $rs_user['created'];
    $counter++;

}
$i = 1;
$files = scandir("uploads_media_file");
$date = $files[$a];
for ($a = 2; $a < count($files); $a++) {

    ?>

    <tr>
        <td> <?php echo $i++; ?></td>
        <td>  <?php echo $files[$a]; ?></td>
        <td>  <?php echo $created_date[$a -2]; ?></td>
        <td>
            <button class="btn btn-sm btn-primary"><a href="uploads_media_file/<?php echo $files[$a]; ?>"
                                                      download="<?php echo $files[$a]; ?>" style="color: white;">
                    Download</a></button>
            &nbsp &nbsp
            <button class="btn btn-sm btn-primary"><a
                        href="delete.php?name=uploads_media_file/<?php echo $files[$a]; ?>" style="color: white;">
                    Delete
                </a></button>
        </td>
    </tr>

    <?php
} ?>

Edit: I just coded this in notepad, maybe it doesn't work 100%, but it should at least give you an idea of how to solve it.

Upvotes: 1

Related Questions