Jump to content

Archived

This topic is now archived and is closed to further replies.

theone

Last row different

Recommended Posts

Right...

[code]
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$sql = "SELECT * FROM news_site WHERE `show` = '1'";
$results = mysql_query($sql);
while ($data = mysql_fetch_array($results)) {
    var_dump($data);
        print "<hr>";
}
?>
[/code]

Now that will fetch small news articles from the database, like a blog style. What i want to do is on the last row it prints, i want to drop the print "<hr>".
I cant do it based on a predifned number as the number of rows is not specified cos it depends on the number or news articles.
How do i do that? Or isnt it possible?

Thanks all,
Dave

Share this post


Link to post
Share on other sites
The only way i can think of doing it would be to count the number of rows... etc. But is there a simpler way than that?

Share this post


Link to post
Share on other sites
two ways you can do this - one you have identifed the other... well I think a bit more messy but each to their own.

counting method...

[code]<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$sql = "SELECT * FROM news_site WHERE `show` = '1'";
$results = mysql_query($sql);
$count = mysql_num_rows($result);
$i = 1;
while ($data = mysql_fetch_array($results)) {
    var_dump($data);
     if ($i++ != $count) print "<hr>";
}
?>[/code]

'messy' method

[code]<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$sql = "SELECT * FROM news_site WHERE `show` = '1'";
$results = mysql_query($sql);
$data = mysql_fetch_array($results);
var_dump($data);
while ($data = mysql_fetch_array($results)) {
        print "<hr>";
    var_dump($data);
}
?>[/code]

I prefer the former as the code is neater but the latter is probably (very slightly) more efficient in terms of resource use as there is no evaluaion of an if statement or the looping increment of $i

Have fun ;)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.