2 Replies - 4482 Views - Last Post: 01 April 2008 - 10:58 PM Rate Topic: -----

#1 Myrilith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 08

Select specific row during While loop?

Posted 27 March 2008 - 01:44 PM

I'm working on an album-oriented picture gallery, and I've had a little snag that I can't seem to find addressed in any tutorials. Google has failed me, so I hope DiC can help!

What I'm trying to do is display all the pictures in a specific album, but I want to show exactly three across in a row. I'm using a while loop to check for pics (code below), but I don't know how to cycle that while loop so that three pics are on the same row and every fourth starts a new one.

How can I set this up using PHP, CSS, etc. so that I have exactly three columns per row, and as many rows as necessary? Here's what I've got so far:

$result_pics = mysql_query("select * from pics where pic_album='" . $album_name . "'");

while ($row_pics = mysql_fetch_array($result_pics)) {
  echo "<div class='pic_cont'>" . 
	$row_pics['pic_filename'] . "
	</div>
	";
}



Is there a way to specify which $row_pics rows are selected? Something like $row_pics[1]['pic_filename'] so that I could then use an increasing integer? This is what I'm trying to do, but can't get to work:

$i = '0';
while ($row_pics = mysql_fetch_array($result_pics)) {
  echo "<div class='pic_cont1'>" . 
	$row_pics[i]['pic_filename'] . "
	</div>";
  $i = $i + 1;
  echo "<div class='pic_cont2'>" . 
	$row_pics[i]['pic_filename'] . "
	</div>";
  $i = $i + 1;
  echo "<div class='pic_cont3'>" . 
	$row_pics[i]['pic_filename'] . "
	</div>
	<br>";
  $i = $i + 1;
}



I need to get that line-break after every third pic, and only after every third pic. How?

Is This A Good Question/Topic? 0
  • +

Replies To: Select specific row during While loop?

#2 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Select specific row during While loop?

Posted 27 March 2008 - 01:57 PM

just compute the mod of the counter i and make a break.
So:
$i = 0;
while($i < count($record_set)){
	if(($i % 3) == 0){
	  echo "<br/>";
	}
	//then display picture
	echo "<div pictureID".$i.">";
	//display picture
   echo "</div>";
   $i = $i + 1;
}


of course you'll need to expand on this alittle to make it work with your code, but the mod (%) is the thing to take from this.
Was This Post Helpful? 0
  • +
  • -

#3 deepamanohar  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-March 08

Re: Select specific row during While loop?

Posted 01 April 2008 - 10:58 PM

Mod works perfect to split the values with three records each and the code works fine :)

but when we use a div to display the records we have to apply the style sheet to display as a row haveing 3 records each.

in your code add a style for the div and in the <br> tag use a style='clear:both'.as this will not allow to float the elements in the left or right side. I have integrated the sample code with the styles below.

<style type="text/css">
.row_block{
	float: left;
	padding: 2px;
	margin: 1px;
   
}
</style>

<?
$i = 0;
while($i < 10){
	if(($i % 3) == 0){
	  echo "<br style='clear:both' />";
	}
	//then display picture
	echo "<div id='pictureID$i' class='row_block'>";
	print  "row".$i."  ";
	//display picture
   echo "</div>";
   $i = $i + 1;
}
?>



Now you can just place your code with your requirements and you can see a line break after every third pic :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1