Jump to content

Recommended Posts

hi all what im trying to do is

 

say $DogField = "german shepard,shitzu,husky, doberman, Lindsey Lohan";

 

how would i get that variable into

<ul>

<li>German Shepard</li>

<li>Shitzu</li>

<li>Husky</li>

</ul>

<ul>

<li>Doberman</li>

<li>Lindesy Lohan</li>

</ul>

 

so that every three <li>s a new list is created

 

my idea was something like str_replace(",","</li><li>",$Dogfield);

<?php
$dogArray = explode(",",$DogField);

$i=1;
foreach($dogArray as $dog) {
  if($i == 1) { echo "<ul>"; }
  echo "<li>" . $dog . "</li>";
  if($i == 3) {
     echo "</ul>";
     $i = 0;
   }
   $i++;
}
if($i ==2) { echo "</ul>"; }
?>

Here's another way:

 

<?php
$DogField = "german shepard,shitzu,husky, doberman, Lindsey Lohan";
$dogs = array_map('trim',explode(',',$DogField));
$st = 0;
$len = 3;
$part = array_slice($dogs,$st,$len);
while (!empty($part)) {
        echo '<ul><li>' . implode('</li><li>',$part) . "</li></ul>\n";
        $st += $len;
        $part = array_slice($dogs,$st,$len);
}
?>

 

Using the $len variable, you can easily change the number of elements in the generated lists.

 

Ken

who's method did you use ? I dont see how mine could have two <li> tags in a row

 

then again, neither of our methods would have all <li> tags without closing any of them, so maybe you just didn't copy/paste exactly what you saw

 

regardless, kenrbnsn's method is probably more dynamic

i used both, both of them display

 

<ul><li>Architects / Designers</li><li></li></ul>

 

the field is trades and contains this data

Architects / Designers,

 

there is not trailing spaces either

 

what you think it is?

Remove the trailing comma before exploding. The trailing comma is causing an empty entry in the array.

<?php
$str = "Architects / Designers,";
$ary = array_map('trim',explode(',',rtrim($str,',')));
?>

 

Ken

well heres the actual code, for what im using it for

 

EDIT: no sorry its displaying 1 blank <li>now

 

<?php
$TradesField = $row["Sect1_6"];
$trades = array_map('trim',explode(',',rtrim(',',$TradesField)));
$st = 0;
$len = 3;
$part = array_slice($trades,$st,$len);
while (!empty($part)) {
        echo '<ul><li>' . implode('</li><li>',$part) . "</li></ul>\n";
        $st += $len;
        $part = array_slice($trades,$st,$len);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.