0

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); ?>
  • Shouldn't there be a `?>` ? after `$totalRows_products = mysql_num_rows($products);` – frunkad Aug 26 '15 at 20:28
  • Assuming you're using bootstrap as for the div classes, you can iterate $row_products with `foreach` creating divs and an internal counter like `$i=1` that increments every loop. Then you put an `if($i == 4)` at the end of the loop, which when tested, closes the row and starts a new one. – al'ein Aug 26 '15 at 20:31
  • Why you use do-while? Why not just while? – SarDau Mort Aug 26 '15 at 20:31
  • Remove 3 of 4 `
    ` and leave only one?
    – u_mulder Aug 26 '15 at 20:37
  • Darshan - since the next line is also php - I don't think it was needed – 1537news Aug 27 '15 at 01:45
  • SarDau - This is migrated code from many years ago - I used it to get something working - as I mentioned it has worked well for line by line db output - this is the first time I am attempting 4 rows in a line. – 1537news Aug 27 '15 at 01:47
  • u_mulder - I tried something like that and just got the same record 4 times across – 1537news Aug 27 '15 at 01:49
  • Aedix - I think your idea is exactly what I am looking for - yes it is bootstrap - also something relatively new to me - there have been so many "new" frameworks in the past 5 years - I will give your idea a go – 1537news Aug 27 '15 at 01:59

2 Answers2

1

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.

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Riggs - both your points are good - I am migrating about 5000 pages of old php - this happens to be a brand new project - I did not expect - I have started the pdo on some of my groups of pages - I find this leap to pdo quite unintuitive - makes my brain hurt. My big hurdle here was to get the database record to flow left to right 4 times then go to the next row - after I have that working - I can move on to styling - that whole line of code will be dynamically generated after I know what sizes the images are going to be . – 1537news Aug 27 '15 at 02:03
  • I want to thank everyone here for their input - anyone have a few good pages on prepared statements - this also seems to be a struggle for my brain to understand - the migration to php 5.5 - 7.0 is really a bear for me – 1537news Aug 27 '15 at 02:05
0

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>
al'ein
  • 1,711
  • 1
  • 14
  • 21