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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.