0

I am trying to make this query loop while value in column Producto_Prioridad = 1 in table Natan_Procesos. So, I pretty much need all the code below to loop.

<div align="center">
<div align="center" style="width:   -600; -moz-box-align: center; alignment-adjust: central; alignment-baseline: central; vertical-align: central; z-index: auto;"><p>&nbsp;</p>
<p> Opciones de <? echo $rows['giro2']; ?></p>
<?php

try {
      $sth = $db->prepare("SELECT Proveedor_Logo, Giro2_ID, Producto_Nombre, Producto_Descripcion, Producto_Precio, Producto_Imagen, Producto_Prioridad FROM Natan_Procesos WHERE Producto_Prioridad = 1");
      $sth->execute(array($ead,$eac));
      $row = $sth->fetch(PDO::FETCH_ASSOC);
       $Proveedor_Logo = $row['Proveedor_Logo'];
        $Giro2_ID = $row['Giro2_ID'];
        $Producto_Nombre = $row['Producto_Nombre'];
        $Producto_Descripcion = $row['Producto_Descripcion'];
        $Producto_Precio = $row['Producto_Precio'];
        $Producto_Imagen = $row['Producto_Imagen'];
        $Producto_Prioridad = $row['Producto_Prioridad'];
        } catch (PDOException $e) {
        echo 'Database operation failed: ' . $e->getMessage();
    }
?>        </div></div>   
<div align="center">
<div align="center" style="width:   -600; -moz-box-align: center; alignment-adjust: central; alignment-baseline: central; vertical-align: central; z-index: auto;"> 
<table width="800" border="0">
  <tr>
  <td><input name="platofuerte" type="radio" value="" /></td>
    <td width="250"><? echo "<img src='". htmlspecialchars($row['Proveedor_Logo'])."' alt='image' />" ?></td>
    <td><? echo $row['Proveedor_Nombre_Comercial']; ?>&nbsp;</td>
    <td><? echo "<img src='". htmlspecialchars($row['Producto_Imagen'])."' alt='image' />" ?>&nbsp;</td>
    <td width="100"><? echo $row['Producto_Nombre']; ?>&nbsp;</td>
    <td width="200"><? echo $row['Producto_Descripcion']; ?>&nbsp;</td>
    <td width="10">$<? echo $row['Producto_Precio']; ?>&nbsp;</td>
  </tr>
</table>
</div></div>

So far it only outputs 1 row when there are 3 rows with value =1 in column Producto_Prioridad from the same table. How do I implement the while loop to show as many rows that fulfill the condition?

Thanks for the help.

  • Natan, there is not enough information here to provide a valid question. What is the expected transformation? What is the expected output? What is it doing now that you don't want it to? – Dykotomee May 23 '16 at 11:15
  • It's because you only output 1 result, not all of them. `fetch` takes 1 row at a time. If you want to output all rows, you have to loop the result from your query and fetch each row. – Michel May 23 '16 at 11:55
  • Or use `fetchAll()`. Take a look [at this question](http://stackoverflow.com/questions/18435317/pdo-return-all-rows) – Michel May 23 '16 at 11:57
  • I believe that question is based on limits of rows instead of all matching a condition? and I wonder how the data would be displayed with fetchall() with a table to fill below without a while? – Natan Coats May 24 '16 at 06:31

1 Answers1

1

fetch only grabs a single result from your query. Try wrapping it in a loop to catch all of the data, then iterating over the data during your output:

<div align="center">
    <div align="center" style="width: -600; -moz-box-align: center; alignment-adjust: central; alignment-baseline: central; vertical-align: central; z-index: auto;">
        <p>&nbsp;</p>
        <p> Opciones de <? echo $rows['giro2']; ?></p>
        <?php

        $rows = array();
        try {
            $sth = $db->prepare("SELECT Proveedor_Logo, Giro2_ID, Producto_Nombre, Producto_Descripcion, Producto_Precio, Producto_Imagen, Producto_Prioridad FROM Natan_Procesos WHERE Producto_Prioridad = 1");
            $sth->execute(array(
                $ead,
                $eac
            ));
            while ($row = $sth->fetch(PDO::FETCH_ASSOC) ) {
                $Proveedor_Logo = $row['Proveedor_Logo'];
                $Giro2_ID = $row['Giro2_ID'];
                $Producto_Nombre = $row['Producto_Nombre'];
                $Producto_Descripcion = $row['Producto_Descripcion'];
                $Producto_Precio = $row['Producto_Precio'];
                $Producto_Imagen = $row['Producto_Imagen'];
                $Producto_Prioridad = $row['Producto_Prioridad'];
                $rows[] = $row;
            }
        } catch (PDOException $e) {
            echo 'Database operation failed: ' . $e->getMessage();
        }
        ?>
    </div>
</div>
<div align="center">
    <div align="center"
         style="width: -600; -moz-box-align: center; alignment-adjust: central; alignment-baseline: central; vertical-align: central; z-index: auto;">
        <table width="800" border="0">
            <?php
            foreach ($rows as $row) {
                ?>
                <tr>
                    <td>
                        <input name="platofuerte" type="radio" value="" />
                    </td>
                    <td width="250">
                        <img src='<?php echo htmlspecialchars($row['Proveedor_Logo']); ?>' alt='image' />
                    </td>
                    <td>
                        <? echo $row['Proveedor_Nombre_Comercial']; ?>&nbsp;
                    </td>
                    <td>
                        <img src='<?php echo htmlspecialchars($row['Producto_Imagen']); ?>' alt='image' />&nbsp;
                    </td>
                    <td width="100">
                        <? echo $row['Producto_Nombre']; ?>&nbsp;
                    </td>
                    <td width="200">
                        <? echo $row['Producto_Descripcion']; ?>&nbsp;
                    </td>
                    <td width="10">
                        $<? echo $row['Producto_Precio']; ?>&nbsp;
                    </td>
                </tr>
                <?php
            }
            ?>
        </table>
    </div>
</div>
Dykotomee
  • 728
  • 6
  • 20
  • This worked perfectly. Thanks. I had the idea of fetch, but couldnt get the loop to go. Your help is much appretiated. Now this will also provide for no row having a value =1 and not get an error? – Natan Coats May 24 '16 at 05:37
  • Yes, it should. Using `foreach` will prevent any iterations should the array be empty. If you want to test it out, add ` AND 1=0` onto the end of your SQL query and see what happens! – Dykotomee May 24 '16 at 15:07