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
https://forums.phpfreaks.com/topic/252185-while-loop-help/
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
https://forums.phpfreaks.com/topic/252185-while-loop-help/#findComment-1292927
Share on other sites

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.