Jump to content


Photo

Last row different


  • Please log in to reply
2 replies to this topic

#1 theone

theone
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 April 2006 - 01:48 PM

Right...

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

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
theone

#2 theone

theone
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 April 2006 - 02:00 PM

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

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 09 April 2006 - 05:13 PM

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

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

'messy' method

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

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 ;)
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users