0

I have no idea how i do this, i have some pieces of code, that i need to "handle" and use in javascript for some search engine

This is what i have in my database

[
["Reparation", "Pris"],
["Fejlfinding", "Gratis"],
["Udskiftning af Skærm (Refurbished)", "3699,-"]
]

This is what i need it to look like after i get it from the database and it has been handled.

var searchChoices = {


"Name": {

    "Model Name": {
        "icons": {
            "dark": "Imagelink",
            "light": "imagelink"
        },

        "items": [

        {
            "headline": "Gratis",
            "text": "Fejlfinding"
        }, 

        {
            "headline": "3699",
            "text": "Udskiftning af Skærm (Refurbished)"
        }

        ]

    }
}
};

I have really no idea how to do this? I don't even know what it's called.

Can someone please help me, or point me in the right way?

Thanks

2 Answers 2

0

I think you should use php json_encode function to send data to javascript. You can make something like this:

<script>
var data = <?=json_encode($data);?>
...

Next step is simple loop to add items:

for(var row in data)
    searchChoices["Name"]["Model name"].items.push({
        "headline": data[row][1],
        "text": data[row][0]
    });

You can also generate whole javascript object using PHP:

<script>
var searchChoices = {
    "Name": {

    "Model Name": {
        "icons": {
            "dark": "Imagelink",
            "light": "imagelink"
        },

        "items": [
        <?php
            foreach($data as $row)
                echo '{"headline": "' . $row[1] . '", "text": "' . $row[0] . '"},';
        ?>
        ]
    }
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

What i have in my database, is in a single colum, so i can not handle each row/colum. :3 and i don't understand everything you're saying :3
I see that you edited you're anwser, but it will not work, the data i have is in a single collum, as i posted above. not in diffents rows :)
OK, I think I understand. Is this your solution?
No unfurtunaly not, what i have is not php, java or something else, its raw data in the the form as postet, nothing else :3
0

OK, this is another solution, regular expression:

(\[\"(.+)\"\,\ ?\"(.+)\"\]\,?)

The next step is to get data from preg_match_all result.

This is simple script i wrote to test:

<?php
$data = <<<JS
[
    ["Reparation", "Pris"],
    ["Fejlfinding", "Gratis"],
    ["Udskiftning af Skærm (Refurbished)", "3699,-"]
]
JS;

$pattern = '/(\[\"(.+)\"\,\ ?\"(.+)\"\]\,?)/';
$matches = [];

$result = preg_match_all($pattern, $data, $matches);

for($i=0, $j=count($matches[2]); $i<$j; $i++) {
    echo '{"headline": "' . $matches[3][$i] . '", "text": "' . $matches[2][$i] . '"},' . "\n";
}

2 Comments

Hey Arc :) the script is working, but I have some problems, are you up for helping me :)? if I take the data directly from the DB, (Hardcoded) as well, the output is broken, but if I run it through a javascript beautifier, it works fine. from the DB it break because all of the ] does not transform correctly... I think I may can be fixet by the pattern? but Regx is not my strong side :3
This is new pattern: /(\"([a-zA-Z0-9\(\)\ ÆØÅæøå]+)\"\,\"([a-zA-Z0-9]+)(\,\-)?\")/ This is not perfect, becaouse i added characters ( and ) to pattern, but you must be sure that there is no more special characters. Second, pattern have also characters like: ÆØÅæøå. Check this to as much records as possible, but remember that this could not be reliable to all situations (ex. string with character \") :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.