Jump to content

Multiple of 4


atlanta

Recommended Posts

different ways

always check if $num has a value (if 0 ya will get an error, divide by zero)

 

if($num && (($num/4)==intval($num/4))  { echo true; } else { echo false; }

 

or

 

if($num && !($num % 4)) { echo true; } else { echo false; }

 

I even think this one will work

if (!( 3 & $num)) { echo true; } else { echo false; }

 

note on the last one, no $num check is done. prolly the fastest as well, since no division is needed :) but dusn work on floats, just integers.

 

Link to comment
https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459543
Share on other sites

Hey another quick question ok i got the the <tr> to show on every multiple of 4 but how do i make the </tr> show on every count of 4 with starting at 0??

 

if ($cnt == "0" || ($cnt % 4) == 0)
{
echo "<tr>";
}
?>
<td>
       <a href="<?php echo $urlpath . $file; ?>" target="_blank" rel="lightbox[album]" title="<?=$exten[0]?>">
      <img src="<?php echo $urlpath . $file; ?>" alt="<?=$exten[0]?>" width="158" height="168" />
       </a>
</td>
<?
if (function??)
{
echo "</tr>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460364
Share on other sites

Here maybe this will help if i added all the code its kinda confusing without it

 

if(isset($_GET['folder']))
{
?>
<table class="phototable">
<?
for ($cnt = $start; $cnt < $end; $cnt++) {
    $file = $files[$cnt];

    $exten = explode('.',$file);

if ($cnt == "0" || ($cnt % 4) == 0)
{
echo "<tr>";
}
?>
<td>
       <a href="<?php echo $urlpath . $file; ?>" target="_blank" rel="lightbox[album]" title="<?=$exten[0]?>">
      <img src="<?php echo $urlpath . $file; ?>" alt="<?=$exten[0]?>" width="158" height="168" />
       </a>
</td>
<?
if ($cnt == "3" || $cnt == "7" || $cnt == "11")
{
echo "</tr>";
}
?>

<?
} // end for
?>
<?
} // end if

 

ok so what i want to do is have it eacho </tr> on every 4th count you get me ?

so that after the forth coloumn it will start a new row

Link to comment
https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460400
Share on other sites

lol

 

i think i said using the booleans was prolly easiest.

 

since 4 is a magic number, in the word of bits

 

<?php
  echo "<TABLE border=1>";
  for($i=0;$i<=20;$i++)
  {
if(!($i & 3)) echo "<tr>";
echo "<td>$i</td>";
if(($i & 3)==3) echo "</tr>";
  }
  if(($i&=3)) echo "<TD COLSPAN=". (4-$i) ."> </TD></TR>";
  echo "</table>";
  
?>

Link to comment
https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460434
Share on other sites

I guess I should have wrote mine like this

 

if(!($cnt % 4))
    echo "\n";
if($cnt == 0 || !($cnt % 4))
    echo "\n";

 

@haku...that pretty much is the answer, but then you'll have an unneeded

at the very beginning...causing a validation error.

 

 

Link to comment
https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460625
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.