我想初始化一個php變量來從數據庫循環獲取圖像源。這是我使用這樣做動態分配圖像src到畫布
<?php foreach($load_preview as $item): ?>
<div class="store-item">
<?php var imgSrc = base_url()."thumbs/".$item["filename"]; ?>//having syntaxt error at this line
<a href="<?php echo site_url()."/"."accessories/preview/".$item["id"];?>">
<canvas width="450" height="450" id="canvas">Sorry, no canvas available</canvas>
</a>
</div>
<?php endforeach; ?>
我重新分配到上述一個javascript代碼代碼繪製一個帆布如圖所示縮放的代碼片段
/**
* START
*/
var img = document.createElement('img');
img.onload = function() {
iw = 500;
ih = 500;
canvas.width = (iw/2 - 1.5)|0; //compensate for rounding errors
canvas.height = (ih/2 - 1.5)|0;
doCanvas();
canvas.addEventListener('mouseout', doCanvas, false);
canvas.addEventListener('mousemove', move, false);
}
img.src = "<?php echo imgSrc ?>";//retrieve the assigned variable here in the javascript assignment
</script>
這看起來有點混亂對我來說。可有人請幫助
PHP中的變量通過'$'跡象preceeded。說到這個:'<?php var imgSrc = base_url()' –
'<?php var imgSrc = base_url()。「thumbs /".$ item [」filename「]; ?>'應該是'<?php $ imgSrc = base_url()。「thumbs /".$ item [」filename「]; ?>' – spex
好的。所以我如何檢索JavaScript塊中的變量 img.src =「<?php echo imgSrc?>」; – parker