0 votes

Get query for multiple results

I am creating with php and mysql a query to a table with the same user id there are multiple product id. I have already achieved that does not show me the product id is repeated, but not how to get the results if it is not using an array. I've done this:

$sql2=$db->prepare('SELECT id_user,id_product FROM contracts WHERE id_user=:id_user GROUP BY id_product');
foreach($id_product[2] as $value){

Below, I call the id_product specific as well:

    echo "<p>Cuenta corriente Básica:</p>";
    echo "<a href='products/cuentas/cuenta_corriente.php?id_product=".$array[0]."'>Ir a mi cuenta básica</a>";

And as well two other times, either the id_product 2 or 3:




This way only I got that I show in the case of 3, that is to say, the last.


itsnesky Points 11

If you want to get the digits 1 and 3 separately, you could pass your variable to an array and then operate with the numbers separately.

$array = [];

foreach($id_product[2] as $value) {
     $array  = array_map('intval', str_split($value)); //=> array = [1, 3]

if($array[0] == 1) { //=> array[0] = 1, la condición se cumple


As I mentioned before, right now you have a array all id of the products associated to that user.

If you want to add as many links as products have a user does not have to do conditionals according to the type of id, you would be to print the custom links for each product by modifying their respective id.

Like so:

$array = [];

foreach($id_product[2] as $value) {
     $array  = array_map('intval', str_split($value)); //=> array = [1, 3]

 * Siguiendo el ejemplo que has puesto (array = [1, 3]):
 * Para la primera iteración la url sería:
 * products/cuentas/cuenta_corriente.php?id_product=1
 * Para la segunda iteración la url sería: 
 * products/cuentas/cuenta_corriente.php?id_product=3

foreach($array as $id) {
         echo "<a href='products/cuentas/cuenta_corriente.php?id_product=".$id."'>Ir a mi cuenta básica</a>";


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: