1

I would like to add ul li html element for every 3 results in foreach using php. I have tried with the following method. but i am not getting the exact results. please advise on this

 Array ( [0] => stdClass Object ( [category_name] => Architect ) 
 [1] => stdClass Object ( [category_name] => Doors & Windows ) 
 [2] => stdClass Object ( [category_name] => Garage Doors ) 
 [3] => stdClass Object ( [category_name] => Home Inspection ) )



      <?php 
                    $i=0;
                     //$arrays = array_chunk($get_business_cat_details, 3);
                    foreach($get_business_cat_details as $key=> $cat_name){                                                         

                                //echo " <ul style='margin-top: 20px;'><li><a href='#'>".ucwords($cat_name->category_name) ."</a></li>";
                                if($i%3==0){

                                    echo "<ul><li><a href='#'>".ucwords($cat_name->category_name) ."</a></li></ul>";

                                }else{
                                    echo "<ul><li><a href='#'>".ucwords($cat_name->category_name) ."</a></li></ul>";
                                }
                                $i++;

                        }


                    ?>

Output:

 Power -- Wash --  Cleaning Paint 

East Valley --  Central/South Phx --  West Valley 
1
  • Post your array along with expected output. You can use for($i = 0; $i < count($your_array); $i += 3;){ //your code} Commented Nov 21, 2015 at 11:16

2 Answers 2

1

Please try below code.

    <?php

$i = 0;
//$arrays = array_chunk($get_business_cat_details, 3);
foreach ($get_business_cat_details as $key => $cat_name) {

    //echo " <ul style='margin-top: 20px;'><li><a href='#'>".ucwords($cat_name->category_name) ."</a></li>";
    if($i==0) {
        $get_style="style='margin-top: 20px;'";
    } else {
        $get_style="";
    }

    if ($i % 3 == 0) {
        echo "<ul ".$get_style." >";
    }

    echo "<li><a href='#'>" . ucwords($cat_name->category_name) . "</a></li>";

    $i++;
    if ($i % 3 == 0 && $i != 0) {
        echo "</ul>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

@markatteteam Can you please send me what type of result you get ?
Yes getting the result but how to add margin-top:20px for the first results alone
@markatteteam did you mean add this "<ul style='margin-top: 20px;'>" only for first result (first <ul>) ?
@markatteteam Replace this code please <?php $i = 0; //$arrays = array_chunk($get_business_cat_details, 3); foreach ($get_business_cat_details as $key => $cat_name) { //echo " <ul style='margin-top: 20px;'><li><a href='#'>".ucwords($cat_name->category_name) ."</a></li>"; if($i==0) { $get_style="style='margin-top: 20px;'"; } else { $get_style=""; } if ($i % 3 == 0) { echo "<ul ".$get_style." >"; } echo "<li><a href='#'>" . ucwords($cat_name->category_name) . "</a></li>"; $i++; if ($i % 3 == 0 && $i != 0) { echo "</ul>"; } } ?>
@markatteteam Sorry, by mistake entered press so comment was post. Please see my answer also i have made changed.
|
0

I think this may be what your looking for. Since your initial tag is in your loop every result would essentially be a complete list. By removing the from the loop you can close the tag and open a new one dynamically in the loop.

    <? php
    $i = 0;
  echo "<ul>";
   foreach($get_business_cat_details as $key => $cat_name) {
     if ($i % 3 == 0) {
       echo "</ul><ul>";
     }
     echo "<li><a href='#'>".ucwords($cat_name - > category_name).
       "</a></li>";
     $i++;

   }
echo "</ul>";
    ?>

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.