Retrieving css from tables in mysql using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gXion
    New Member
    • Mar 2008
    • 5

    Retrieving css from tables in mysql using php

    hi there,,
    I searched b4 I submit this post.

    so,
    let say:
    if I have a css data stored in mysql.
    the data looks like :
    Code:
    .name 
    {
    background-color: #0000EE;
    color: #0000EE
    text-decoration: italic;
    }
    
    .age
    {
    background-color: #00FF00;
    color: #0000FF
    text-decoration: bold;
    }
    OK ?

    now, if I want to split this data into a form ..
    the form contains of tow tables..
    each table name will be ( the class name ).

    the properties will break into <td>'s.
    here is a picture of what I want but could'nt do ( I made it in MS Word ) :


    how can i do that with php ?
    is it about preg_match_all( ) ?
    if so, what will be the table structure and php code ?

    thank you even if you don't have an answer ..
    your solutions helped me many times when I was a Guest.
    I hope you help my as a Member.
    Last edited by ronverdonk; Mar 19 '08, 07:11 PM. Reason: code tags!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • arggg
      New Member
      • Mar 2008
      • 91

      #3
      So you're wanting to display what is in the database into a table or you are wanting whatever css is in the database to be the css the site runs off of?

      Comment

      • gXion
        New Member
        • Mar 2008
        • 5

        #4
        ronverdonk
        sorry ,, i sweer the god i would do that but i frogot.

        Originally posted by arggg
        So you're wanting to display what is in the database into a table or you are wanting whatever css is in the database to be the css the site runs off of?
        I want to display what is in the database into a table. ( like the picture above )
        the first choise :)

        breaking data into <td>'s.
        to display the current css settings [ not using the css in <style> tag ].
        just to show it ( in the admin settings site );
        thanks.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          I'm not entire sure what you are talking about...

          Are you trying to parse actually CSS code into PHP and echo the the results in a table?
          To do that you may want to familiarize yourself with some of the String Functions. Most important of which are stripos, substr and explode
          Unless you want to go with some sort of regex solution, in which case I'm close to useless :P

          Is the CSS data already parsed in a table inside your database?
          If that is the case, assuming each row has a column for each CSS attribute, you would simply have to:
          [code=php]
          # Query the data
          $tableName = "myTable";
          $result = mysql_query("SE LECT atrribute1, attribute2, (...) FROM $tableName");

          # Display table
          echo "<table><tr ><td colspan=2>$tabl eName</td></tr>";
          while($row = mysql_fetch_ass oc($result))
          {
          foreach($row as $_k => $_v)
          {
          echo "<tr><td>$_ k</td><td>$_v</td></tr>";
          }
          }
          echo "</table>";
          [/code]

          If none of this is of any help, please try to explain exactly where and how your data is stored.

          Comment

          • gXion
            New Member
            • Mar 2008
            • 5

            #6
            Originally posted by Atli
            Hi.

            I'm not entire sure what you are talking about...

            Are you trying to parse actually CSS code into PHP and echo the the results in a table?
            To do that you may want to familiarize yourself with some of the String Functions. Most important of which are stripos, substr and explode
            Unless you want to go with some sort of regex solution, in which case I'm close to useless :P
            well, this is something.

            I want exactly what you understand ( parse actually CSS code into php .. etc ).
            I don't want to create a unique table for css.
            just one column with type MEDIUMTEXT in a table.

            in fact I tried this php code :
            ( forget about mysql right now , let's make the next code working, if so, i can try it in mysql myself )

            [PHP]<?php

            $css_is = "

            body
            {
            background: #FEFDF0;
            color: #000000;
            font: bold 12pt arial;
            margin: 0px 0px 0px 0px;
            padding: 0px;
            }

            .special
            {
            background: #FAF1D5 url(images/design-body.jpg) repeat-x top left;
            color: #000000;
            font: bold 8pt tahoma;
            }

            ";
            /*

            NOW we have a text variable which contains CSS code. Which we want to break it into <td>s like
            <table><tr>
            <td>body></td>
            <tr>
            <td>backgroun d</td><td>#FAF1D5 url(images/design-body.jpg) repeat-x top left</td>
            </tr>
            and so on.

            */

            $css_parsed = explode("\n",$c ss_is);
            foreach($css_pa rsed as $css_now)
            {
            // my stupid old code was here;
            }
            ?>[/PHP]

            lets say when I use echo ,it prints the { symbol as well . I don't now how to extract only the class name and the class properities into

            [PHP]$some_array[$class_name][$class_properit ies][/PHP]

            I tried also [PHP]preg_match_all( "/[(.*)][\{(.*)\}]/",$css_is ,$css_parsed );[/PHP]
            now it prints $css_parsed like separated letters each letter printed in one line .

            what i want from this code : is to create a html table filled in with $some_array.

            I don't want to cry.

            hope i explained it well.

            Comment

            • gXion
              New Member
              • Mar 2008
              • 5

              #7
              so .. ?‌‌‌‌‌‌‌‌‌‌‌‌‌‌ ‌‌‌‌‌‌‌‌‌‌

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Forgive the delayed answer. I have been away for a while.
                To answer your question...

                You will simply have to build a code that dissects the CSS code and cuts it into usable pieces.

                There is always the possibility of using some elaborate regex solution, but that is a whole another bowl of wax.

                So, using PHP's String functions, we could start by cutting each class into it's own array element by exploding the CSS file on each end bracket:
                [code=php]
                // Cut the raw CSS text into classes
                $rawClass = explode("}", $cssString);
                [/code]

                Then, to clean this up, we could cut each class into two elements, the first containing the name and the second the raw text containing it's members:
                [code=php]
                // Create class array
                $classes = array();

                // Loop through all the raw classes
                foreach($rawCla ss as $_v)
                {
                // Make sure that the class isn't empty
                $_v = trim($_v);
                if(empty($_v)) continue;

                // Cut the raw class
                $rawClassBuffer = explode("{", $_v);

                // Create the class buffer and add the name
                $classBuffer = array();
                $classBuffer['Name'] = trim($rawClassB uffer[0]);
                [/code]

                Now we would seperate each member of each class and seperate that into name and value:
                [code=php]
                // Seperate each line of members
                $rawMember = explode(";", $rawClassBuffer[1]);

                // Create member buffer
                $members = array();

                // Seperate the name and value for each member
                foreach($rawMem ber as $_v2)
                {
                // Make sure the member isn't empty (again)
                $_v2 = trim($_v2);
                if(empty($_v2)) continue;

                // Seperate the names and values
                $rawMemberBuffe r = explode(":", $_v2);

                // Add a name and value for each member to the member array
                $memberBuffer = array();
                $memberBuffer['Name'] = trim($rawMember Buffer[0]);
                $memberBuffer['Value'] = trim($rawMember Buffer[1]);

                $members[] = $memberBuffer;
                }
                [/code]

                And to finish it off, we add the class members to the class buffer and the class buffer to the class array:
                [code=php]
                // Add the members array to the class buffer
                $classBuffer['Members'] = $members;

                // Add the class to the class array
                $classes[] = $classBuffer;
                }
                [/code]

                And finally, now that we have an array, to display it we could simply do:
                [code=php]
                // Show each class as a table
                foreach($classe s as $_class)
                {
                // Print table header and class name
                echo "<table style='width: 250px;' border='1'>";
                echo "<tr><td colspan='2' style='text-align:center;'> {$_class['Name']}</td></tr>";

                // Show each member
                if(count($_clas s['Members']) > 0)
                {
                foreach($_class['Members'] as $_member)
                {
                // Print row and columns for names and values
                echo "<tr><td>{$_mem ber['Name']}</td><td>{$_membe r['Value']}</td></tr>";
                }
                }

                // Close table
                echo "</table>";
                }
                [/code]

                Put that all together and provide a valid CSS string and you should see all your classes in neat little tables :)

                Hope this helps.

                Comment

                Working...