开发者

php & mysql while loops

开发者 https://www.devze.com 2023-04-11 03:07 出处:网络
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:

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_fe开发者_运维技巧tch_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


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.


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 } ?>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消