Bakit kaya error ilan days ko lang di nabuksan

loktoy

Established
Warning: Undefined variable $post_row in C:\xampp\htdocs\Innerjoin\user\posting.php on line 62

Warning
: Trying to access array offset on value of type null in C:\xampp\htdocs\Innerjoin\user\posting.php on line 62


bakit kaya ganito error ilan araw lang di ko nabuksan.. siguro sa hours, days po ang error nya


$post_query = mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_created as TimeSpent FROM post left join registration on registration.user_id = post.user_id where comment_id = '$id_comment' order by comment_id DESC limit 1 ") or die(mysqli_error($con));
// loop
while ($content_row = mysqli_fetch_array($post_query)){
$id = $content_row['comment_id']; //get the comment_id
$uid = $content_row['user_id']; //get the user_id
$postedby = $content_row['username']; //get the username
?> <div class="container col-md-6 bg-body-tertiary border rounded-3 mb-2 p-3">
<h5>Title: &nbsp;&nbsp;<?php echo $content_row['title']; ?></h5>
</div>
<!-- body of posted -->
<div class="container col-md-6 bg-body-tertiary border rounded-3">
<p>Posted by: <a href="#" style= "margin-left:10px; text-decoration:none;"> <?php echo $postedby; ?></a>
<!-- time interval --><br>
<?php
$days = floor($content_row['TimeSpent'] / (60 * 60 * 24));
$remainder = $content_row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "A few seconds ago";
elseif($days == 0 && $hours == 0)
echo $minutes.' minutes ago';


?>
 
The error you're encountering is due to an undefined variable $post_row. It appears that you're trying to access $post_row['date_created'] on line 62, but $post_row is not defined anywhere in your code.

Looking at your code, it seems that you meant to use $content_row instead of $post_row in the if condition. You should change the line to:

PHP:
echo date('F d, Y - H:i:sa', $content_row['date_created']);

This will retrieve the date_created value from the $content_row array.

Additionally, it would be a good practice to check if the $content_row variable is not null before accessing its elements. You can modify the if condition like this:

PHP:
if ($content_row && $days > 0) {
    echo date('F d, Y - H:i:sa', $content_row['date_created']);
} elseif ($days == 0 && $hours == 0 && $minutes == 0) {
    echo "A few seconds ago";
} elseif ($days == 0 && $hours == 0) {
    echo $minutes.' minutes ago';
}

This will prevent accessing the array elements when $content_row is null, avoiding the "Trying to access array offset on value of type null" warning.

Make sure to apply these changes to your code, and the error should be resolved.
 

About this Thread

  • 2
    Replies
  • 321
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
636
Guests online
1,260
Total visitors
1,896

Forum statistics

Threads
2,274,090
Posts
28,953,622
Members
1,235,061
Latest member
ekaj_meiho
Back
Top