atlanta Posted February 6, 2008 Share Posted February 6, 2008 Hi all, I wanted to know what function i should use to make a if statement that will retrue if the number is a multiple of 4 for instance ex. if($number == "0" || multiple of 4 function) { echo "<tr>"; } Thanks In advance Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/ Share on other sites More sharing options...
awpti Posted February 6, 2008 Share Posted February 6, 2008 <?php if($number == 0 OR ($number %4)) { //.... } else { // ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459534 Share on other sites More sharing options...
atlanta Posted February 6, 2008 Author Share Posted February 6, 2008 That didnt worked it echoed on every number.. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459536 Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 if($num % 4 == 0) { // do something } Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459542 Share on other sites More sharing options...
laffin Posted February 6, 2008 Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459543 Share on other sites More sharing options...
atlanta Posted February 6, 2008 Author Share Posted February 6, 2008 Thanks Guys i got it working ! Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-459767 Share on other sites More sharing options...
atlanta Posted February 7, 2008 Author Share Posted February 7, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460364 Share on other sites More sharing options...
Zane Posted February 7, 2008 Share Posted February 7, 2008 if ($cnt == "0" || ($cnt % 4) == 0) this has to be in a loop somewhere for $cnt to even be changing... or else it will only work as soon as it loads so to answer your question....change $cnt. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460375 Share on other sites More sharing options...
atlanta Posted February 7, 2008 Author Share Posted February 7, 2008 oh sorry it is in the look which is above it somewhat for ($cnt = $start; $cnt < $end; $cnt++) { $file = $files[$cnt]; Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460379 Share on other sites More sharing options...
Zane Posted February 7, 2008 Share Posted February 7, 2008 for every count of $cnt? just echo it...it's already in the loop.... if you just echo it....it'll be echoed to the end. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460382 Share on other sites More sharing options...
atlanta Posted February 7, 2008 Author Share Posted February 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460400 Share on other sites More sharing options...
Zane Posted February 7, 2008 Share Posted February 7, 2008 mix this up a little bit if ($cnt == "0" || ($cnt % 4) == 0) { echo ""; } to this if($cnt == 0 || !($cnt % 4)) echo ""; if(($cnt % 4) != false) echo "\n"; Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460419 Share on other sites More sharing options...
atlanta Posted February 7, 2008 Author Share Posted February 7, 2008 I tried the one u suggested but it puts every picture on a new row.. i want 4 on a row then </tr> then <tr> and start the next 4 Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460430 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460434 Share on other sites More sharing options...
haku Posted February 7, 2008 Share Posted February 7, 2008 You are going to kick yourself for not realizing this. Change this: if ($cnt == "0" || ($cnt % 4) == 0) { echo "<tr>"; } To this: if ($cnt == "0" || ($cnt % 4) == 0) { echo "</tr><tr>"; } This will end the previous row and start the next one. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460590 Share on other sites More sharing options...
Zane Posted February 7, 2008 Share Posted February 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89677-multiple-of-4/#findComment-460625 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.