Printing HTML dom inside PHP foreach loop - php

I'm trying to make an accordion for each row in the database using the Foreach loop
I tried this but I'm getting only one accordion out its supposed to make more than 2 accordion deppending on rows count this what i have tried :-
<?foreach ($fetchedDrafts as $draft):?>
<div id="<?=$draft['name'];?>" role="tablist" class="border border-danger rounded" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" style="height:120px !important;" id="section1HeaderId">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordianId" href="#section1ContentId" aria-expanded="true" aria-controls="section1ContentId">
<center>
<!---->
<button type="button" class="btn btn-warning pill" style="display:none;"><?$draft['name'];?></button>
<h3 class="Title" style="display:none;font-family:hana;"></h3>
</center>
<img class="accordianimg pull-left" style="width:180px; height:120px;" src="..\upload\gsk.png" alt="alt"></img>
</a>
<div class="input-container" style="margin-top:2rem !important; margin-right:6rem;">
<h2>
<button onclick="funvction()" class="btn btn-danger SearchButton"style="font-family:hana;">بحث</button>
<input class="border border-danger rounded SearchInput" type="text" style="width:440px;"id="SearchInput" placeholder="البحث عن المنتج" name="DraftSearchFiled" onchange="DraftSearch()">
</h2>
</div>
</div><br>
<div id="section1ContentId" class="collapse in" role="tabpanel" aria-labelledby="section1HeaderId" style="overflow-x: hidden !important;" onscroll="">
<div class="card-body d-flex flex-wrap">
<div class="row justify-content-center">
<div id="demo" class="carousel slide carousel" data-ride="carousel" style="margin-left:20rem;width: 26rem; height:31rem;">
<!-- no need for indicators -->
<div class="carousel-inner canner">
<?
$i = 0;
foreach($DRTitems as $row){
$actives = '';
if($i == 0){
$actives ='active';
}
?>
<div class="carousel-item <?= $actives;?>">
<div class="container">
<div class="row justify-content-center">
<div class="card" style="width: 26rem; height:auto;">
<? if($row['Sale'] == true){
echo'<div class="corner-ribbon top-left sticky shadow">تخفيض</div>';
}else{
echo'<div class="corner-ribbon top-left-green sticky shadow">افضل سعر</div>';
}
?>
<img class="card-img-top" src="<?= $row['imageurl'] ?>" alt="Drug" style=""></img>
<div class="card-body body">
<h1 class="card-title Productname " style="font-family:Tahoma !important; font-size:42px !important;"><?= $row['name'] ?></h1><br><br><br>
<p class="chemicalcom"><?= $row['chemicalcom']?></p><br>
<p class="price"><?= $row['price'] ?>$</p>
<input type="submit" class="btn btn-success btn-sm btn-block btn" value="أضف للسلة"></input>
</div>
</div>
</div>
</div>
</div>
<? $i++; }?>
</div>
<?php endforeach; ?>
what i have in db is 3 rows but the code makes only one accordion

Related

How to access form element in while loop in PHP

i want to get acces to comment and get the value of it , i used $_POST but i get error
Undefined array key
the code create photo posts with while loop the post also contains the comment section.
here the Photo post code:
<!-- Photos section -->
<section>
<div class="container mt-5">
<div class="album py-5 bg-light">
<div class="row row-cols-1 row-cols-sm-1 row-cols-md-1 row-cols-lg-1">
<div class="container">
<?php
if (mysqli_num_rows($res_for_pics) > 0) {
while ($pics = mysqli_fetch_assoc($res_for_pics)) { ?>
<div class="col mb-3">
<div class="card shadow-sm">
<img src="images/user/<?= $pics['pic'] ?>" class="card-img-top img-fluid" alt="...">
<div class="card-body">
<h5 class="card-title" id="add-com"><?php echo $pics['pic_title'] ?></h5>
<p class="card-text"><?php echo $pics['pic_des'] ?></p>
<div class="d-flex justify-content-between align-items-center">
<small class="text-muted mb-3">by nithan</small>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<h5 class="card-title"> Comments</h5>
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div class="accordion-body">
<ul class="list-group list-group-flush">
<li class="list-group-item">
howa
<p class="card-text"><small class="text-muted"></small></p>
</li>
</ul>
</div>
</div>
</div>
**I Want to get access to the value of textarea in the other php file**.
<form method="$_POST" action="../model/comment.php">
<div class="mb-3 mt-5">
<h5 for="message-text" class="col-form-label">add your comment</h5>
<textarea type="text" class="form-control" id="message-text" name="theComment"></textarea>
</div>
<button type="submit" class="btn btn-primary text-light" name="addComment">Comment</button>
</form>
</div>
</div>
</div>
<?php }
} ?>
</div>
</div>
</div>
</div>
</section>
and here comment.php
<?php
include('db_con.php');
$comment = $_POST["theComment"];
echo $comment;
So i try to get access to the 'theComment' value from the form .

Bootstrap grid does not show properly

I've been working on my webpage but somehow bootstrap would not show it properly. Below is my code:
<div class="main-panel">
<div class="content-wrapper">
<button type="button" class="btn btn-primary btn-rounded btn-icon" data-toggle="modal" data-target="#newGroupModal">
<i class="ti-plus"></i>
</button>
<div class="row">
<form method="post">
<?php
foreach($data as $row) {
?>
<div class="col-md-4">
<div class="card-body">
<div class="card">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">
<?=$row['group_name']?>
</h5>
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<?=$row['group_id']?>">View Group</a>
</div>
</div>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>
I want it to be 3 column in a row but I ended up like this:
Click here to view the picture
I'm not sure whats wrong with my code, if anybody could help me identify it would be great. Thank you so much
<div class="main-panel">
<div class="content-wrapper">
<button type="button" class="btn btn-primary btn-rounded btn-icon" data-toggle="modal" data-target="#newGroupModal">
<i class="ti-plus"></i>
</button>
<div class="row">
<?php
foreach($data as $row) {
?>
<div class="col-4">
<form method="post">
<div class="card"> <div class="card-body">
<img class="card-img-top" src="../../template/images/banner.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">
<?=$row['group_name']?>
</h5>
<p class="card-text"></p>
<a class="btn btn-primary" href="view_group.php?group_id=<?=$row['group_id']?>">View Group</a>
</div>
</div>
</div>
</form>
</div>
<?php } ?>
</div>
</div>
</div>
Explaination:
Change position of: <form> and <form>

Multiple Bootstrap accordion are opening and closing at the same time

Here is the situation. I have an accordion, setup and the problem is that multiple accordions are opening and closing the same time.
Here is the code:
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-<?php echo $x; ?>">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</a>
</h4>
</div>
<div id="collapse-<?php echo $x; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading-<?php echo $x; ?>">
<div class="panel-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"> </iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Any help would be appreciated.
Thank you,
Kevin Davis
You can use latest Bootstrap 4 Accordion. More about it here https://getbootstrap.com/docs/4.3/components/collapse/#accordion-example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="heading-1">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="true" aria-controls="collapse-1">
111
</button>
</h2>
</div>
<div id="collapse-1" class="collapse show" aria-labelledby="heading-1" data-parent="#accordionExample">
<div class="card-body">
111
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-2">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-2" aria-expanded="false" aria-controls="collapse-2">
222
</button>
</h2>
</div>
<div id="collapse-2" class="collapse" aria-labelledby="heading-2" data-parent="#accordionExample">
<div class="card-body">
222
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-3">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-3" aria-expanded="false" aria-controls="collapse-3">
333
</button>
</h2>
</div>
<div id="collapse-3" class="collapse" aria-labelledby="heading-3" data-parent="#accordionExample">
<div class="card-body">
333
</div>
</div>
</div>
</div>
So your code will be
<div class="accordion" id="accordionExample">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="card">
<div class="card-header" id="heading-<?php echo $x; ?>">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</button>
</h2>
</div>
<div id="collapse-<?php echo $x; ?>" class="collapse<?php if ($x == 0) { ?> show<?php } ?>" aria-labelledby="heading-<?php echo $x; ?>" data-parent="#accordionExample">
<div class="card-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"></iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Added this class="collapse<?php if ($x == 0) { ?> show<?php } ?>" for the first box to be opened.
If you have more then one accordion on one page - don't forget to change id="accordionExample" and data-parent="#accordionExample", maybe by adding them an increment for counting accordions.

I want to repeat row after 2 columns where datas are retrieved dynamically in php

Upon doing this I am gating the data in an un alligned manner. I am getting 2 datas but the third data is coming under second data and 4th below first data but 3rd and forth are not in a single row there placed one after the other.
My View
<div class="container col-md-9">
<?php
foreach($unidet as $tempuni)
{
?>
<div class="service-items col-md-6">
<h4 class="title-normal"><img class="img-responsive thumb" src="images/service/service-icon1.png" alt=""><strong><?php echo $tempuni['name'] ?></strong></h4>
<div class="row">
<div class="col-sm-6 service-item-img">
<img class="img-responsive thumbnail" src="admin/<?php echo $tempuni['image'];?>" alt="">
</div>
<div class="col-sm-6 service-item-content">
<p><?php echo $tempuni['description'];?></p>
<button type="button" class="btn btn-warning btn-sm" onclick="more(<?php echo $tempuni['id'];?>)">Read More</button>
</div>
</div>
</div>
<?php
}
?>
</div>
This is what I am getting
Nice solution would be to use array_chunk()
<?php foreach (array_chunk($unidet, 2) as $row): ?>
<div class="row">
<?php foreach ($row as $tempuni): ?>
<div class="service-items col-md-6">
<h4 class="title-normal"><img class="img-responsive thumb" src="images/service/service-icon1.png" alt=""><strong><?php echo $tempuni['name'] ?></strong></h4>
<div class="row">
<div class="col-sm-6 service-item-img">
<img class="img-responsive thumbnail" src="admin/<?php echo $tempuni['image'];?>" alt="">
</div>
<div class="col-sm-6 service-item-content">
<p><?php echo $tempuni['description'];?></p>
<button type="button" class="btn btn-warning btn-sm" onclick="more(<?php echo $tempuni['id'];?>)">Read More</button>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>

pass different variables in bootstrap modal

i am fetching few results from mysql db where each result has link ,on clicking that link a bootstrap modal should open.
So,everytime modal should shows content related to selected result.
My problem is its only taking 1st variable of loop.
My code is:
<?php
$id = 1;
while($row = mysqli_fetch_array($result))
{
$title = $row['content_title'];
$description = $row['content_description'];
$content_url = $row['content_url'];
$icon_url = $row['thumb_icon_url'];
$id++;
$_SESSION['result'][$id] = array('content_title' => $title, 'content_description' => $description, 'content_url' => $content_url, 'thumb_icon' => $icon_url);
?>
<?php include 'templates/div_data.php'; ?>
<?php
}
}
?>
div_data.php
<div class="col-md-3 photo-grid " style="float:left">
<div class="well well-sm" target="_blank">
<figure>
<img class="img" alt="PDF" src="<?php echo $icon_url; ?>" >
<figcaption id="hide"> <p> <a data-toggle="modal" data-target="#myModal">Preview</a></p></figcaption>
</figure>
<a href="final.php?id=<?php echo $id; ?>&name=<?php echo $title; ?>" target="_blank">
<h4><small><?php echo $title; ?></small></h4>
</a>
<br>
<ul style="list-style: none;" class="container-fluid">
<li class="col-md-4 col-xs-4 pull-left" id="hide">Preview</li>
<li name="id" class="col-md-6 col-xs-6"> View Full Doc</li>
<li class="col-md-2 col-xs-2"><i class="fa fa-eye" aria-hidden="true"> 100</i></li>
</ul>
</div>
</div>
<a data-toggle="modal" class="pull-right" data-target="#myModal"></a>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Preview</h4>
</div>
<div class="modal-body">
<form action="final.php?id=<?php echo $id; ?>&name=<?php echo $title; ?>" class="form-horizontal" role="form" method="post">
<div class="embed-responsive embed-responsive-16by9 col-md-8 col-sm-8 col-xs-8" >
<iframe id="iframe" class="embed-responsive-item item"src="web/viewer.html?file=https://cors-anywhere.herokuapp.com/<?php echo $content_url; ?> &embedded=true#toolbar=0&navpanes=0&scrollbar=0" frameborder="0" ></iframe>
</div>
<h3 class="full col-md-3"><?php echo $title; ?></h3>
<hr> <br>
<p class="col-md-4">Description:</p>
<p class="col-md-4"><?php echo $_SESSION['result'][$id]['content_description']; ?>
</p>
<p class="col-md-2">Shared by: Sarthak tuteja</p>
<p class="col-md-2">Views:200000</p>
<div class="col-md-4">
<button type="submit" id="btnFilter" formtarget="_blank" class="btn btn-primary " >View Full Doc</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

Categories