Jump to content

Last row different


theone

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
Link to comment
https://forums.phpfreaks.com/topic/6939-last-row-different/
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 ;)
Link to comment
https://forums.phpfreaks.com/topic/6939-last-row-different/#findComment-25261
Share on other sites

Archived

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

×
×
  • Create New...

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.