TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-19-2008, 07:55 PM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default 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?
benton is offline  
Reply With Quote
Old 10-20-2008, 06:27 AM   #2 (permalink)
The Contributor
 
awuehr's Avatar
 
Join Date: Oct 2008
Location: Nuremberg, Germany
Posts: 26
Thanks: 3
awuehr is on a distinguished road
Images

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
Send a message via ICQ to awuehr Send a message via Skype™ to awuehr
awuehr is offline  
Reply With Quote
Old 10-20-2008, 02:05 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Oh, I understand what you're asking now. Awuehr's solution looks as though it might work, and work very well, too!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-22-2008, 02:17 AM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by awuehr View Post
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.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design