Jump to content

how to determine the last item in an array that is being looped HELP!


applebiz89

Recommended Posts

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

 

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

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

 

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.