0

I fetched data from the db and saved it in a smarty array. Now I want to save those values in a javascript array and i dont know the size of it.

{foreach from=$car_owner item=car_item}
    <tr>
        <td>
            <a href="#">
                <script>
                    var arrayPos = [
                        ['{$car_item['longitude']}', '{$car_item['latitude']}']]
                    ];
                </script>
                {$car_item['carName']}
            </a>
        </td>
    </tr>
{/foreach}

The array is only saving one value for the longitude and one for the latitude despite that i have more than one value for each one. Im sure i need to loop it, but as i said i dont know the size of what i should loop because the $car_owner array contains data fetched from the db. An help would be appreciated. Regards

4
  • - Look to this package, it might help you: github.com/laracasts/PHP-Vars-To-Js-Transformer Commented Oct 13, 2020 at 9:21
  • @MuhammadSadiq Sorry, it didnt help. My problem is when i use the array outside the script tag i only get the last value because it is all inside a loop, and i dont get the older values.. Commented Oct 13, 2020 at 12:45
  • okay, then put foreach inside of the array Commented Oct 18, 2020 at 9:01
  • define js array outside of the loop and then inside of loop, push the values to it. Commented Oct 18, 2020 at 9:02

1 Answer 1

1

You can do this with two loops.

<script>
var arrayPos = [
{foreach from=$car_owner item=car_item}
    ['{$car_item['longitude']}', '{$car_item['latitude']}']]{if !$car_item@last},{/if}
{/foreach}
];
</script>

{foreach from=$car_owner item=car_item}
    <tr>
        <td>
            <a href="#">{$car_item['carName']}</a>
        </td>
    </tr>
{/foreach}
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.