Question:

How to split this up with PHP

by  |  earlier

0 LIKES UnLike

For my website each user has their own mini web site. With each page they can rearrange their blocks (holds page content). Once they have rearranged their blocks a string like this '3,1|1,1|1,1|1,1|1,1|1,1|1,1|1,1|1,1' goes to the DB .

Each section which is separated by '|' is the colspan and rowspan of a block for a table. So 3,1 is for block1, 3 is the colspan and 1 is the rowspan.

How do I spilt this all up, so it because variables, like;

$block1_colspan = 3;

$block1_rowspan = 1;

and so on for every block?

 Tags:

   Report

1 ANSWERS


  1. Your best bet is to first break up the data retrieved from the DB using the explode() function, and I would recommend using arrays for your data.  (Replace the $string variable with the name of your string that containst the data)

    Code:

    $dimensions=explode("|", $string);

    // dimensions variable is an array that has values such as 3,1 in one field, 1,1 in the next...

    $numinarray = count($dimensions);

    for($i=0;$i<$numinarray;$i++){

    list($colspan[], $rowspan[])=explode(",", $dimensions);

    }

    Your output would be like:

    $colspan[0]=3

    $rowspan[0]=1

    $colspan[1]=1

    $rowspan[1]=1

    ...

    Be sure to remember that arrays begin at 0, not 1.

    More info on functions used in the sources

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions