Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

arrays - PHP Foreach Column Data Wise

I want to make for-each loop where div will echo qty as the diagram below - for example, the div will echo 2 times for product1 and the same for product2 6 times.

I know the for-each function in PHP but don't know how to echo some columns data-wise. I have also seen some threads on StackOverflow but it was not understood by me.

Please help me -

$stmt = $con->prepare("SELECT * FROM product_name");
$stmt->execute();
$result = $stmt->fetchAll();
 foreach ($result as $data => $logs) {
  if (!empty($logs)) { ?>
    <div class="myClass" id="count1"><?= $logs['name'] ?></div>
<?php } } ?>

table : product_name

|---------------------------------------|
| ID   |  name      |  qty              |
-----------------------------------------
| 1    | product1   |   2               |
-----------------------------------------
| 2    | product2   |   6               |
-----------------------------------------
question from:https://stackoverflow.com/questions/65880249/php-foreach-column-data-wise

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your requirement you need to use one another loop for print repeated row as per your qty field.

For(var i=1;i<=qty;i++){
<div class="myClass" id="count1"><?= $logs['name'] ?></div>
}

I am not php developer so you need to correct the syntax but i tried to help using logically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...