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.
Related
I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post
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 am attempting to create a PHP script that will build a table of images from a database. I'm having a hard time figuring out how to properly nest my while loops to make a 6x(how ever many are in the db) table. I understand the concept but I am new to PHP and just can't wrap my head around doing it here.
<?php
mysql_connect("localhost","root","");
mysql_select_db("images");
$res=mysql_query("SELECT * FROM table1");
echo "<table>";
while($row=mysql_fetch_array($res)){
echo "<tr>";
echo "<td>";?>
<img src="<?php echo $row["images1"]; ?>" height="150" width="150">
<?php echo "</td>";
echo "</tr>";}
echo "</table>";
?>
if you keep a count of how many images you have processed you can use if($count % 6 == 0) to test if you are at the 6th item in the list. So your loop would look like the following:
$c = 0; //our count
while($row = mysql_fetch_array($res)){
if(($count % 6) == 0){ // will return true when count is divisible by 6
echo "<tr>";
}
echo "<td>";
?>
<img src="<?php echo $row["images1"]; ?>" height="150" width="150">
<?php
echo "</td>";
$c++; // Note we are incrementing the count before the check to close the row
if(($count % 6) == 0){
echo "</tr>";
}
}
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
I have a list of names that are fetched from a while loop coded in PHP.
When I try to display the content of the loop which is embedded in a div, the content gets overlapped.
The style I am using for the content is:
<style>
#cap{
position: absolute;
top: 0px;
right: 0px;
}
</style>
Here is the entire code of the loop content along with style of the div in PHP:
<?php
$result2 = mysql_query("SELECT `player_name` FROM `player_data` WHERE `team_id`=$team2");
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "<div id='cap'>";
echo "" . $name2 . "<br/>";
echo "</div>";
}
?>
How do I get this content $name2 not overlapped?
Something like this:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo '<div class="player">';
echo $name2;
echo '</div>';
}
echo '</div>';
Put the div with that CSS outside the loop:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "" . $name2 . "<br/>";
}
echo "</div>";