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

 

Link to comment
Share on other sites

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

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