1537news
1537news

Reputation: 1

mysql fetch assoc into columns

I have used the do while for years but has always be in line by line output

What I am wanting to do is output into 4 columns -

I have tried several things from various other questions but can't seem to lock it down.

In this example I get the same info across all four columns -

Not quite understanding how to get the individual row counter to click for each column -

Please excuse any formatting grossness - not quite sure about this interface

mysql_select_db($database_products, $products);
        $query_products = "SELECT productURL, productName, price FROM products WHERE      productName LIKE '%earring%' AND merchantId = 37638 ORDER BY id ASC";
        $products = mysql_query($query_products, $products) or die(mysql_error());
        $row_products = mysql_fetch_assoc($products);
        $totalRows_products = mysql_num_rows($products);


    <?php do { ?>  

            <div class="col-md-3">

              <img src="<?php echo $row_products['productURL']; ?>" width="200"><br>
                <span class="dM"><?php echo $row_products['productName']; ?></span> <br>
                <span class="dN">$<?php echo $row_products['price']; ?></span><br>
              <p><a class="btn btn-default" href="#" role="button">add to cart &raquo;</a></p>
            </div>


            <div class="col-md-3">
            <img src="<?php echo $row_products['productURL']; ?>" width="200" height="auto"><br>
                <span class="dM"><?php echo $row_products['productName']; ?></span> <br>
                <span class="dN">$<?php echo $row_products['price']; ?></span><br>

              <p><a class="btn btn-default" href="<?php echo $row_products['productLink']; ?>" role="button">add to cart &raquo;</a></p>
           </div>


            <div class="col-md-3">
            <img src="<?php echo $row_products['productURL']; ?>" width="200"><br>
                <span class="dM"><?php echo $row_products['productName']; ?></span> <br>
                <span class="dN">$<?php echo $row_products['price']; ?></span><br>
              <p><a class="btn btn-default" href="<?php echo $row_products['productLink']; ?>" role="button">add to cart &raquo;</a></p>
            </div>


            <div class="col-md-3">
            <img src="<?php echo $row_products['productURL']; ?>" width="200"><br>
                <span class="dM"><?php echo $row_products['productName']; ?></span> <br>
                <span class="dN">$<?php echo $row_products['price']; ?></span><br>
              <p><a class="btn btn-default" href="<?php echo $row_products['productLink']; ?>" role="button">add to cart &raquo;</a></p>
       </div> 
               <?php } while ($row_products = mysql_fetch_assoc($products)); ?>
<?php mysql_free_result($products); ?>

Upvotes: 0

Views: 117

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

I am assuming this is what you are after i.e. the four items from the row across the page, repeated for each row read from the database.

mysql_select_db($database_products, $products);
$query_products = "SELECT productURL, productName, price 
                   FROM products 
                   WHERE productName LIKE '%earring%' 
                     AND merchantId = 37638 
                   ORDER BY id ASC";
$products = mysql_query($query_products, $products) 
             or die(mysql_error());

$totalRows_products = mysql_num_rows($products);


while ($row_products = mysql_fetch_assoc($products) ) : 
?>  
    <div class="col-md-3">
        <img src="<?php echo $row_products['productURL']; ?>" width="200">
    </div>

    <div class="col-md-3">
        <span class="dM"><?php echo $row_products['productName'];?></span>
    </div>

    <div class="col-md-3">
        <span class="dN">$<?php echo $row_products['price']; ?></span>
    </div>

    <div class="col-md-3">
        <p>
         <a class="btn btn-default" href="<?php echo $row_products['productLink']; ?>" role="button">add to cart &raquo;</a>
        </p>
    </div>

<?php 
endwhile;  

mysql_free_result($products); 
?>

As you are using a modern layout tool, you should also change width="200" to an inline style at least i.e. style="width:200px"

You are also using the mysql_ extension which is deprecated and to be totally removed in PHP7 out in a few months. You should look to move this to mysqli_ or PDO see this document for a bit of help on that.

Upvotes: 1

al&#39;ein
al&#39;ein

Reputation: 1729

Based on my comment, see if it works for you.

<div class='row'>
<?php $i=1; for ($x=0;$x<count($row_products);$x++) { ?>  
    <div class="col-md-3">
        <img src="<?php echo $row_products['productURL']; ?>" width="200"><br>
        <span class="dM"><?php echo $row_products['productName']; ?></span> <br>
        <span class="dN"><?php echo $row_products['price']; ?></span><br>
            <p><a class="btn btn-default" href="#" role="button">add to cart &raquo;</a></p>
    </div>
<?php if ($i == 4) {
        echo "</div><div class='row'>";
        $i = 1;
    } else {
        $i++;
    }
}
?>
</div>

Upvotes: 0

Related Questions