Jump to content

How to grab first MySQL row


Bricktop

Recommended Posts

Hi guys and gals,

 

Bit of a question for you. I have built a gallery script and everything works just great.

 

However, I am using a third-party Javascript tool to display the images. It takes images in an unordered list and outputs them.

 

But, the first image it displays needs <li class="active"> to highlight that image when the page loads. All other images just have <li> preceeding them.

 

So, my code goes:

 

$sql = mysql_query("SELECT * FROM gallery ORDER BY id");

	$cntavs=0;

	while ($a = mysql_fetch_array($sql)) 
	{

$content .= '<li><img src="'.$a['path'].''.$a['filename'].'" alt="'.str_replace('"','"',$a[description]).'" title="'.str_replace('"','"',$a[description]).'" /></a></li>';

}

 

The above code works fine, but obviously the first image in the database does not have the <li class="active"> applied to it.

 

I hope I have made myself clear, any idea on how I can make the first output have the active class applied?

 

Thanks in advance!

 

This

Link to comment
https://forums.phpfreaks.com/topic/100100-how-to-grab-first-mysql-row/
Share on other sites

$sql = mysql_query("SELECT * FROM gallery ORDER BY id");

	$cntavs=0;
	$firstRow = true;
	while ($a = mysql_fetch_array($sql)) 
	{
$class = $firstRow ? ' class="active"' : '';
$content .= '<li' . $class . '><img src="'.$a['path'].''.$a['filename'].'" alt="'.str_replace('"','"',$a[description]).'" title="'.str_replace('"','"',$a[description]).'" /></a></li>';
$firstRow = false;
}

 

 

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.