PHP Boostrap Accordion - How to display dynamic data? - php

So I'm trying to get the bootstrap accordion to dynamically show the Top 5 teams judged by most wins, I also use it for other rankings.
The problem is whenever I click tab 2, 3, or 4 it only opens tab 1 in the accordion. I know the issue is from "href='#collapse41'" being the same from every loop. I assume this will require a unique ID for each accordion div tab but I don't know how to implement it correctly. I could be 100% wrong, if so please correct me!
I've done about 15 searches on this and only found one answer, which wasn't very detailed or helpful. Any help or pointers to learn how to do this would be much appreciated.
(The reason I renamed it to #collapse41 is because I have multiple accordions on the same page.)
<div class="accordion" id="accordion2">
<?php
include 'db.php'; //connect to database
$result = mysql_query("SELECT * FROM teams ORDER BY wins DESC LIMIT 5");
$rank = 1;
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {
echo "<div class='accordion-group'>";
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion4' href='#collapse41'>";
echo "<td>{$row['name']} <br /> </td>";
echo "</a>";
echo "</div>";
echo "<div id='collapse41' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<td>Rank: {$rank} <br /> </td>";
echo "<td>Wins: {$row['wins']} <br /> </td>";
echo "<td>Losses: {$row['losses']} <br /> </td>";
echo "</div>";
echo "</div>";
echo "</div>";
$rank++;
}
}
?>
</div>

You have to break the loop up....and loop once to get headings, and twice to get body...
So you need to echo the group div first....
echo "<div class='accordion-group'>";
Then start the loop to output headings...
Then run another loop to output divs....
if (mysql_num_rows($result)) {
//LOOP 1
while ($row = mysql_fetch_assoc($result)) {
//Build heading pieces
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion-".$row['id']." href='#accordian-".$row['id']."'>";
echo "<td>{$row['name']} <br /> </td>";
echo "</a>";
echo "</div>";}
//LOOP 2
//Build inner pieces
while ($row = mysql_fetch_assoc($result)) {
echo "<div id='accordian-".$row['id']."' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<td>Rank: {$rank} <br /> </td>";
echo "<td>Wins: {$row['wins']} <br /> </td>";
echo "<td>Losses: {$row['losses']} <br /> </td>";
echo "</div>";
echo "</div>"; }
echo "</div>";
$rank++;
}
}
Im not actually too sure of how your accordian is to be structured, so this may or may not be correct information, but whenever I do twiiter-bootstrap accordians, this is how I do it. If you could post your expected HTML output (template for accordian), that would help me greatly, and I can revamp my answer to suit what you actually need

Related

Tinymce echo Images next to each other instead below

I have a MYSQL database where I store images for reports I can import them to the Tinymce text area but they are being placed below each other. Is there away I can make them place two two next to each other. Some reports have 2 photos and other might have 10 depending on how many was loaded to the DB
This is my current code:
$result = mysqli_query($db, "SELECT * FROM images WHERE file_nr = '$id'");
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='300' height='300' display: inline-block;
src='../../cases_1/overdue/images/".$row['image']."' >";
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
I tried inline-block not working.
I don't think this is the cleanest method but got this working
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='350px' height='350px' style='float: left; padding: 4px;' src='../../cases_1/overdue/images/".$row['image']."' >";
echo "</div>";
}
?>

Count within nested loop and use value before second nested loop

So I have a nested loop due to getting data from two different sources.
I want to count how many times it goes through the second loop, and use that value in the first loop.
I have 2 entries in the RaidFacade
And 10 entries in "GetRaidProgression"
$raid_facade = new RaidFacade();
$raids = $raid_facade->getAll();
unset($raid_facade);
<div class='col-lg-4' id='toggleraid'>
<div class='topbar'>Raid Progress</div>
<?php
$boss_count_alive = 0;
$boss_count_killed = 0;
foreach ($raids as $raid)
{
$raid_name = $raid->getName();
echo "<div class='raid'>";
echo "<div class='name'>";
echo "<a class='collapsed' data-toggle='collapse' data-target='#raid".$raid_name."' aria-expanded='false' aria-controls='raid".$raid_name."'>";
echo $raid_name;
echo "</a>";
echo "</div>";
echo "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";
echo "<div class='bar'>";
echo "<div class='color' style='width: 60%'></div>";
echo "</div>";
echo "<div class='gradient'></div>";
echo "<img src='img/layout/raid/zul_gurub.jpg'>";
echo "</div>";
echo "<div id='raid".$raid_name."' class='collapse raidCollapse' data-parent='#toggleraid'>";
foreach ($raid->getRaidProgression() as $boss)
{
$boss_count_alive++;
$class = "fas fa-times fa-sm";
$youtube = "";
if ($boss->getStatus() == 1)
{
$class = "fas fa-check fa-sm";
$boss_count_killed++;
}
echo "<div>";
echo "<div><span><i class='".$class."'></i>".$boss->getBoss()."</span></div>";
echo "</div>";
}
echo "</div>";
}
?>
</div>
In the div class='prog' I would like to use the $boss_count_alive and $boss_count_killed values.
This is not happening, the first entry returns 0/0, the next one returns 3/10 (Which are my expected result for first entry)
To get a visual look:
Thanks in advance!
Your second loop will only run after you have echoed "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";, which is why it's echoing 0/0 the first time.
If you want it to echo 3/10 the first time, you need to run your inner loop first to calculate the values, then echo. In your case it would be something like this:
foreach ($raids as $raid)
{
$raid_name = $raid->getName();
$bosses_html = "";
foreach ($raid->getRaidProgression() as $boss)
{
$boss_count_alive++;
$class = "fas fa-times fa-sm";
$youtube = "";
if ($boss->getStatus() == 1)
{
$class = "fas fa-check fa-sm";
$boss_count_killed++;
}
$bosses_html .= "<div>";
$bosses_html .= "<div><span><i class='".$class."'></i>".$boss->getBoss()."</span></div>";
$bosses_html .= "</div>";
}
echo "<div class='raid'>";
echo "<div class='name'>";
echo "<a class='collapsed' data-toggle='collapse' data-target='#raid".$raid_name."' aria-expanded='false' aria-controls='raid".$raid_name."'>";
echo $raid_name;
echo "</a>";
echo "</div>";
echo "<div class='prog'>".$boss_count_killed."<mark>/</mark>".$boss_count_alive."</div>";
echo "<div class='bar'>";
echo "<div class='color' style='width: 60%'></div>";
echo "</div>";
echo "<div class='gradient'></div>";
echo "<img src='img/layout/raid/zul_gurub.jpg'>";
echo "</div>";
echo "<div id='raid".$raid_name."' class='collapse raidCollapse' data-parent='#toggleraid'>";
echo "</div>";
echo $bosses_html;
}
In this example, the first set of echoes are moved below the inner loop. The html that would normally be echoed in the inner loop is stored in a variable to be echoed later at the end.

Printing an associative array inside a columned table

<div class="form-group">
<td><label for="genre">Names</label></td>
<td>
<?php
foreach($array as $row){
echo "<div class='form-check form-check-inline'>";
echo "<label class='form-check-label'>";
echo "<input class='form-check-input' type='checkbox' name='genre_checked[]' value='".$row->name."'>";
echo $row->name;
echo "<label>";
echo "</div>";
}?>
</td>
</div>
I'm making a form to submit to my controller and inside that form, i am printing an array inside a "" but the way i've coded it is that it will print in a single column from top to bottom and makes the "td" height increase. I wanted to make use of the extra space so instead of increasing the height i atleast wanted to make use of the td width.
Nevermind. After a few hours of sleep. Took only 5mins.
<?php
foreach($genre as $row){
echo "<div class='col-lg-2'>";
echo "<input type='checkbox' name='genre_checked[]' value='".$row->genre_name."'>";
echo $row->genre_name;
echo "</div>";
}?>

Why divs are running towards left margin?

I am new to css and php, i want to retrieve information from db via php and display it to the user, all working fine but problem is with the UI part enter image description here
and my code is
<?
$result=mysql_query($query) or die (mysql_error());
echo "<div class='container'>";
while($row =mysql_fetch_array($result))
{
echo "<div class='row custom1'>";
echo "<div class='col-sm-12' id='dd' style='border-style:solid; border-width:1px; margin-left:10px; margin-top:20px'><a href='showit.php' class='kk'>";
echo '<p class="head1">QUESTION:',$row['title'],'</p><p clsss="head2" style="font-size:20px;"></br>Tags:',$row['tags'],'</br>Posted by:',$row['posted'],'</br>Posted On:',$row['postedon'],'</p>';
echo "</div>";
}
echo "</div>";
?>
is there any way to align the divs properly?
it's because you're missing the closing </div> for the second div and the closing </a> in your while loop making a bunch of children of each other, rather than inner nodes of the container parent, use this loop instead:
$result=mysql_query($query) or die (mysql_error());
echo "<div class='container'>";
while($row =mysql_fetch_array($result))
{
echo "<div class='row custom1'>";
echo "<div class='col-sm-12' id='dd' style='border-style:solid; border-width:1px; margin-left:10px; margin-top:20px'>";
echo "<a href='showit.php' class='kk'>";
echo "<p class=\"head1\">QUESTION:".$row['title']."</p>";
echo "<p class=\"head2\" style=\"font-size:20px;\">";
echo "</br>Tags:".$row['tags']."</br>Posted by:".$row['posted']."</br>Posted On:".$row['postedon'];
echo "</p>";
echo "</a>";
echo "</div>"; //col-sm-12
echo "</div>"; //row custom1
}
echo "</div>";
By careful you are not closing all tags. I saw that
<div class='col-sm-12' id='dd' style='border-style:solid; border-width:1px; margin-left:10px; margin-top:20px'><a href='showit.php' class='kk'>
is not closed.
Please go into your browser page and inspect the elements an verify how they are closed.

I upload videos into into directory and i want them to display horizontall when i echo them

I have these codes and am not good at php please can someone help me on how to echo the videos to appear horizontall in my page thanks in advance
<?php
$h=2;
$k=mysql_query("SELECT * from aupload where type='audios' and view>='$h' order by view DESC");
while ($la=mysql_fetch_array($k)){
?>
<center>
<table width='100%'height=''>
<tr>
<?php echo"<td>" . "<a href='uploads/$la[filename]'><img src='uploads/$la[size]' width='180px' height='180px'>
<br><b>$la[filename]</b><br><a href='music1.php?id=".$la['id']."'><input type='button' value='DOWNLOAD'><input type='button' value='$la[view]'> </a></a>
"?></td>
</tr>
</table><br>
</center>
<?php }?>
<?php
include('connect.php');
$k=mysql_query("SELECT * from aupload where type='audios'");
#$la=mysql_fetch_array($k)
?>
<?php
$h=2;
$y=mysql_query("SELECT * from aupload where type='videos' and view>='$h' order by view DESC ");
?>
<table border='0' id='myTable'width='100%'>
<?php
while ($x=mysql_fetch_array($y)){
echo "<tr>";
echo "<td>" . "<video id='myVideo' onclick='message()' width='180px' height='180px' controls><source src='uploads/$x[filename]' '></video>" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='' height=''>"."<a href='music2.php?id=$x[id]' > <b>$x[filename]</b><br><input type='button' value='DOWNLOAD'></a>" ."<input type='button' value='$x[view]'></a></a>". "</td>";
echo "</tr>";
}
echo "</table>"; echo"</center>";
?>
the problem is when I upload two or more videos they appear vertically but I just want them to appear horizontally when echo them please someone help and thanks
Use frames is the best way to display a video, try this code
This is actually a HTML question, not a PHP question. In HTML, designates a table row, and can contain many or table cell elements. So you want to have the a echoed before the while loop, then print just the elements in the while loop, then print the . This will put all the elements in a single table row.

Categories