2017-04-02 101 views
0

我想初始化一個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> 

這看起來有點混亂對我來說。可有人請幫助

+0

PHP中的變量通過'$'跡象preceeded。說到這個:'<?php var imgSrc = base_url()' –

+0

'<?php var imgSrc = base_url()。「thumbs /".$ item [」filename「]; ?>'應該是'<?php $ imgSrc = base_url()。「thumbs /".$ item [」filename「]; ?>' – spex

+0

好的。所以我如何檢索JavaScript塊中的變量 img.src =「<?php echo imgSrc?>」; – parker

回答

0

此行是錯誤的:

<?php var imgSrc = base_url()."thumbs/".$item["filename"]; ?> 

它可能應該是這樣的:

var imgSrc = '<?php echo base_url()."thumbs/".$item["filename"]; ?>'; 
+0

var imgSrc返回拼寫錯誤的單詞 – parker

+0

@parker對不起,我不引用你的意思? – Schlaus

+0

這一行給出了錯誤var imgSrc = – parker