2016-02-19 161 views
0

我有一个代码,通过ajax将产品的id发送到php文件。 在这个php文件中,我处理产品的id,并从mysql db中选择产品id为外键(pid_fk)的图像的所有路径(位于本地主机服务器上)。 其实,我选择的数据是:从.php发送图像到存储在服务器中的ajax

图像(FID)的-Id

-Path图像(URL)

我有什么:

阿贾克斯文件:

var datos = { 
    cod: id //This is the id of the product 
} 
$.ajax({ 
    type: "GET", 
    url: "php/carga/adm_prodfotos.php", 
    data : datos, 
    cache: false, 
    dataType: 'json', 
    success: function(result){ 
     //I'll catch the images here and to show on my website 
    } 

}); 

PHP文件:

<?php 

    if(isset($_GET['cod'])){ 
     $cod = $_GET['cod']; 
    } 
    else{ 
     $cod = ''; 
    } 

    require_once("../../../php/DBClass.php"); 
    $db = new Cl_DBClass(); 
    $query = "SELECT fid, url FROM fotos WHERE pid_fk = $cod"; 
    $result = mysqli_query($db->con, $query); 
    while($fila = $result -> fetch_assoc()){ 
     //Here I think I have to process the path and convert to something to send to the ajax file (Such an array) 
    } 


?> 

所以问题是:

1)我不知道如何将图像的路径转换为“包”发送到ajax文件。

2)我必须在这个“捆绑”来转换图像显示。

+1

如果存储在'url字段'中的数据真的是'url',那么你不必做任何事情,只是把它放在'src属性'和' img'元素会为你做诡计。但如果它是服务器上的物理路径,则必须首先在服务器端创建图像的url(使用物理路径),然后将其传递给您'ajax'函数调用。也请做一些研究'php json_encode' – EhsanT

+0

@EhsanT是的。我刚刚回答了我自己的问题,我做了类似的事情。不管怎么说,还是要谢谢你。 –

+0

好,有趣的编码... – EhsanT

回答

0

我刚刚做到了。我的想法更容易。 我所做的是选择图像的路径并以html形式发送。

代码: PHP:

<?php 

    if(isset($_GET['cod'])){ 
     $cod = $_GET['cod']; 
    } 
    else{ 
     $cod = ''; 
    } 

    require_once("../../../php/DBClass.php"); 
    $db = new Cl_DBClass(); 
    $query = "SELECT fid, url FROM fotos WHERE pid_fk = $cod"; 
    $result = mysqli_query($db->con, $query); 
    $html = ''; 

    while($fila = $result -> fetch_assoc()){ 
     $img = $fila['url']; 
     $fid = $fila['fid']; 
     $html .= <<<HTML 
      <img data-id=$fid src="http://127.0.0.1/project/front/images/productos/$img" height='250' width='250'> 

HTML; 
    } 
    echo $html; 
?> 

阿贾克斯:

$('#modal2').on("shown.bs.modal",function(e){ 
    var datos = { 
     cod: id 
    } 
    $.ajax({ 
     type: "GET", 
     url: "php/carga/adm_prodfotos.php", 
     data : datos, 
     cache: false, 
     dataType: 'html', 
     success: function(result){ 
      $('#imagenes').html(result); 
     } 

    }); 
}); 

因为,如果你需要我这是怎么保存的图像的URL在db:

pid_fk/fid(请记住,pid_fk是产品的id并且是图像的id)

例如: 1/image1.jpg