开发者

Get specific values from multidimentional array locked to ID

开发者 https://www.devze.com 2023-04-13 08:21 出处:网络
Maybe the title is a little confusing, but let me try to explain. I have a database (informix), and use PHP PDO, in that database there is a table which contains stylesheets for pages.

Maybe the title is a little confusing, but let me try to explain.

I have a database (informix), and use PHP PDO, in that database there is a table which contains stylesheets for pages. See it like this:

Special ID, complete stylesheet (like we make it in a CSS file).

At the moment I can display the records in an multidimentional array (see below the code and output):

$query = $db -> prepare("select alpb_box_id, alpb_style::lvarchar as STYLESHEET  
from ao_link_page_boxes WHERE  alpb_li_id = 633 AND alpb_lngcode = '031' AND 
alpb_pageno = 1"); 

$query -> execute();

$result = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)) {

$result[] = $row;
}
$db = null;

echo "<pre>";
print_r($result);
echo "</pre>";

And the output:

Array
(
[0] => Array
(
[ALPB_BOX_ID] => TXT-633-P1B1
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Swis721 BT; font-size:69px; font-style:normal; font-variant:normal; font-weight:normal; height:75px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:793px; word-spacing:0px; z-index:auto;
    )

[1] => Array
    (
        [ALPB_BOX_ID] => TXT-633-P1B2
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Swis721 BT\'; font-size:16px; font-style:italic; font-variant:normal; font-weight:normal; height:197px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:absolute; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:75px; vertical-align:baseline; white-space:normal; width:717px; word-spacing:0px; z-index:auto;
    )

[2] => Array
    (
        [ALPB_BOX_ID] => IMG-633-P1B3
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style开发者_开发百科:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Times New Roman\'; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:207px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
    )

[3] => Array
    (
        [ALPB_BOX_ID] => IMG-633-P1B4
        [STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Times New Roman; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
    )

)

Now what I try to do is, get from every ID (like: TXT-633-P1B1), the following styles:

height:75px;, width:793px;, top:0px; and left:0px;.

And store them into results so I can reuse and echo them when needed.

In this case I should get 4 ID's with 4 style items (including amount px).

Does anyone has an idea how to do this. I hope I explained my question correct.

Thanks


Thanks that was very helpfull, I do have one question.

How can I add these values into an pre-made div. For example:

<div id="TXT-633-P1B1" style="height:75px; width:793px; top:0px; left:0px;"></div>

And this for every ID that is in that array.

Thanks I really appreciate it.


Try this:

$new_styles = array();
$styles_to_collect = array('height', 'width', 'top', 'left');
foreach($result as $r)
{
        $new_entry = array();
        foreach($styles_to_collect as $stc)
        {
                preg_match('/[;\s]\s*'.$stc.':\s*([\d]+(px)?)([;\s]|$)/', $r['STYLESHEET'], $matches);
                $new_entry[$stc] = $matches[1];
        }
        $new_styles[$r['ALPB_BOX_ID']] = $new_entry;
}

produces ($new_styles):

Array
(
    [TXT-633-P1B1] => Array
        (
            [height] => 75px
            [width] => 793px
            [top] => 0px
            [left] => 0px
        )

    [TXT-633-P1B2] => Array
        (
            [height] => 197px
            [width] => 717px
            [top] => 75px
            [left] => 0px
        )

    [IMG-633-P1B3] => Array
        (
            [height] => 188px
            [width] => 377px
            [top] => 207px
            [left] => 0px
        )

    [IMG-633-P1B4] => Array
        (
            [height] => 188px
            [width] => 377px
            [top] => 0px
            [left] => 0px
        )

)

EDIT: Per the OP's request I am adding more to my answer. To use these styles in the content you could do something like:

$content = '';
foreach($new_styles as $id=>$ns)
{
        $inline_styles = '';
        foreach($ns as $name=>$value) $inline_styles .= $name.': '.$value.'; ';
        $inline_styles = trim($inline_styles);
        $content .= '<div id="'.$id.'" style="'.$inline_styles."\"></div>\n";
}

Which will set $content to:

<div id="TXT-633-P1B1" style="height: 75px; width: 793px; top: 0px; left: 0px;"></div>
<div id="TXT-633-P1B2" style="height: 197px; width: 717px; top: 75px; left: 0px;"></div>
<div id="IMG-633-P1B3" style="height: 188px; width: 377px; top: 207px; left: 0px;"></div>
<div id="IMG-633-P1B4" style="height: 188px; width: 377px; top: 0px; left: 0px;"></div>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号