Use information from DB to store it in variables and put it in an array

Asked 1 weeks ago, Updated 1 weeks ago, 0 views

var marker =  {
                        index: i,
            title: '<?php echo $rows['restaurantName'] ?>',
            latlng: new kakao.maps.LatLng(<?php echo $rows['location'] ?>),
            content: '<div class="overlaybox">' +
            '    '    <div class="boxtitle"><?php echo $rows["restaurantName"]?></div>' +
            '    '    <div class="first">' +
            '        '        <div class="triangle"></div>' +
            '        '        <div class="categori"><?php echo $rows['categori']?></div>' +
            '    '    </div>' +
            '    '    <div class="instaId"><?php echo $rows['instaId']?></div>' +
            '    '    <ul class="information">' +
            '        '        <li class="address">' +
            '            <div class="addr">Address</div>' +
            '            '            <div class="addr1"><?php echo $rows['addr1']?></div>' +
            '            '            <div class="addr2"><?php echo $rows['addr2']?></div>' +
            '        '        </li>' +
            '        '        <li class="review">' +
            '            <div class="review1">one-line review</div>' +
            '            '            <div class="review2"><?php echo $rows['review']?></div>' +
            '        '        </li>' +
            '    '    </ul>' +
            '</div>'
          };

How can I save the information from DB in this way in a var marker, push it to positions, and create a marker on the Kakao Map map using a repetitive statement?

The information is located in localhost root tasty (DB name) restoreants (table name). Create one marker in one row.

If I take it to select * from json and make an array, can I make it in the above format with only script in the array?

javascript php

2022-09-20 10:56

1 Answers

PHP should provide raw data contained in DB without processing, and JavaScript should map (processed consistently in order) and use it. Please see the code below.

// PHP only prints the string "var positions = ..." in HTML.
// The string will be a great thing called the JS array at the runtime of the web application.
var positions = [
    {
        "restaurantName": "foo",
        "location": "Sindang-dong 1, Jung-gu, Seoul",
        // Others, etc
    }
];

// The key is that there is no phpecho in this.
// Positions are already a complete JS array, so you can access the properties of the objects in them and process them as you want.
var markers = positions.map(function (position) {
    return {
        "title": position.restaurantName,
        "latlng": new kakao.maps.LatLng(position.location),
        // Others, etc
    }
});

The reason why I didn't write down index key examples is because... Because there's something wrong. In fact, each object in the positions array must already have a value such as restaurantId. The ID that comes with the original data is the real ID.

I hope it helps. #makePHPgreatagain


2022-09-20 10:56

If you have any answers or tips


© 2022 pinfo. All rights reserved.