将存储在服务器中的图像从 .php 发送到 ajax
Send images from .php to ajax that are stored in the server
我有一个代码,通过 ajax 将产品 ID 发送到 php 文件。
在这个 php 文件中,我处理了产品的 ID,我 select 从 mysql 数据库中处理了所有具有该 ID 的图像路径(在我的本地主机服务器上)产品的外键 (pid_fk)。
其实我select的数据是:
-图片的Id(fid)
-图片路径(url)
我有:
ajax 文件:
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) 我不知道如何将图像的路径转换为 "bundle" 以发送到 ajax 文件。
2) 我必须处理此 "bundle" 以转换为要显示的图像。
我刚刚完成了。这比我想的要容易。
我所做的是 select 图片的路径并作为 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;
?>
Ajax:
$('#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 的方式:
pid_fk/fid(记住pid_fk是产品的id,fid是图片的id)
例如:
1/image1.jpg
我有一个代码,通过 ajax 将产品 ID 发送到 php 文件。 在这个 php 文件中,我处理了产品的 ID,我 select 从 mysql 数据库中处理了所有具有该 ID 的图像路径(在我的本地主机服务器上)产品的外键 (pid_fk)。 其实我select的数据是:
-图片的Id(fid)
-图片路径(url)
我有:
ajax 文件:
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) 我不知道如何将图像的路径转换为 "bundle" 以发送到 ajax 文件。
2) 我必须处理此 "bundle" 以转换为要显示的图像。
我刚刚完成了。这比我想的要容易。 我所做的是 select 图片的路径并作为 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;
?>
Ajax:
$('#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 的方式:
pid_fk/fid(记住pid_fk是产品的id,fid是图片的id)
例如: 1/image1.jpg