0

I was able to retrieve data from Mysql database however every 4 column I need to close row and start and new row Following is my HTML code that I am trying to loop through columns and after every 4 columns close the row and keep adding columns from database. enter image description here

Here is my code (I know this is not PDO and I am trying to learn so I can convert to PDO MySQLi connection)

 <div class="row">
    <div class="row margin-bottom-20">

    <?php
       include('dbconnect.php');
       $query = "SELECT * FROM selection"; 
       mysql_set_charset("UTF8");
       $result = mysql_query($query) or die(mysql_error());

    for($i=1; $row = mysql_fetch_array($result); $i++){
    ?>
             <div class="col-md-3">

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
                        </div>
                        <div class="panel-body">

                          <p>
                             <?php echo $row['ContactInfo']; ?><br/>
                             <img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
                          </p>
                        </div>
                    </div>

                </div>


             <?php
                }
             ?>  

I need to add a new for loop but I haven't been successful so far. Thank you for your help.

4
  • Your image of text isn't very helpful. It can't be copied into an editor, and it doesn't index very well, making other users with the same problem less likely to find the answer here. Please edit your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). Commented Apr 4, 2016 at 17:21
  • Sorry Toby. I will do a better job next time. Commented Apr 5, 2016 at 17:38
  • It is not fully related to the question, but I see you are using bootstrap, and you want to display 4 columns each row. So you don't actually need to create a new <div class="row"> every four elements, you just need one and add all next elements inside the <div class="col-md3"> Bootstrap will take care of the rest Commented Apr 6, 2016 at 7:12
  • Borjante when I did that there was a white space in between the elements. I may have done something wrong too. Thank you for your recommendation. I will try test it again. Commented Apr 6, 2016 at 19:22

2 Answers 2

1
if($i%4==0)
{
    //close the existing div and start new row div here
}

That will start a new row after every 4 iterations

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks hanky Panky :) I added following code and it worked. <?php if($i%4==0) { //close the existing div and start new row div here echo "</div>"; } } ?>
0

Try this

<?php
include('dbconnect.php');
$query = "SELECT * FROM selection"; 
mysql_set_charset("UTF8");
$res = mysql_query($query) or die(mysql_error());

while($rows = mysql_fetch_array($res)){
    $result[] = $rows;
}

$array = array_chunk($result, 4);

foreach ($array as $value) {
    foreach ($value as $row){
         ?>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
                </div>
                <div class="panel-body">
                    <p>
                        <?php echo $row['ContactInfo']; ?><br/>
                        <img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
                    </p>
                </div>
            </div>
        </div>                      
<?php 
    }
}

1 Comment

Numan I will test your solution and see if your solution works as well. Thank you for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.