We get the ids of the products (the unique identifier) primary
which is theid_product
$smtm = $con->prepare("SELECT id_product FROM products WHERE active=?");
$smtm->bind_param("i",$active);
$active = "1";
$smtm->execute();
$smtm->store_result();
if ($smtm->num_rows>0) {
//Si existen registros en la tabla
$smtm->bind_result($id_product);
while ($smtmId->fetch()) {
echo ''.$id_product.'';
}
} else {
//Si no existe registros en la tabla
echo "No se encontro registro";
}
Now the verification if a user bought or not a product is verified by if (checkSubscribeComplete($id_product) === true){}
checking with the ids that the variable contains$id_product
The data received by the variable
$id_product
is:12345678910
Point to the problem The variable $id_product
is taking a value 12345678910
and it is not, it is a result that will contain the values 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
, and 10
when writing them without separation inside a loop they look like a12345678910
Now remembering that the check
if (checkSubscribeComplete($id_product) === true) {}
checks if a user has a product or more than one purchased product, the problem is that the check is receiving the following data12345678910
and is checking it in its query as if it wereid
a product when it is not.
Example: the product you are looking for (and this product does not exist)
| id_product | product | active |
12345678910 x 1
Checking:
if (checkSubscribeComplete($id_product) === true){}
Receive the following:
if (checkSubscribeComplete(12345678910) === true){}
Therefore the verification will always be false false
because it is receiving erroneous data.
How to check a value for value to identify the purchased product?
That is to say, send data to the check as follows:
if (checkSubscribeComplete(1) === true){}
and so on:
if (checkSubscribeComplete(2) === true){}
or as follows:
if (checkSubscribeComplete(1,2,4,5,6,7,8,9,10) === true){}
Instead of sending the data like this:
if (checkSubscribeComplete(12345678910) === true){}
*I have a user who bought a product, a product with a id
(one)1
The Fuction
to check if a user has purchased products.
function checkSubscribeComplete($id_product){
global $con;
global $id_user;
$res = false; // the default result
$stmt = $con->prepare("SELECT status FROM order WHERE id_product=? AND UserId=? AND status = ?");
$stmt->bind_param("iis",$id_product,$UserId,$status);
$status='complete';
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows ===1){
$res = true;
} else {
$res = false;
}
return $res;
}
What do I want to do?
In the product catalog page I am going to show all the products of the table products
now if a user logs in to the system and if he does not have purchased products the products will be shown without any change, it will also happen when there are no users logged in to the system, but if a user has one or more purchased products in the list of products in the catalog I want to hide the price of that product that the user has purchased and show another data instead of the price.
But I can't get the desired results:
$smtm = $con->prepare("SELECT product,price FROM products WHERE active=? order by id_product ASC");
$smtm->bind_param("i",$active);
$active = "1";
$smtm->execute();
$smtm->store_result();
if ($smtm->num_rows>0) {
$smtm->bind_result($product, $price);
while ($smtm->fetch()) {
echo '<div class="four-column middle-pro">
<div class="all-description">
<h2>'.$product.'</h2>';
if (checkSubscribeComplete($id_product) === true) {
echo '<a href="#">Ver factura</a>';
echo '<h2>'.$product.'</h2>';
echo 'Producto comprado :)';
} else {
//Si el producto no esta comprado mostramos el precio del producto
echo '<h3>'.$price.'</h3>';
}
echo'
</div>
</div>';
echo '';
}
} else {
echo "No existen productos";
}
Han realizado una tormenta en un vaso de agua, se han complicado pero así es el amor a la programación :)
Vamos por parte
¿Cómo comprobar un valor por valor para identificar el producto adquirido?
Para obtener este resultado
Se deben realizar cambios en la consulta, de la siguiente forma:
Debes cambiar en la comprobación el
$id_product
por lo siguiente$array
Ahora recibirás los datos de siguiente forma
1
,2
,3
...Ahora para solucionar el problema del catalogo de productos.
Debes modificar la consulta a:
Ahora para que puedas ocultar el precio del producto que un usuario haya comprado debes hacer el siguiente cambio.
Suerte :)
EDITION INCLUDING PROOF OF CONCEPT, SHOWING WHAT I TOLD YOU IN VARIOUS COMMENTS
I have reproduced your program on a real stage.
This is my proposal.
Since you want to display all products, we will make two queries to the database and store their results in an array:
One to get all the products from the products table:
The result of this query will be the following array:
Another query to get the ids of products the user has purchased. We will use as an example user 1, who bought products 1 and 3.
Notes :
I have saved the table design that I mentioned below, seeing that
ordenes
you had a date column in the table. If you don't create a tableordenes_detalles
you will have to repeat that date column in the tableordenes
for each product purchased in the same order unnecessarily.I don't know what the column is for
active
. I suspect it has no use here. But that's not important.The result of that query is the following array:
We will then read the array of all products, and do a comparison with the array of products the user has purchased. If we find any product id in that array, we will change the HTML code.
We do it in this piece of code:
The result will be similar to the one you show in your image. In the proof of concept user 1 has bought products 1 and 3. You will see that it says View invoice , while in the others, the price is shown.
Of course, I have not applied CSS rules or anything like that, that will be your job. I haven't put links to View Invoice either ... all that remains is the least of it, you just change the value of the variables.
Result:
Data used for the test
Boards:
PHP code
I hope it helps you.
Greetings.
PREVIOUS ANSWER WITH ALSO VALID INFORMATION
After a few comments on this and another deleted question , I'm encouraged to answer you (partially).
The truth is that, no offense intended, the more you explain yourself, the less I understand you :)
From everything I have read, I have understood that what you want is to identify the products that the user has purchased. If you have purchased them, you want to show them, but indicating a product download link.
En un comentario te hablaba de que hay un error de lógica con respecto a la forma en que estás concibiendo el programa.
¿Dónde está el error de lógica?
En que tú quieres consultar por
id_producto
, cuando en realidad deberías consultar porid_usuario
, traer todos los productos que el usuario ha comprado y mostrarlos con su respectivo enlace.Además de un error de lógica, tienes (intuyo) un error de diseño de datos
Deberías tener una tabla
usuarios
, una tablaproductos
, pero además una tablacompras
y otracompras_detalles
por llamarla de alguna manera. La constitución de dichas tablas sería más o menos esta.usuarios
id_usuario
usuario
productos
id_producto
producto
precio
url
url_img
compras
id_compra
fecha_compra
id_usuario
compras_detalles
id_compra
id_producto
cantidad
Veamos un pseudo_código:
La función sería algo como esto:
Voy a usar datos ficticios... Debes adaptarlos.
Cómo llamar esa función pasándole datos dinámicos
Esa era una de tus preguntas.
La función la llamas desde donde obtienes el id del usuario.
Por ejemplo:
Ahí estás llamando la función y pasándole el id de usuario actual.