0

I'm new in PHP and I think this is an easy question.

I can show the content that comes from my db this way:

...
...
 <?php while($rows = mysql_fetch_array($queryNews)){    ?>
  <div class='title_news'><?php echo $row['Title'] ?> </div>
.... and so on... 

Thats ok so far

Then I want to simulate a kind of pagination: I mean showing as many numbers as news I first tried a for-loop

<?php for($i=1; $i<= sizeof(mysql_fetch_array($queryNews)); $i++){ ?>
    <div class="num_txt"><?php echo $i ?></div>
<?php } ?>

No success. so I tried an similar way as the first one, also without success.

    <?php 
        $i=1;
        while($row = mysql_fetch_array($queryNews)){    ?>
        echo '<script language="javascript">confirm('.$i.')</script>;';
            <div class="num_txt"><?php echo $i ?></div>
   <?php $i++; } ?>

Does anyone knows what I'm doing wrong?? Thanks

3 Answers 3

2

Use mysql_num_rows to calculate the total number of rows, or create a variable that increments when you loop through your records to get the total.

Sign up to request clarification or add additional context in comments.

1 Comment

Dont' know why but the block of code that increments weren't showing up..it is now... When you refer to a a variable that increments, is like this way?? Meanwhile I'll trie the mysql_num_rows. Thanks
1

Your for loop was almost correct, but sizeof(mysql_fetch_array($queryNews)) fetches a row from the result set and returns the number of columns in that row. You want the number of rows in the result set:

<?php for($i=1, $numRows = mysql_num_rows($queryNews); $i<= $numRows; $i++){ ?>
    <div class="num_txt"><?php echo $i ?></div>
<?php } ?>

Comments

1

To generate an array of numbers try this:

$r = range(mysql_num_rows(YOUR_RESULT_SET));

foreach($r as $n){
echo $n;
}

Thats not pagination as most define it, but you can easily search for PHP Pagination if you want to learn how to get groups of, say, 10 results at a time using the LIMIT clause in Mysql.

Comments

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.