0

I am learning basic Vue.js, when I fetch data using PHP, why it can not put into the Vue object data array

    data: {
        message: "vue?",
        homeView: true,
        brandView: false,
        flipF: true,
        flipB: false,
        prices:[
             {coffee1:15},{coffee1:12},
            <?php 
                while($row = mysqli_fetch_array($result)){
                echo "{coffee1:".$row['price']."},";
                }
            ?>
        ]
    },

{{ item.coffee1}}

The php part could output {coffee1:20},{coffee1:18}, but they can not be shown on webpage

1 Answer 1

1

Ideally, you should fetch from an API which will serve JSON, but for sake of answering you should instead build your array, and then use json_encode().

<?php
$prices = [
 ['coffee1' => 15],
 ['coffee1' => 12]
];

while($row = mysqli_fetch_array($result))
  $prices[] = ['coffee1' => $row['price']];

?>

Now in your vue:

data() {
   return {
       message: "vue?",
       homeView: true,
       brandView: false,
       flipF: true,
       flipB: false,
       prices: <?= json_encode($prices) ?>
   }
},
Sign up to request clarification or add additional context in comments.

Comments

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.