applebiz89 Posted October 22, 2010 Share Posted October 22, 2010 I have this function, but it doesnt work. Basically the list outputs a list of news features, and puts a line under neath each article to seperate them. However once the last article has been reached, I don't want the break to be shown. this is my code, any help would be appreciated as i am really struggling on this. Just need to determine the last item in the array, and stop the <hr split> from being output. function newsFeature(){ $event = "SELECT imgname, title, content, pkID from news ORDER BY date, time DESC LIMIT 5"; $event_page = query($event); $count = numRows($event_page); while ($row = mysql_fetch_assoc($event_page)) { $rowcount = $count; $para = substr($row['content'] , 0,100); $result .= '<article>'; $result .= '<a href="news-article.php?pkID=' . $row['pkID'] . '"><img src="uploads/thumbs/' . $row['imgname'] . '" alt="" width=116 height=83/ ></a>'; $result .= '<h2><a href="news-article.php?pkID=' . $row['pkID'] . '">' . $row['title'] . '</a></h2>'; $result .= '<p>' . $para . '...</p>'; $result .= '<p><a class="read-more" href="news-article.php?pkID=' . $row['pkID'] . '">Read more<span></span></a></p>'; $result .= '</article>'; if($row != end($row[])){ $result .= '<hr class="split">'; } } return $result; } Link to comment https://forums.phpfreaks.com/topic/216558-how-to-determine-the-last-item-in-an-array-that-is-being-looped-help/ Share on other sites More sharing options...
litebearer Posted October 22, 2010 Share Posted October 22, 2010 Perhaps... function newsFeature(){ $event = "SELECT imgname, title, content, pkID from news ORDER BY date, time DESC LIMIT 5"; $event_page = query($event); $last_row = 1; $count = numRows($event_page); while ($row = mysql_fetch_assoc($event_page)){ $para = substr($row['content'] , 0,100); $result .= '<article>'; $result .= '<a href="news-article.php?pkID=' . $row['pkID'] . '"><img src="uploads/thumbs/' . $row['imgname'] . '" alt="" width=116 height=83/ ></a>'; $result .= '<h2><a href="news-article.php?pkID=' . $row['pkID'] . '">' . $row['title'] . '</a></h2>'; $result .= '<p>' . $para . '...</p>'; $result .= '<p><a class="read-more" href="news-article.php?pkID=' . $row['pkID'] . '">Read more<span></span></a></p>'; $result .= '</article>'; if($last_row<$count) { $result .= '<hr class="split">'; $last_row ++; } } return $result; } Link to comment https://forums.phpfreaks.com/topic/216558-how-to-determine-the-last-item-in-an-array-that-is-being-looped-help/#findComment-1125189 Share on other sites More sharing options...
kenrbnsn Posted October 22, 2010 Share Posted October 22, 2010 Use explode with a temporary array: <?php function newsFeature(){ $event = "SELECT imgname, title, content, pkID from news ORDER BY date, time DESC LIMIT 5"; $event_page = query($event); $count = numRows($event_page); $tmp = array(); while ($row = mysql_fetch_assoc($event_page)){ $para = substr($row['content'] , 0,100); $result = '<article>'; $result .= '<a href="news-article.php?pkID=' . $row['pkID'] . '"><img src="uploads/thumbs/' . $row['imgname'] . '" alt="" width=116 height=83/ ></a>'; $result .= '<h2><a href="news-article.php?pkID=' . $row['pkID'] . '">' . $row['title'] . '</a></h2>'; $result .= '<p>' . $para . '...</p>'; $result .= '<p><a class="read-more" href="news-article.php?pkID=' . $row['pkID'] . '">Read more<span></span></a></p>'; $result .= '</article>'; $tmp[] = $result; } return explode('<hr class="split">',$tmp); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/216558-how-to-determine-the-last-item-in-an-array-that-is-being-looped-help/#findComment-1125242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.