0

I have a variable named post_counter, and I want to get the value of it after it finished the loop, and I want it as the first list item to display. but the problem is the php only read the variable as 0 because the compiler read the codes from top to bottom. how can i update my counter? this is my php code:

$post_counter = 0;
<ul>
<?php
$query_post = "SELECT post_date,post_message FROM tbl_posts";
$post_run = mysqli_query($con,$query_post);
echo "<li style = 'text-align:center; cursor:default'><h3>"."New posts".$post_counter."</h3></li>";
while($row = mysqli_fetch_assoc($post_run))
{
    echo "<li><a class = 'mark_read'>"."mark as read"."</a></li>";
    $post_counter++;
}
?>
</ul>

the output of this will be:

New posts 0

but it should be

New posts 5 //since there are 5 rows in my table that will be selected
1
  • 1
    Since you're using it before you actually loop, it will always print 0. Why not get the mysqli_num_rows() of the query and set that for the "New Posts" ? Commented Oct 8, 2014 at 1:16

1 Answer 1

3

Use mysqli_num_rows()

$post_run = mysqli_query($con,$query_post);
$num_posts = mysqli_num_rows($postrun);
echo "<li style = 'text-align:center; cursor:default'><h3>"."New posts".$num_posts ."</h3></li>";
Sign up to request clarification or add additional context in comments.

9 Comments

You can't tell the OP to use mysql_ functions and then say don't use mysql_ functions...
Yes I can. I can tell them how to solve their problem and then I can tell them a better way to do it. They get a solution and learn a better way to do it.
this answer does not merit a downvote, and is correct
What's funny is the real issue is that they were using mysqli the whole time. I just saw mysql since it is what we see the most here.
This has been quite the rollercoaster.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.