4

I'm using a while loop to echo a PHP array :

$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);

if(mysqli_num_rows($result) > 0)
{
 echo'<section>
        <div class="container">
           <div class="row">
             <h2 class="bold">title</h2>
               <hr> ';

                while($row = mysqli_fetch_array($result))
                {
                 echo'
                <div class="col-sm-3 col-md-3 col-md-push-3">
                    <div class="portfolio-wrapper">   
                        <div class="portfolio-single">
                            <div class="portfolio-thumb">
                                <a href="'.$row['link']." target="_blank">                                                        <img src="'.$row['image'].'" class="img-responsive" alt="'.$row['alt']."></a>
                            </div>
                        </div>
                        <div class="portfolio-info" dir="rtl">
                            <a href="'.$row['link2']." target="_self"><h2>                     </h2>
                                <h6>'.$row['title'].</h6>
                            </a>
                        </div>
                    </div>
                </div>';

            }
                echo'
                </div>
                    </div>
                    </section';
            }   

            ?>

This code works fine for me but the problem is that i need to echo 4 different

for example :

   first will be: <div class="col-sm-3 col-md-3 col-md-push-9">
   second: <div class="col-sm-3 col-md-3 col-md-push-3">
   third: <div class="col-sm-3 col-md-3 col-md-pull-3">
   last: <div class="col-sm-3 col-md-3 col-md-pull-9">

any ideas how can i accomplish this structure using a while loop ?

2
  • Keep a counter variable, use it to determine which class to use then increment the counter variable for the next iteration of your loop. Commented Sep 22, 2016 at 11:22
  • Like robbie said. Something like this $array = array( '0' => 'col-sm-3 col-md-3 col-md-push-9' '1' => 'col-sm-3 col-md-3 col-md-push-3', '2' => 'col-sm-3 col-md-3 col-md-pull-3', '3' => 'col-sm-3 col-md-3 col-md-pull-9'); $count = 0; while(){ div... class=$array[$count]; ... count ++ } Done in notepad so not correct syntax but you get the idea Commented Sep 22, 2016 at 11:25

2 Answers 2

4

Use a counter, and put your classes into an array like this:

$classes = [
    "col-sm-3 col-md-3 col-md-push-9",
    "col-sm-3 col-md-3 col-md-push-3",
    "col-sm-3 col-md-3 col-md-pull-3",
    "col-sm-3 col-md-3 col-md-pull-9"
];

$i = 0;
while($row = mysqli_fetch_array($result)) {
    if ($i > 3) {
        $i = 0;
    }
    ?>
    <div class="<?php echo $classes[$i]; ?>">
        Your content comes here
    </div>
    <?php
    $i++;
}

NOTE

This is resets the counter, if there is no index like that in the array. Your script assumes, that you will get always 4 rows.

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

Comments

0
<?php
$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);
$i = 1;
$count = count($result);
if(mysqli_num_rows($result) > 0)
{
 echo'<section>
        <div class="container">
           <div class="row">
             <h2 class="bold">title</h2>
               <hr> ';

                while($row = mysqli_fetch_array($result))
                {
                if($i==1){echo '<div class="col-sm-3 col-md-3 col-md-push-9">';}
                elseif($i == $count){echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';}
                elseif($i%2 == 0){echo '<div class="col-sm-3 col-md-3 col-md-push-3">';}
                elseif($i%3 == 0){echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';}

                 echo'
                        <div class="portfolio-wrapper">   
                            <div class="portfolio-single">
                                <div class="portfolio-thumb">
                                    <a href="'.$row['link']." target="_blank">                                                        <img src="'.$row['image'].'" class="img-responsive" alt="'.$row['alt']."></a>
                                </div>
                            </div>
                            <div class="portfolio-info" dir="rtl">
                                <a href="'.$row['link2']." target="_self"><h2>                     </h2>
                                    <h6>'.$row['title'].</h6>
                                </a>
                            </div>
                        </div>
                    </div>';
                    $i =$i+1;
            }
                echo'
                </div>
                    </div>
                    </section';
            }   

?>

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.