White_Lily Posted March 22, 2013 Share Posted March 22, 2013 Hey, I have a database full of "Depots" and basically instead of just one straight contact form on this clients website, they want a list of each of their depots on the contact page instead with individual phone numbers and locations, etc. What I was wondering is, is there a way with php where I can basically say: "For every 4 entries listed, put one large div around them"? So far there are 13 depots listed, but there are 4 depots for every row on their website. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/ Share on other sites More sharing options...
gmaestroo Posted March 22, 2013 Share Posted March 22, 2013 When you loop though the results, you should add a condition and incriment variable. Something like this : $i = 0; <?php foreach($db_res as $value) : ?> <?php if($i == 4) : ?> <?php $ = 0; ?> <div class="something"><?php echo $value->something; ?></div> <?php else : ?> <?php echo $value->something; ?> <?php endif; ?> <?php endforeach; ?> Sorry about ugly formatting, this is one bad editor Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/#findComment-1420302 Share on other sites More sharing options...
DaveyK Posted March 22, 2013 Share Posted March 22, 2013 (edited) Or you can try doing it in one clean script. This is somewhat the same script as gmaestroo's but... cleaner <?php $i = 0; foreach($db_res as $value) { if ($i == 0) { echo '<div class="something">'; } echo $value['some_key_name']; if ($i == 4) { echo '</div>'; $i = 0; // reset to 0 for future rows. } ++$i; } ?> I havent tested the script but the logic is there. Also, if the number of rows is not exactly x*4 this script will fail to add the last </div>. Im sure you can figure that out. Edited March 22, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/#findComment-1420303 Share on other sites More sharing options...
gmaestroo Posted March 23, 2013 Share Posted March 23, 2013 Thank you Davey, I have forgot to put $i++ Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/#findComment-1420480 Share on other sites More sharing options...
PaulRyan Posted March 23, 2013 Share Posted March 23, 2013 Both answers lacked a check if the results are not a multiple of 4. If you put 13 results, you get an open div round the last result, but not a closing one. Try this: <?PHP $num = 0; $output = ''; foreach($db_res as $value) { $num++; //### Open div if($num%4==1) { $output .= '<div>'; } //### Add value to output $output .= $value; //### Close div if($num%4 == 0) { $output .= '</div>'; } } //### If total results is not multiple of 4, close the final div if($num%4 != 0) { $output .= '</div>'; } //### Display output echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/#findComment-1420494 Share on other sites More sharing options...
PaulRyan Posted March 23, 2013 Share Posted March 23, 2013 I havent tested the script but the logic is there. Also, if the number of rows is not exactly x*4 this script will fail to add the last </div>. Im sure you can figure that out. I totally missed this, I don't know how I manage to do that. Davey pointed out the error for the final div closing, kudos. Quote Link to comment https://forums.phpfreaks.com/topic/276004-putting-a-div-around-every-4-database-items/#findComment-1420495 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.