Jump to content

While loop help


nsanford

Recommended Posts

I need to pull information from a db table for a slideshow on my page, and need a little help.

All of the {$image}'s need to be pulled out and displayed in a div#slider first, then if $caption is set to 1, the caption info is displayed in a div#htmlcaption after the images div#slider.

I have an example of the divs below the mysql query, but I'm not sure how to insert it into the while loop. 

 

TIA :)

 

<?php
$sql = mysql_query("SELECT * FROM slideshow");
if(mysql_num_rows($sql) > 0) {
while($row = mysql_fetch_assoc($sql)) {
	$slideID = $row['slideID'];
	$image = $row['image'];
	$caption = $row['caption'];
	$caption_h3 = $row['caption_h3'];
	$caption_content = $row['caption_content'];
	$caption_link = $row['caption_link'];
	$caption_link_text = $row['caption_link_text'];

}
}
?>

 

<div id="slider">
<img src="{$image}" alt="" title="#htmlcaption{$slideID}" />
        <img src="{$image}" alt="" title="#htmlcaption{$slideID}" />
        <img src="{$image}" alt="" title="#htmlcaption{$slideID}" />
</div>

<div id="htmlcaption{$slideID}" class="nivo-html-caption">
<h3>$caption_h3</h3>
<p>$caption_content</p>
if($caption_link != '') {
	<p><a href="{$caption_link}">{$caption_link_text}</a></p>	
}
</div>

Link to comment
Share on other sites

Something like this?

 

$sql = mysql_query("SELECT * FROM slideshow");
if(mysql_num_rows($sql) > 0) {
$images   = array();
$captions = array();

while($row = mysql_fetch_assoc($sql)) {
	$images[] = array(
		'slideID' => $row['slideID'],
		'image'   => $row['image']
	);

	if ($row['caption'] == 1) {
		$captions[] = array(
			'slideID'           => $row['slideID'],
			'caption_h3'        => $row['caption_h3'],
			'caption_content'   => $row['caption_content'],
			'caption_link'      => $row['caption_link'],
			'caption_link_text' => $row['caption_link_text']
		);
	}
}

echo '<div id="slider">';
foreach($images as $i)
{
	echo '<img src="' . $i['image'] . '" alt="" title="#htmlcaption{' . $i['slideID'] . '}" />';
}
echo '</div>';

if (!empty($captions)) {
	foreach($captions as $c)
	{
		echo '<div id="htmlcaption{' . $c['slideID'] . '} class="nivo-html-caption">
		  <h3>' . $c['caption_h3'] . '</h3>
		  <p>' . $c['caption_content'] . '</p>
		  ' . ($c['caption_link'] != '' ? '<p><a href="' . $c['caption_link'] . '">' . $caption_link_text . '</a>' : '');
	}
}
}

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.