简体   繁体   English

如何将此数据库记录字符串格式化为该JavaScript对象数组?

[英]How to format this Database record string into this JavaScript array of objects?

In a MySQL Database table column, I have a value like this returned to my PHP script... 在MySQL数据库表列中,我将这样的值返回了我的PHP脚本...

position=1&x=0.203125&y=0.23125&position=2&x=0.8427083333333333&y=0.415625&position=3&x=0.5510416666666667&y=0.4375&

Broken down it looks like this... 分解看起来像这样...

position=1&
x=0.203125&
y=0.23125&

position=2&
x=0.8427083333333333&
y=0.415625&

position=3&
x=0.5510416666666667&
y=0.4375&

So this particular record has 3 sets of a position , x , and y 因此,此特定记录具有3组positionxy

Now in my PHP script while I am in a while loop getting each row from the database, I need to take this column and break the string into seperate variables so that it can create this JavaScript from the above example database value I need to build this JavaScript value with my PHP... 现在,在我的PHP脚本中,当我处于while循环中时,从数据库中获取每一行,我需要获取此列并将字符串分成单独的变量,以便它可以根据上面的示例数据库值创建此JavaScript,我需要构建此我的PHP的JavaScript价值...

<script>
$('#photo1 .avoid').addAnnotations(annotation, [
    {x: 0.203125, y: 0.23125, position: 1},
    {x: 0.8427083333333333, y: 0.415625, position: 2},
    {x: 0.5510416666666667, y: 0.4375, position: 3}
  ]);  
</script>

So each record/row in my database will have a column that has values like this and I need to build that little JavaScript snippets in my Loop for each row. 因此,数据库中的每条记录/行都将具有一列具有这样的值的列,我需要在我的Loop中为每一行构建一些JavaScript片段。

I would appreciate any help in how I can extract and get the data in that column to build this JavaScript? 我将在如何提取和获取该列中的数据以构建此JavaScript方面提供帮助,我将不胜感激。

If an absolute must, I can also alter how that data is stored in that database column if it needs to be separated differently or something to make it easier. 如果绝对需要,我还可以更改数据在该数据库列中的存储方式(如果需要以不同的方式进行分隔)或可以简化的方法。 I appreciate any help with this thank you. 感谢您的帮助。

Simple PHP loop example... 简单的PHP循环示例...

<?php

foreach ($rmaRecord as $rma){

    $img1 = "
        <script>
        $(document).ready(function() {
            $('#photo1 .avoid').addAnnotations(annotation, [
               {x: 0.3875, y: 0.3246, position: 4},
               {x: 0.8427083333333333, y: 0.415625, position: 2},
               {x: 0.5510416666666667, y: 0.4375, position: 3}
            ]);  
        });
        </script>";

}

As per my comment, if you could save the data as json, that would be easiest, but presuming you cannot, try the following: 根据我的评论,如果您可以将数据另存为json,那将是最简单的,但是假设您不能,请尝试以下操作:

$data = 'position=1&x=0.203125&y=0.23125&position=2&x=0.8427083333333333&y=0.415625&position=3&x=0.5510416666666667&y=0.4375&';
$json = array();
$pairs = array_filter(explode('&', $data));

for($i=0; $i < count($pairs); $i+=3){
    $position = explode('=', $pairs[$i]);
    $position = $position[1];

    $x = explode('=', $pairs[$i+1]);
    $x = $x[1];

    $y = explode('=', $pairs[$i+2]);
    $y = $y[1];
    $json[]=array('x' => $x, 'y' => $y, 'position' => $position);
};

echo json_encode($json);
//echo '<pre>' . json_encode($json, JSON_PRETTY_PRINT) . '</pre>';

You are right that you need to split that string to an array and I think there are a number of ways to get that done. 没错,您需要将该字符串拆分为一个数组,我认为可以通过多种方法来完成。 If your using a template style you can intersperse html with php. 如果您使用的是模板样式,则可以将html与php穿插。 PHP is not my main strong point but as a quick and dirty solutions you can work with something like the folowing. PHP不是我的主要优点,但是作为一种快速又肮脏的解决方案,您可以使用folowing之类的东西。

    <script>
        $(document).ready(function() {
            $('#photo1 .avoid').addAnnotations(annotation, [
    <?php

        $a = "position=1&x=0.203125&y=0.23125&position=2&x=0.8427083333333333&y=0.415625&position=3&x=0.5510416666666667&y=0.4375&";
        $a = preg_replace("/&$/","",$a);
        $array = array_chunk(preg_split("/&/",$a),3);
        $js_obj = "";
        foreach ($array as $key) {
            $js_obj .= "{" . $key[0] .",". $key[1] .",". $key[2] . "},";
        }

      echo preg_replace("/,$/","",$js_obj);
      // Outputs {position=1,x=0.203125,y=0.23125},{position=2,x=0.8427083333333333,y=0.415625},{position=3,x=0.5510416666666667,y=0.4375}

    ?>
            ]);  
        });
    </script>

Depending on your PHP version you could also try: 根据您的PHP版本,您还可以尝试:

<script>
        $(document).ready(function() {
            $('#photo1 .avoid').addAnnotations(annotation, [
    <?php

        $a = "position=1&x=0.203125&y=0.23125&position=2&x=0.8427083333333333&y=0.415625&position=3&x=0.5510416666666667&y=0.4375&";
        $a = preg_replace("/&$/","",$a);
        $array = array_chunk(preg_split("/&/",$a),3);
        echo json_encode($array, JSON_PRETTY_PRINT);
        ?>
        ]);  
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM