How Can I fillup bootstrap grid using foreach?










0














As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.



Note: Not issue with CSS but first three items are goes in grid as picture.



Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?



Any other way to workaround on this layout? Tried so far as below



 <?php $counter = 0;
foreach($view->result as $result) {

<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body

</div>
</div>

<?php if( $counter == 1 || $counter == 2 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1) ?>
<div class="post post-thumb">
// second element common body
</div>

</php if ( $counter == 2)
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; ?>
</div> //closing row
<?php ?> //closing foreach


enter image description here










share|improve this question





















  • You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
    – miken32
    Nov 12 '18 at 21:25
















0














As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.



Note: Not issue with CSS but first three items are goes in grid as picture.



Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?



Any other way to workaround on this layout? Tried so far as below



 <?php $counter = 0;
foreach($view->result as $result) {

<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body

</div>
</div>

<?php if( $counter == 1 || $counter == 2 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1) ?>
<div class="post post-thumb">
// second element common body
</div>

</php if ( $counter == 2)
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; ?>
</div> //closing row
<?php ?> //closing foreach


enter image description here










share|improve this question





















  • You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
    – miken32
    Nov 12 '18 at 21:25














0












0








0







As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.



Note: Not issue with CSS but first three items are goes in grid as picture.



Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?



Any other way to workaround on this layout? Tried so far as below



 <?php $counter = 0;
foreach($view->result as $result) {

<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body

</div>
</div>

<?php if( $counter == 1 || $counter == 2 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1) ?>
<div class="post post-thumb">
// second element common body
</div>

</php if ( $counter == 2)
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; ?>
</div> //closing row
<?php ?> //closing foreach


enter image description here










share|improve this question













As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.



Note: Not issue with CSS but first three items are goes in grid as picture.



Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?



Any other way to workaround on this layout? Tried so far as below



 <?php $counter = 0;
foreach($view->result as $result) {

<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body

</div>
</div>

<?php if( $counter == 1 || $counter == 2 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1) ?>
<div class="post post-thumb">
// second element common body
</div>

</php if ( $counter == 2)
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; ?>
</div> //closing row
<?php ?> //closing foreach


enter image description here







php for-loop foreach twitter-bootstrap-2 col






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 20:06









Nisarg

1,33931123




1,33931123











  • You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
    – miken32
    Nov 12 '18 at 21:25

















  • You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
    – miken32
    Nov 12 '18 at 21:25
















You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
– miken32
Nov 12 '18 at 21:25





You may want to look at this to make your code easier to read when mixing PHP and HTML like you're doing here: php.net/manual/en/control-structures.alternative-syntax.php
– miken32
Nov 12 '18 at 21:25













1 Answer
1






active

oldest

votes


















1














There are couple of syntax errors in your code, such as:



  • Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly


  • counter++; should be $counter++;

Plus, there are few logical errors as well. Change your code in the following way,



<?php 
$counter = 0;
foreach($view->result as $result) $counter % 3 == 0 ) ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1) ?>
<div class="post post-thumb">
// second element common body
</div>
<?php ?>
<?php if ( $counter % 3 == 2) ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php ?>
</div>
<?php

++$counter;

?>





share|improve this answer




















  • Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
    – Nisarg
    Nov 12 '18 at 20:54







  • 1




    @Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
    – Rajdeep Paul
    Nov 12 '18 at 21:01











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269345%2fhow-can-i-fillup-bootstrap-grid-using-foreach%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are couple of syntax errors in your code, such as:



  • Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly


  • counter++; should be $counter++;

Plus, there are few logical errors as well. Change your code in the following way,



<?php 
$counter = 0;
foreach($view->result as $result) $counter % 3 == 0 ) ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1) ?>
<div class="post post-thumb">
// second element common body
</div>
<?php ?>
<?php if ( $counter % 3 == 2) ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php ?>
</div>
<?php

++$counter;

?>





share|improve this answer




















  • Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
    – Nisarg
    Nov 12 '18 at 20:54







  • 1




    @Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
    – Rajdeep Paul
    Nov 12 '18 at 21:01
















1














There are couple of syntax errors in your code, such as:



  • Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly


  • counter++; should be $counter++;

Plus, there are few logical errors as well. Change your code in the following way,



<?php 
$counter = 0;
foreach($view->result as $result) $counter % 3 == 0 ) ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1) ?>
<div class="post post-thumb">
// second element common body
</div>
<?php ?>
<?php if ( $counter % 3 == 2) ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php ?>
</div>
<?php

++$counter;

?>





share|improve this answer




















  • Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
    – Nisarg
    Nov 12 '18 at 20:54







  • 1




    @Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
    – Rajdeep Paul
    Nov 12 '18 at 21:01














1












1








1






There are couple of syntax errors in your code, such as:



  • Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly


  • counter++; should be $counter++;

Plus, there are few logical errors as well. Change your code in the following way,



<?php 
$counter = 0;
foreach($view->result as $result) $counter % 3 == 0 ) ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1) ?>
<div class="post post-thumb">
// second element common body
</div>
<?php ?>
<?php if ( $counter % 3 == 2) ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php ?>
</div>
<?php

++$counter;

?>





share|improve this answer












There are couple of syntax errors in your code, such as:



  • Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly


  • counter++; should be $counter++;

Plus, there are few logical errors as well. Change your code in the following way,



<?php 
$counter = 0;
foreach($view->result as $result) $counter % 3 == 0 ) ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1) ?>
<div class="post post-thumb">
// second element common body
</div>
<?php ?>
<?php if ( $counter % 3 == 2) ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php ?>
</div>
<?php

++$counter;

?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 20:44









Rajdeep Paul

15.9k31229




15.9k31229











  • Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
    – Nisarg
    Nov 12 '18 at 20:54







  • 1




    @Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
    – Rajdeep Paul
    Nov 12 '18 at 21:01

















  • Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
    – Nisarg
    Nov 12 '18 at 20:54







  • 1




    @Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
    – Rajdeep Paul
    Nov 12 '18 at 21:01
















Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
– Nisarg
Nov 12 '18 at 20:54





Great! that works. due to copy-paste forgot to add Opening and closing braces but the main issue is counter++ and if condition
– Nisarg
Nov 12 '18 at 20:54





1




1




@Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
– Rajdeep Paul
Nov 12 '18 at 21:01





@Nisarg, Glad I could help! :-) I don't think the suggested edit is necessary, given the current problem at hand.
– Rajdeep Paul
Nov 12 '18 at 21:01


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269345%2fhow-can-i-fillup-bootstrap-grid-using-foreach%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3