Jump to content

php Explode and long, delimited lists


Joss
 Share

Recommended Posts

This is a little php tip that might come in useful for those who are new to this php stuff (like me)

Repeater fields are wonderful in ProcessWire, but if, like me today, you are faced with a list of a hundred odd items, then they can become a little overwhelming to use.

Sometimes all you want to do is put a list in a textarea and hope something wonderful will come of it.

Well, Explode in php is just the wonderful thing you might want. In this example I am creating a simple list where each list item has three parts - a name, a type and a colour.

Create a textarea field and add it to your template.

Create a page and in the textarea add your list - something like this, perhaps?

Ford Fiesta,1.3xl,red
Morris Marina,GT,blue
Hilman Hunter,1.6SE,green

There are two delimiters here - a new line between each car entry (yes, those are all cars) and a comma between each detail of the car.

On our template, we first grab our textarea and "explode" it to separate the lines into an array:

$cars = explode("\n", $page->my_textarea);

"\n" is the new line markup and we are identifying it within $page->my_textarea and then exploding at those points.

We now need to loop through out array:

foreach($cars as $car) {

All familiar so far. However, we need to split up those comma delimited bits so we can deal with them separately.  We will use explode again:

$car = explode(',', $car); 

In this case we are identifying the comma within the string $car and exploding it. This has now created an array of bits that we can access with indexes. So here is a completed version:

<?php
$cars = explode("\n", $page->my_textarea);
   foreach($cars as $car) {
   $car = explode(',', $car);
      echo "<h3>{$car[0]}</h3>";
      echo "<p>Car Model: {$car[1]}</p>";
      echo "<p>Car Colour: {$car[2]}</p>";
      echo "<hr>";
}
?>

The indexes are within the square brackets and start from zero - though you can use them in any order, of course!

And there you are - looping happily through your long list. 

I have just used the same principle to list 100 music tracks on my new site which I will showcase in a day or so.

Obviously, you could have achieved the same thing with repeaters and had the luxury of being able to reorder them and edit them easily - but sometimes all you want is a silly list, and this method does silly lists very well.

Joss

PS: Thanks to Apeisa, because I stole this from his Thumbnails module!

  • Like 8
Link to comment
Share on other sites

Thanks for this, Joss. It's nice to have not only PW but also PHP tutorials here :)

It should probably also be noted that an alternative method for handling CSV strings (which your list there seems to be, though admittedly a very simple one) is to use str_getcsv, which is available since PHP 5.3.0. In the context of your sample code it would look like this:

<?php
$cars = explode("\n", $page->my_textarea);
foreach($cars as $car) {
    $car = str_getcsv($car);
    echo "<h3>{$car[0]}</h3>";
    echo "<p>Car Model: {$car[1]}</p>";
    echo "<p>Car Colour: {$car[2]}</p>";
    echo "<hr>";
}

There's not much difference between these two methods really, as far as I know even performance-wise they are pretty much equal. Still if there's even a small possibility that you'd need to handle more complex CSV content at some point, using str_getcsv makes sense -- that way you won't need to worry about things like commas inside quotes, escaped characters etc.

  • Like 7
Link to comment
Share on other sites

Thanks Teppo - I didn't know about that one.

In fact, I think you could go as far as doing:

<?php
$cars = str_getcsv($page->my_textarea, "\n");
foreach($cars as $car) {
$car = str_getcsv($car);
echo "<h3>{$car[0]}</h3>";
echo "<p>Car Model: {$car[1]}</p>";
echo "<p>Car Colour: {$car[2]}</p>";
echo "<hr>";
}
 

​I haven't tried this though

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...