below I have values and below that I have a while loop with multiple values that are sorted by $top_list_friends['workouts'], with the highest number at top.
How can a merge the below values into one list?
echo "<div class='user'>";
echo "<span class='number'>" . $my_rank . "</span>";
echo "<span class='name'>" . $personal['name'] . "</span>";
echo "<span class='workouts'>" . $personal['workouts'] . "</span>";
echo "</div>";
$i = 0;
while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {
$i++;
echo "<div class='user'>";
echo "<span class='number'>" . $i . "</span>";
echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
echo "</div>";
}
Didn't understand question very well, but I would do it in this way:
$name = $personal['name'];
$workouts = $personal['workouts'];
$html = <<<html
<div class='user'>
<span class='number'>$my_rank</span>
<span class='name'>$name</span>
<span class='workouts'>$workouts</span>
</div>
html;
$i=0;
while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {
$i++;
$topName = top_list_friends['name'];
$topW = $top_list_friends['workouts']
$html += <<<html
<div class='user'>
<span class='number'>$i</span>
<span class='name'>$topName</span>
<span class='workouts'>$topW</span>
</div>
html;
}
echo $html;
At the beginning:
$inserted=0;
into the loop:
if(!$inserted && $personal['workouts']> $top_list_friends['workouts'])}
$inserted=0;
//first div echoes
}
at the end
if(!$inserted){
//the same echos
}
Related
This question already has answers here:
New containing div after every 3 records
(3 answers)
Closed 5 years ago.
<?
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$selection = "SELECT steamUsername, steamid64, twitter, discord, github FROM hnTeam";
$result = $conn->query($selection);
?>
<div class="card-group">
<?
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
/*if($result->num_rows === 1 || $result->num_rows % 3 === 0) {
echo "<div class='card-group'>";
}*/
echo "<div class='card'>";
echo "<img class='card-img-top' src='...' alt='Profile picture'>";
echo "<div class='card-body'>";
echo "<p class='card-text'>";
echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
echo "</p>";
echo "</div>";
echo "</div>";
/*if($result->num_rows === 1 || $result->num_rows % 3 === 0) {
echo "</div>";
}*/
}
}
?>
</div>
</div>
There is my code so far. Essentially, I need to do the following:
For context, I will be referring to the following as a card:
echo "<div class='card'>";
echo "<img class='card-img-top' src='...' alt='Profile picture'>";
echo "<div class='card-body'>";
echo "<p class='card-text'>";
echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
echo "</p>";
echo "</div>";
echo "</div>";
Okay, so what I need to do is, every 3 cards need to be put into this div: <div class='card-group'>.
I tried some if statements as shown in the code block, however to no avail. From what I understand I need to echo it into a div once and after that echo it into a div every group of 3 cards. I tried to base the if statement on $result->num_rows but that isn't procedural, it constantly appears as the final amount of cards, it doesn't increase by 1 each run through.
I'm stumped here, any help is greatly appreciated!
You use the % operator with $result->num_rows, that is not correct, you must use the current iteration, so I added a $c var.
if($result->num_rows > 0) {
$c = 0;
while($row = $result->fetch_assoc()) {
if ($c % 3 === 0) {
if ($c > 0) {
echo "</div>"; //Close before one
}
echo "<div class='card-group'>";
}
echo "<div class='card'>";
echo "<img class='card-img-top' src='...' alt='Profile picture'>";
echo "<div class='card-body'>";
echo "<p class='card-text'>";
echo "<span id='social'><i class='fab fa-steam'></i> " . $row["steamUsername"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-discord'></i> " . $row["discord"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-twitter'></i> " . $row["twitter"] . "</span>" . "<br>";
echo "<span id='social'><i class='fab fa-github'></i> " . $row["github"] . "</span>" . "<br> <br>";
echo "</p>";
echo "</div>";
echo "</div>";
$c++;
if ($c == $result->num_rows) {
echo "</div>"; //Close last one
}
}
}
Working on getting a page to build off of an array that is returned from a DB to post a story, not sure what it is not working. The page URL looks like this: https://ohcrap.ninja/games/ps4/article.php?id=1
Here is the code that should be generating the content:
<?php
$id = $_GET['id'];
$query = mysqli_query($con,'SELECT * FROM `PS4` WHERE `id` =' .$id) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($query));
// Echo page content
echo "<div class='col s12 m12 l12'>";
echo "<div class='card small grey darken-3'>";
echo "<div class='card-stacked'>";
echo "<div class='card-content'>";
echo "$id";
echo "<span class='card-title'>" . $row['title'] . "</span>";
echo "<hr color='black'>";
echo "<P>By:<i> " . $row['author'] . "</i></P>";
echo "<P>Published: " . $row['published'] . "</P>";
echo "<br>";
echo "<P class='truncate'>" . $row['story'] . "</P>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
Your while loop is not doing anything useful, because you're immediately ending it with that ;.
while ($row = mysqli_fetch_array($query)) {
// all those echoes
}
I have a problem. I have a one page website and if there is posted a message a posts shows up and below the post there needs to be an image, but on the last on the index page I don't want an image to show up. So it's like if the id from the database is the highest that post doesn't get a image below it.
Here is the code:
<?php
include 'functions/image/functions.php';
$countQuery = $db->prepare("SELECT paginaNummer AS max FROM pages ;");
$countRow = $countQuery->fetch();
$maxId = $countRow['max'];
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
while($row = $query->fetch()) {
if(isset($row)) {
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if (count($row['paginaNummer']) == max($row)){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>
I won't get any further with this part I hope you can help me with this problem
It's hard to understand what you need. But try this query:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY paginaNummer DESC LIMIT 1");
Or may be this:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY id DESC LIMIT 1");
Update: this code changes instead of top:
<?php
include 'functions/image/functions.php';
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$maxrow = $query->rowCount();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
$n = 0;
while($row = $query->fetch()) {
if(isset($row)) {
$n++;
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if ($n == $maxrow){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>
Is there a way that I can put the data from the while loop into an array and output it using echo? I've tried defining an array and just appending it with .= but it does not add.
$query = "SELECT * FROM coils WHERE name like '%$term%' or
resistance like '%$term%' or
wraps like '%$term%' or
wire_one like '%$term%' or
wire_two like '%$term%' or
wire_three like '%$term%' or
wire_four like '%$term%' or
wire_five like '%$term%' or
wire_six like '%$term%'
LIMIT 25";
$prep = $db->getConnection()->prepare($query);
$result = $prep->execute();
$rowCount = $prep->rowCount();
if ($rowCount <= 0) {
echo "<script>alert('No Results, please try another search');</script>";
}
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
echo "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
echo "<div id='search_result'>";
echo "<div id='search_title'>Name: " . $row['name'] . "</div>";
echo "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
echo "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
echo "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
echo "<div id='search_description'>" . $row['description'] . "</div>";
echo "</div>";
echo "</a>";
}
Try to add output in an array and then use implode().
$temp = array();
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
$temp[] = "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
$temp[] = "<div id='search_result'>";
$temp[] = "<div id='search_title'>Name: " . $row['name'] . "</div>";
$temp[] = "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
$temp[] = "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
$temp[] = "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
$temp[] = "<div id='search_description'>" . $row['description'] . "</div>";
$temp[] = "</div>";
$temp[] = "</a>";
}
echo implode(' ', $temp); //with or without space
You can get all the data in an array with:
$data = $prep->fetchAll(PDO::FETCH_ASSOC);
To display it, you can close PHP with ?>, then use the alternate syntax for nice clean HTML:
<?php foreach ($data as $row) : ?>
<a href="coil.php?id=<?= htmlentities($row['uniqueid']) ?>">
<div id='search_result'>
<div id='search_title'>Name: <?= htmlentities($row['name']) ?></div>
<div id='search_ohms'>Resistance: <?= htmlentities($row['resistance']) ?></div>
<div id='search_wraps'>Wraps: <?= htmlentities($row['wraps']) ?></div>
<div id='search_around'>Wrapped Around: <?=htmlentities($row['wrapped']) ?></div>
<div id='search_description'><?= htmlentities($row['description']) ?></div>
</div>
</a>
<?php endforeach ?>
I have little experience with AJAX and jQuery so apologize if this seems trivial.
I have a list of ingredients that are generated from a DB via PHP foreach loop:
<div class="pure-u-1 pure-u-md-2-5 pure-u-lg-2-5 content-left">
<div id="scroll" class="card">
<h2 class="is-center">green</h2>
<ul id="greens" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($greens as $green) {
echo "<li>";
echo "<span class='item-name-small'>" . $green['name'] . "</span>";
echo "<span class='item-description-menu'>" . $green['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $green['id'] . "' name='" . $green['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}?>
</ul>
<h2 class="is-center">essentials</h2>
<ul id="essentials" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "veggies") || ($essential['subtype'] == "fruit")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
<h2 class="is-center">crunch</h2>
<ul id="crunch" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "crunch")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
<h2 class="is-center">grains</h2>
<ul id="grains" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($essentials as $essential) {
if (($essential['subtype'] == "grains")) {
echo "<li>";
echo "<span class='item-name-small'>" . $essential['name'] . " </span>";
echo "<span class='item-description-menu'> " . $essential['description'] . "</span>";
echo "<span class='content-right'>";
echo "<a class='minus increment' href='#'> - </a>";
echo "<input class='quantity' type='text' size='1' id='" . $essential['id'] . "' name='" . $essential['id'] . "' value='0'>";
echo "<a class='plus increment' href='#'> + </a>";
echo "</span>";
echo "</li>";
echo "</br>";
}
}?>
</ul>
</div> <!-- END CARD -->
</div> <!-- END ESSENTIALS -->
Currently, I have it that next to each ingredient, a - and + appear next to a text input. What I'm trying to do is change the code that when a customer clicks on each individual item, it is added to a different div that has a summary of their order. The second div is on the same page as the item list.
If you just want to copy the list item to a different div you can do it like this:
$(function (){
$('ul.card-content-ingredients li').click(function(){
$('#new-div-id ul').append($(this));
}
});