admin管理员组文章数量:1279175
I have a php loop that loops through products and their data:
<?php foreach ($this->products as $product) { ?>
<script type=text/javascript>
product = {
id: $product->virtuemart_product_id,
name: $product->product_name
};
</script>
<?php } ?>
The data contains information about the product un the $product like so:
- $product->virtuemart_product_id
- $product->product_name
Is it possible to add the data to a existing javascript array or object in each of the loops? Basically it will add the data of the loop to the javascript array or product.
Any help would be appreciated :)
I have a php loop that loops through products and their data:
<?php foreach ($this->products as $product) { ?>
<script type=text/javascript>
product = {
id: $product->virtuemart_product_id,
name: $product->product_name
};
</script>
<?php } ?>
The data contains information about the product un the $product like so:
- $product->virtuemart_product_id
- $product->product_name
Is it possible to add the data to a existing javascript array or object in each of the loops? Basically it will add the data of the loop to the javascript array or product.
Any help would be appreciated :)
Share Improve this question asked Jul 25, 2013 at 10:26 Sebastian OppermanSebastian Opperman 2411 gold badge7 silver badges18 bronze badges 07 Answers
Reset to default 8There is a more better and clean way, use json_encode
PHP
<?php
$data = array();
foreach( $this->products as $product ) {
$data[] = array(
"id" => $product->virtuemart_product_id,
"name" => $product->product_name
);
}
?>
HTML
<script type=text/javascript>
var product = <?php echo json_encode( $data ) ?>;
</script>
You can also use
var product = <?php echo json_encode( $product ) ?>;
So when you want to access the product
with javascript, your code will be
<script type="text/javascript">
for( i in product ) {
console.log( product[i] );
}
</script>
You can use json using the php function json_encode:
<script type='text/javascript'>
<?php
$products = $this->products;
$js_array = json_encode($products);
echo "var products = ". $js_array . ";\n";
?>
</script>
Another way to do the same thing:
<script type="text/javascript">
var products= <?php echo json_encode($products); ?>;
for(var i=0;i<n;i++)
{
alert(products[i]);
}
</script>
you mean like this?:
<script type=text/javascript>
var x, productArray = [];
<?php foreach ($this->products as $product) { ?>
x = {
id: <?php print $product->virtuemart_product_id; ?>,
name: <?php print $product->product_name; ?>
};
productArray.push(x);
<?php } ?>
</script>
What about that:
<script type=text/javascript>
var products= array();
<?php foreach ($this->products as $product) {
echo "product.push({
id: $product->virtuemart_product_id,
name: $product->product_name
});" ;
?>
</script>
Instead of having a script tag inside your loop, build a PHP array containing id/name for each product then, outside the loop, use json_encode() on it. This is then given to your JavaScript.
Using .push()
you can append elements to array like this:
var products = [{
name: 'Product 1',
id: 1
}, {
name: 'Product 2',
id: 2
}]
So, if you want to append element to products
array from your loop:
products.push({
id: <?php echo $product->product_id; ?>,
name: '<?php echo $product->product_name; ?>'
});
My solution:
<?php foreach ($this->products as $product) { ?>
<script type=text/javascript>
product = {
id: <?=json_encode($product->virtuemart_product_id)?>,
name: <?=json_encode($product->product_name)?>
};
</script>
<?php } ?>
本文标签: javascriptAdding php loop data to js arrayStack Overflow
版权声明:本文标题:javascript - Adding php loop data to js array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245139a2364730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论