Jump to content

help with loop


croakingtoad

Recommended Posts

Hello,

 

I have written a basic loop-

$row = mysql_num_rows($query);

for ($i = 0; $i< $row; $i++) {
	$record = mysql_fetch_row($query);
	if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") {
		echo "$record[1]" . " " . "$record[2]" . "<br />"; }
}

 

What I think I need is a loop within a loop because what I want to end up with is something like--

<div class="container">
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 1</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 2</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 3</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 4</div>	
</div>

<div class="container">
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 1</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 2</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 3</div>
<div class="associate">
	<img src="**" width="147" height="220" /><br />div 4</div>	
</div>

 

The details being pulled in from the database are marked with an asterisk above.  I need the container div to only be echoed once every 4 iterations.  Does that make sense?  How would I take what I have and get that? 

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/42359-help-with-loop/
Share on other sites

you want to do something like:

 

$row = mysql_num_rows($query);

echo '<div class="container">';
for ($i = 0; $i< $row; $i++) 
{
  $record = mysql_fetch_row($query);
  if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") 
  {
    if( $new_div )
      echo '</div><div class="container">';
    echo "$record[1]" . " " . "$record[2]" . "<br />"; }
  }
}

echo '</div>';

 

But you will need to figure out the $new_div bit (call a function or something)

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/42359-help-with-loop/#findComment-205488
Share on other sites

Here is what I have come up with that is very close to working, I'm just having one problem-

$row = mysql_num_rows($query);

for ($i = 0; $i< $row; $i++) {
	$record = mysql_fetch_row($query);
	if ($record[0] !== "12" && $record[0] !== "20" && $record[0] !== "10") {
		if (is_int($i / 4)) {
			echo "<div class=\"container\" id=\"$i\">";
		}
		echo "<div class=\"associate\">
				<img src=\"\" width=\"147\" height=\"220\" /></div>
		";
		//echo "$row $record[1]" . " " . "$record[2]" . "<br />"; 
		}
		if (is_int($i / 4)) {
			echo "</div>";
		}
}

 

In short what this should be doing is determining if the value of $i divided by 4 is an integer.  If it is, then it should echo out my container div with it's closing tag.  This seems to be working okay except that when it calculates iteration '4' it is returning false.  The answer is 1 with no decimals so I'm not sure why it's failing that count.  Here is the subsequent output I am getting-

<div class="container" id="0">
<div class="associate">
    <img src="" width="147" height="220" /></div>
</div>  (WHY IS THIS DIV HERE?)
<div class="associate">				
    <img src="" width="147" height="220" /></div>
<div class="associate">
    <img src="" width="147" height="220" /></div>
<div class="associate">
    <img src="" width="147" height="220" /></div>
</div>
<div class="associate">
    <img src="" width="147" height="220" /></div>
<div class="associate">
    <img src="" width="147" height="220" /></div>

<div class="container" id="8">
<div class="associate">
    <img src="" width="147" height="220" /></div>
</div>  (WHY IS THIS HERE?)
<div class="associate">
     <img src="" width="147" height="220" /></div>
<div class="associate">
     <img src="" width="147" height="220" /></div>
<div class="associate">
     <img src="" width="147" height="220" /></div>

<div class="container" id="12">
<div class="associate">
    <img src="" width="147" height="220" /></div>
</div>  (WHY IS THIS HERE?)
<div class="associate">
    ETC ETC...

 

I have put in the container divs an id that is the value of $i.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/42359-help-with-loop/#findComment-205547
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.