0
I’m using Extjs 4 and PHP.
I have an image saved in a server folder and its path is saved in my database. I tried to do something like this:
Code in the controller:
onVerImgClick: function(btn, o, e0pts){
var grid = btn.up('grid');
var records = grid.getSelectionModel().getSelection();
var produtoId = records[0].data.id;
//var image = Ext.create('ExtMVC.view.verImagem');
Ext.Ajax.request({
method: 'POST',
url: 'php/importadorDadosImagem.php',
success: function(response){
var array = Ext.decode(response.responseText);
var cont = 0;
var caminho;
while(cont < array.data.length){
if(produtoId == array.data[cont].idProduto){
caminho = array.data[cont].imagem;
}
cont++;
}
cont = 0;
if(caminho != null){
var image = Ext.create('ExtMVC.view.verImagem');
image.getEl().set({
src: caminho
});
}
}
});
}
mage.js
Ext.define('ExtMVC.view.verImagem', {
extend: 'Ext.window.Window',
alias: 'widget.verimagem',
autoShow: true,
modal: true,
width: 400,
height: 400,
items: [
{
xtype: 'component',
autoEl: {
tag: 'img',
src: ' '
}
}
]
});
importadorDadosImage.php
<?php
include("connect.php");
$queryString = "SELECT idProduto, imagem FROM imagens";
//consulta sql
$query = mysql_query($queryString) or die(mysql_error());
//faz um looping e cria um array com os campos da consulta
$dados = array();
while($dado = mysql_fetch_assoc($query)) {
$dados[] = $dado;
}
$total = count($queryString);
echo json_encode(array(
"success" => mysql_errno() == 0,
"total" => $total,
"data" => $dados
));
This way, no error is shown. However, the image does not appear.
In the database I have saved the full path of the image. For example: . /uploads/Jellyfish.jpg .
– Marcelo Augusto
@Marceloaugusto beware of this passage . / in my experience, I saw that this always error, instead of this, use a full url request, and see if only this will make the image appear, manually put in a variable with http and everything and see if it works, if yes, just create a request of your full url
– flourigh
If I put the full path does not give. If I put . /uploads/Jellyfish.jpg in the src of the script verImagem.js, leaving it static, the image appears.
– Marcelo Augusto
@Marceloaugusto then if you put static it appears with this formatting, already if you search the database it does not appear, then your error is not in js but in the database, as you are searching in the database?
– flourigh
I added the PHP code where I import the image data.
– Marcelo Augusto
I have all the products on a grid. To add an image, I select the product and click on a button. To see the image, it’s the same way. In BD, I have a table for the products and another table for the images. So to reference an image, I get the id of the selected product. When recovering, I compare the image id with the product id, if it is equal and take the path and try to set in src. But it is not setando.
– Marcelo Augusto
@Marceloaugusto try using PDO you will see that it is much simpler and safer, follow an example. , but I saw that you didn’t add a search criteria to your querystring
– flourigh
Have you checked if php’s return is coming to ajax? Because as I saw you did not set the header in php, put this before echo: header('Content-Type: application/json');
– Luis Henrique
<?php try { $a = new PDO("mysql:host=" . $host . "; dbname=" . $base, $user, $pass);
$a -> setAttribute(PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
$a -> exec("set names utf8");
$b = $a -> prepare("SELECT idProduto, imagem FROM imagens WHERE idProduto = :var");
$b -> execute(array(":var" => $var));
$c = $b -> fetchAll();
if(count($c)) {
foreach($c as $d) {
echo $d["imagem"]; } }
$a = null; } catch(PDOException $e) { echo $e -> getMessage(); } ?>
– flourigh
Do not post code in the comments, edit your reply and the suggestion there.
– Luis Henrique
@fly flourigh the return is correct yes. I put it on the console and it’s coming out right. The header is set in connect.php.
– Marcelo Augusto
I suggest that you debug in parts then, set a static string for src instead of the path variable, if you pass change level until you get to where it fails.
– Luis Henrique
because, you have to see in each part where is the error, because as I understood, the variable of one that is the path, is not passing to the variable of another to display the image, I mean, Voce search with php and inserts the path in a variable, this path then passes to another variable of javascrip and then is displayed, well, in case, Voce opens a product, this product has an id this id has multiple images these images display in various places, ie when ordering the product it creates an array with all images, if Voce creates a loop, it displays the images before going to javascript?
– flourigh
I was able to load the images as I wanted. I created another variable and made it receive the component that has xtype = 'image'. From there, I was able to do the novaVariable.setSrc(path); The problem now is how I save the path to the folders @flyflourigh spoke about. I am sending the way. /uploads/ it does not save and shows the following error: Warning: move_uploaded_file(./uploads/Koala.jpg): failed to open stream: No such file or directory in C: Program Files (x86) Easyphp-12.1 www app extjs-crud-mvc php receivedUpload.php on line 56.
– Marcelo Augusto
If I put .. /uploads/ it saves, but at the time of seeing the image it does not found, that is, it does not find the folder with the image.
– Marcelo Augusto
for this, just create two variables, one to insert the image in the database that would be a . /uploads/ and another to move the image that is a .. /uploads/
– flourigh
basically you will concatenate a point . before the other point .. when moving only
– flourigh
Ready! It worked. Thanks, guys!
– Marcelo Augusto