TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How do I code this array (http://www.talkphp.com/general/3497-how-do-i-code-array.html)

benton 10-19-2008 07:55 PM

How do I code this array
 
I want to write a script that will read in a css file, allow the user to alter the settings via a form and then save the file. The problem I'm having is in figuring out the best way to cross-reference the array I read in and the fields read from the form. This is the code I wrote to read in the css file. It creates a multi-array with each class from the css file in its only array.
Code:

function GetClasses($cssFile)
{
  $classesArray = array();

  if (($fp = @file($cssFile)))
  {
    $i = 0;
    $start = false;
   
    foreach ($fp as $line)
    {
      if ($line[0] == '#')
      {
        $classesArray[$i][] = $line;
        $start = true;
      } 
      else if ($start)
      {
        $classesArray[$i][] = $line;

        if ($line[0] == '}')
        {
          $i++;
          $start = false;
        } 
      }
    } 
  }
  return $classesArray;
}

Let's say the above creates this array
Code:

Array
(
    [0] => Array
        (
            [0] => #nav, #nav ul {
            [1] => float: left;
            [2] => width: 58em;
        )

    [1] => Array
        (
            [0] => #nav a {
            [1] => font-family: Tahoma, Verdana, sans-serif;
            [2] => font-size: .8em;
            [5] => width: 7em;
        )
}

and that the form is setup to hold those values
Code:

<input type="text" name="nav_float"
<input type="text" name="nav_width"
<input type="text" name="nav_a_font_family"
<input type="text" name="nav_a_width"

How do I read in the form elements and match them to the correct array element? The only way I can think of is to assign numbers to the form elements, like
Code:

<input type="text" name="0_0_nav_float"
<input type="text" name="0_1_nav_width"
<input type="text" name="1_0_nav_a_font_family"
<input type="text" name="1_1_nav_a_width"

Then I would explode each name and match it to the array element using those keys but that seems awfully convoluted. Is there some better way to do this?

awuehr 10-20-2008 06:27 AM

Hi Benton,

In the HTML-Form just write:
HTML Code:

<input type="text" name="nav[float]" />
<input type="text" name="nav[width]" />
<input type="text" name="nav[a][font_family]" />
<input type="text" name="nav[a][width]" />

$_POST['nav'] should return the following:

Code:

$_POST['nav'] = array(
    'float' => 'some value',
    'width' => 'some value',
    'a' => array(
        'font-family'=>'some value',
        'width' => 'some value'
    )
);

Please let me know it this works, I am not really shure.


Greetings,

Alex

Wildhoney 10-20-2008 02:05 PM

Oh, I understand what you're asking now. Awuehr's solution looks as though it might work, and work very well, too!

benton 10-22-2008 02:17 AM

Quote:

Originally Posted by awuehr (Post 18964)
Hi Benton,

Please let me know it this works, I am not really shure.

Greetings,

Alex

Thanks for the suggestion. I hadn't thought of getting an array from the form and it is better than what I had. I've done that before using the serialize function so coding it isn't a problem. But that wasn't really the part I was having a problem with, or maybe I don't fully understand your solution.

Let's say there is one class in the css file so when the file is read in, the array will appear as
Code:

Array
(
    [0] => Array
        (
            [0] => #nav, #nav ul {
            [1] => float: left;
            [2] => width: 58em;
        )
}

When the array is then created from the form, it would appear as
Code:

$_POST['nav'] = array(
    'float' => 'some value',
    'width' => 'some value'
);

What I am failing to see is how to transfer the elements of the second array into the first. The
Code:

[0] => #nav, #nav ul
entry would have to stay the same since that is the actual class name and is required for proper operation. Do I just walk through both arrays and set the items after that? Something like
Code:

foreach ($1stArray as $array) {
 foreach ($array as $k => $d) {
  $d = $2ndArray[ I don't know what goes here]

I suppose that if I named the form elements with the actual class name, then I could just use the new array read from the form. Is having a form element setup like the following considered valid code?
Code:

<input type="text" name="#nav, #nav ul {"
I hope the above explained the problem I am having a little better.


All times are GMT. The time now is 09:18 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0