Crazy Horse
asked on
How to loop bootstrap columns which contain database records
I am trying to loop 2 col-md-6 columns using php. They are meant to display product names and loop through. I have simplified this a lot to make it easier to work with but I can hopefully implement it with the actual columns I am working with.
This obviously doesn't work. But I posted it as a starting point.
function bootloop($link) {
$stmt = $link->prepare("SELECT `prod_name` FROM `bb_products`");
$stmt->execute();
$result = $stmt->get_result();
if($result) {
while($row = $result->fetch_assoc()) {
$prod_name = sanitize($row['prod_name']);
$bootloop = <<<LOOPCOLUMNS
<div class="col-md-6 text-center">{$prod_name}</div>
<div class="col-md-6 text-center">Product name goes here</div>
LOOPCOLUMNS;
echo $bootloop;
}
}
$stmt->close();
}
This obviously doesn't work. But I posted it as a starting point.
What part doesn't work?
ASKER
Currently, it outputs like this.
Apple Juice Product name goes here
Grape Juice Product name goes here
Cranberry Juice Product name goes here
Some other juice Product name goes here
Obviously, it will because I have hardcoded "Product name goes here". But if I didn't do this and used my variables in both columns I would have:
Apple Juice Apple Juice
Grape Juice Grape Juice
Cranberry Juice Cranberry Juice
Some other Juice Some other juice
I want it to look like:
Apple Juice Grape Juice
Cranberry Juice Some other juice
Apple Juice Product name goes here
Grape Juice Product name goes here
Cranberry Juice Product name goes here
Some other juice Product name goes here
Obviously, it will because I have hardcoded "Product name goes here". But if I didn't do this and used my variables in both columns I would have:
Apple Juice Apple Juice
Grape Juice Grape Juice
Cranberry Juice Cranberry Juice
Some other Juice Some other juice
I want it to look like:
Apple Juice Grape Juice
Cranberry Juice Some other juice
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fantastic! Now we're getting somewhere. I added the SQL statement to retrieve the records from the database like this:
This is looping through the database records but the layout isn't quite 100%.
If I only have 3 records in the database it looks like this:
Apple Juice Grape Juice
Strawberry Juice
Instead of:
Apple Juice Grape Juice
Strawberry Juice
function bootloop($link) {
$stmt = $link->prepare("SELECT `prod_name` FROM `bb_products`");
$stmt->execute();
$result = $stmt->get_result();
if($result) {
while($row = $result->fetch_assoc()) {
$prod_name = sanitize($row['prod_name']);
// A SIMULATED QUERY RESULTS SET
$rows = [ $prod_name ];
// MAKE PAIRS OF RESULTS
$pairs = array_chunk($rows, 2);
// MAKE SURE THE LAST PAIR IS COMPLETE
$end = array_pop($pairs);
if (empty($end[1])) $end[1] = '';
array_push($pairs, $end);
// ITERATE OVER THE PAIRS TO PRODUCE THE RESPONSE DOCUMENT
foreach ($pairs as $pair)
{
$bootloop = <<<LOOPCOLUMNS
<div class="col-md-6 text-center">{$pair[0]}</div>
<div class="col-md-6 text-center">{$pair[1]}</div>
LOOPCOLUMNS;
echo $bootloop;
}
}
}
}
This is looping through the database records but the layout isn't quite 100%.
If I only have 3 records in the database it looks like this:
Apple Juice Grape Juice
Strawberry Juice
Instead of:
Apple Juice Grape Juice
Strawberry Juice
ASKER
Interestingly enough, if I remove this line:
It displays correctly. I just want to add some more products and see what it does...
<div class="col-md-6 text-center">{$pair[1]}</div>
It displays correctly. I just want to add some more products and see what it does...
Untested, but hopefully it communicates the intent of the design.
function bootloop($link) {
$stmt = $link->prepare("SELECT `prod_name` FROM `bb_products`");
$stmt->execute();
$result = $stmt->get_result();
$rows = [];
if($result) {
while($row = $result->fetch_assoc()) {
$rows[] = sanitize($row['prod_name']);
}
}
// MAKE PAIRS OF RESULTS
$pairs = array_chunk($rows, 2);
// MAKE SURE THE LAST PAIR IS COMPLETE
$end = array_pop($pairs);
if (empty($end[1])) $end[1] = '';
array_push($pairs, $end);
// ITERATE OVER THE PAIRS TO PRODUCE THE RESPONSE DOCUMENT
foreach ($pairs as $pair)
{
$bootloop = <<<LOOPCOLUMNS
<div class="col-md-6 text-center">{$pair[0]}</div>
<div class="col-md-6 text-center">{$pair[1]}</div>
LOOPCOLUMNS;
echo $bootloop;
}
}
ASKER
I think have this now. I just want to confirm that what I am doing is correct before closing out.
I have added 2 more variables that I want to display. This displays how I want it to and the data it should but I don't know if what I have done is correct.
I have added 2 more variables that I want to display. This displays how I want it to and the data it should but I don't know if what I have done is correct.
function bootloop($link) {
$stmt = $link->prepare("SELECT `prod_name`, `reg_price`, `sale_price` FROM `bb_products`");
$stmt->execute();
$result = $stmt->get_result();
if($result) {
while($row = $result->fetch_assoc()) {
$prod_name = sanitize($row['prod_name']);
$reg_price = sanitize($row['reg_price']);
$sale_price = sanitize($row['sale_price']);
// A SIMULATED QUERY RESULTS SET
$rows = [ $prod_name, $reg_price, $sale_price ];
// MAKE PAIRS OF RESULTS
$pairs = array_chunk($rows, 3);
// MAKE SURE THE LAST PAIR IS COMPLETE
$end = array_pop($pairs);
if (empty($end[1])) $end[1] = '';
array_push($pairs, $end);
// ITERATE OVER THE PAIRS TO PRODUCE THE RESPONSE DOCUMENT
foreach ($pairs as $pair)
{
$bootloop = <<<LOOPCOLUMNS
<div class="col-md-6 text-center">{$pair[0]} - {$pair[1]} - {$pair[2]}</div>
LOOPCOLUMNS;
echo $bootloop;
}
}
}
}
ASKER
Sorry, I must have posted just before you did. Will take a look...
The part about making sure "the last pair is complete" will need to be modified if you're using more than a pair!