1

Hi so I am fairly new at getting into php and sql so this might be a very simple problem but I cant seem to figure it out.

I need to pull all the data from our staff table and then list it on our page. This would be very simple if it was just going down the page but for every 4 staff members that are pulled I need to close the div and reopen a new div and continue pulling the members. I'm not sure on how to code this and still be able to open and close the divs. Here is what I have right now which just pulls the same staff member 4 times and then moves on to the next staff member in the next row. Thanks in advance for the help!

<?php
$sql = mysql_query("SELECT * FROM staff ORDER BY id"); 
$array = array();


while ($row = mysql_fetch_assoc($sql)) { ?>
<div class="row staff-padding">
    <div class="col-sm-3">
        <img src="<?php echo $row[img_link]; ?>" class="img-thumbnail img-staff"/>
        <h4><?php echo $row[name]; ?></h4>
        <p><?php echo $row[job_title]; ?><br />
            <span class="glyphicon glyphicon-phone-alt"></span> <?php echo $row[phone_number]; ?> <br />
            <span class="glyphicon glyphicon-envelope"></span> <a href="<?php echo $row[email]; ?>"><?php echo $row[email]; ?></a><br />
        </p>
    </div>

    <div class="col-sm-3">
        <img src="<?php echo $row[img_link]; ?>" class="img-thumbnail img-staff"/>
        <h4><?php echo $row[name]; ?></h4>
        <p><?php echo $row[job_title]; ?><br />
            <span class="glyphicon glyphicon-phone-alt"></span> <?php echo $row[phone_number]; ?> <br />
            <span class="glyphicon glyphicon-envelope"></span> <a href="<?php echo $row[email]; ?>"><?php echo $row[email]; ?></a><br />
        </p>
    </div>
    <div class="col-sm-3">
        <img src="<?php echo $row[img_link]; ?>" class="img-thumbnail img-staff"/>
        <h4><?php echo $row[name]; ?></h4>
        <p><?php echo $row[job_title]; ?><br />
            <span class="glyphicon glyphicon-phone-alt"></span> <?php echo $row[phone_number]; ?> <br />
            <span class="glyphicon glyphicon-envelope"></span> <a href="<?php echo $row[email]; ?>"><?php echo $row[email]; ?></a><br />
        </p>
    </div>
    <div class="col-sm-3">
        <img src="<?php echo $row[img_link]; ?>" class="img-thumbnail img-staff"/>
        <h4><?php echo $row[name]; ?></h4>
        <p><?php echo $row[job_title]; ?><br />
            <span class="glyphicon glyphicon-phone-alt"></span> <?php echo $row[phone_number]; ?> <br />
            <span class="glyphicon glyphicon-envelope"></span> <a href="<?php echo $row[email]; ?>"><?php echo $row[email]; ?></a><br />
        </p>
    </div>
</div>
3
  • possible duplicate of Split table rows into columns Commented Jun 23, 2015 at 17:48
  • WARNING: If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs. Commented Jun 23, 2015 at 19:58
  • Thanks I do plan on learning this method in the near future becuase when I was doing research on how to do stuff it always came back with PDO stuff. However the schools server I am currently working on doesn't seem to support it yet. Commented Jun 24, 2015 at 14:11

2 Answers 2

2

You need to implement the while loop to single iterations over each employee and add an incremented counter variable that keeps track of the iterations. Inside the loop you'll have two if statements, when the counter variable is divisible by 4 evenly, you open and close the divs.. Something like (pseudo code):

counter=0;
while(records_still_exist){

if(counter%4==0){

// open div code
}

...employee stuff...

if(counter%4==0){

// close div code
}

counter ++;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a fairly clean way of doing it.

<?php ?>foreach(array_chunk($memebers->all(), 4) as $row) ?>
    <div class="extra div u want to add every 4 members">
        <?php foreach($row as $memebers)

        endforeach ?>
    </div>
<?php endforeach ?>

Comments

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.