Looping PHP Numbers To Make A Slideshow - php

I have this page that looks a little like this.
<div class="ecigcontainer">
<div class="backbutt"><?php include("php/back.php"); ?></div>
<div class="ecig"><?php include("php/ecig.php"); ?></div>
<div class="forwardbutt"><?php include("php/forward.php"); ?></div>
</div>
A back button that works like this.
<?php
if (isset($_GET["ecig"])) {
$back = $_GET["ecig"] - 1;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";
} else {
$back = $_GET["ecig"] - 1;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";
}?>
The forward button pretty much has the same code, and the display looks like this.
<div class="ecigpic">
<img src="images/ecigs/<?php include("php/ecigcounter.php"); ?>.png" height="400px" />
</div>
<div class="ecigdesc">
<?php
if (isset($_GET["ecig"])) {
include("ecigs/" . $_GET["ecig"] . ".php");
echo "<h3>" . $ecigname . "</h3>";
echo "<p>" . $ecigdesc . "</p>";
} else {
$_GET["ecig"] = 1;
include("ecigs/" . $_GET["ecig"] . ".php");
echo "<h3>" . $ecigname . "</h3>";
echo "<p>" . $ecigdesc . "</p>";
}?>
</div>
With the "ecigcounter" that does this.
<?php
if (isset($_GET["ecig"])) {
echo $_GET["ecig"];
} else {
$_GET["ecig"] = 1;
echo $_GET["ecig"];
}?>
But the whole reason for this question is, how to I make it so it only displays numbers 1-40? Kind of lost and can't really seem to figure it out, and put it where it needs to go.

All the it needed was
<?php
if (isset($_GET["ecig"])) {
if ($_GET["ecig"] == 1){
$back = 40;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";
} else {
$back = $_GET["ecig"] - 1;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";
}
} else {
$back = $_GET["ecig"] - 1;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";
}?>
This little script made the world.
if ($_GET["ecig"] == 1){
$back = 40;
echo "<a href=\"?ecig=" . $back . "\">";
echo "<img src=\"images/ecigs/" . $back . ".png\" height=\"300px\" /></a>";

Related

Echoing a div every 3 rows of a MySQL table [duplicate]

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
}
}
}

Run UPDATE on single row within PHP loop

I have written this code, sorry I'm pretty new to PHP.
I have a loop which grabs all the horses from the database.
I have created a sell button underneath each entry, the problem I have is that when I hit "Sell" on a single entry it "Sell"s ALL of the entries.
Here is my code;
$query = "SELECT * FROM horses WHERE (owner = '$id') AND (status = 'active')";
if($result = $connect->query($query)) {
echo "<ul id='horses' class='row'>";
while ($row = $result->fetch_assoc()) {
echo "<li class='col-sm-4'>";
echo "<div class='horse-wrap'>";
echo "<h2>" . $row['name'] . "</h2>";
echo "<div class='details'>";
echo "<strong>Age:</strong> " . $row['age'] . "<br>";
echo "<strong>Colour:</strong> " . $row['colour'] . "<br>";
echo "<strong>Country:</strong> " . $row['country'] . "<br>";
echo "<strong>Value:</strong> £" . number_format($row['value']) . "<br>";
echo "</div>";
echo "<h3>Record</h3>";
echo "<ul class='history row'>";
echo "<li class='col-sm-3'>Runs: " . $row['runs'] . "</li>";
echo "<li class='col-sm-3'>Wins: " . $row['first'] . "</li>";
echo "<li class='col-sm-3'>2nd: " . $row['second'] . "</li>";
echo "<li class='col-sm-3'>3rd: " . $row['third'] . "</li>";
echo "</ul>";
echo "<ul class='skills'>";
echo "<h4>Speed</h4><li><span class='skill' style='width: " . $row['skill_speed'] . "%;'>" . $row['skill_speed'] . "</span></li>";
echo "<h4>Jumping</h4><li><span class='skill' style='width: " . $row['skill_jump'] . "%;'>" . $row['skill_jump'] . "</span></li>";
echo "<h4>Temperament</h4><li><span class='skill' style='width: " . $row['skill_temperment'] . "%;'>" . $row['skill_temperment'] . "</span></li>";
echo "<h4>Endurance</h4><li><span class='skill' style='width: " . $row['skill_endurance'] . "%;'>" . $row['skill_endurance'] . "</span></li>";
echo "</ul>";
// SELL HORSE BUTTON
echo "
<form action='./stables.php' method='post'>
<input type='hidden' value='" . $row['value'] . "' name='sellvalue' />
<input type='hidden' value='" . $row['name'] . "' name='sellname' />
<input type='submit' value='Sell Horse' name='sell' class='sell' />
</form>
";
echo "</div>";
echo "</li>";
// SELL HORSE
if(!empty($_POST["sell"])) {
//REMOVE HORSE FROM USER
$horseName = $row['name'];
$status = 'sold';
if($updateHorse = $connect->prepare("UPDATE horses SET status = ? WHERE name = ?")) {
$updateHorse->bind_param('ss', $status, $horseName);
$updateHorse->execute();
$updateHorse->close();
}
//ADD MONEY TO USER
$updateAmount = $money + $row['value'];
if($updateUser = $connect->prepare("UPDATE users SET money = ? WHERE id = ?")) {
$updateUser->bind_param('ss', $updateAmount, $id);
$updateUser->execute();
$updateUser->close();
}
}
}
echo "</ul>";
} else {
echo "You currently have no horses.";
}
You're doing the update inside of a loop, but you're never checking to see if the loop's current horse is the one the customer is trying to sell. As it's written, it is going to execute for every one.
You're checking to see if the form was submitted:
// SELL HORSE
if(!empty($_POST["sell"])) {
This is the perfect place to also make sure you have the right horse:
// SELL HORSE
if(!empty($_POST["sell"]) && $row["name"] === $_POST["sellname"]) {

where to put class or style name

I want to add three class for bottom of pagination where show page number , I used that in PHP but I can't figure out where to put class name inside of php echo to make work... because i'm getting errors...
class="box_one" for ... << Prev Page
class="box_two" for ... echo $thenumrows or $i
class= "box_three" ... Next Page >>
here is code...
<?php
if ($thenumrows != "")
{
echo "<br>";
if ($thecurrentpage > 1)
echo "<a href='". $_SERVER['PHP_SELF'] . "?cat= ". $theselectedcat ."&rows= "
. $thenumrows ."&page=". $thepreviouspage ."'>
<< Prev Page </a> "; for ($i=1;$i<=$totalpages;$i++)
{
if ($i == $thecurrentpage)
echo $i ." ";
else
echo "<a href='". $_SERVER['PHP_SELF'] ."?cat=". $theselectedcat ."&rows=".
$thenumrows ."&page=". $i ."'>$i</a> ";
}
if ($thecurrentpage < $totalpages)
echo "<a href='". $_SERVER['PHP_SELF'] ."?cat=". $theselectedcat ."&rows=".
$thenumrows ."&page=". $thenextpage ."'>Next Page >></a> ";
}
?>
please help thanks.
AM
I had to clean up your code. The classes are added to the a-tags:
if ($thenumrows != "") {
echo "<br />";
// Back
if ($thecurrentpage > 1) {
echo '<a class="box_one" href="'. $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $thepreviouspage . '"><< Prev Page</a> ';
}
// Paginating
for ($i=1; $i<=$totalpages; $i++) {
if ($i == $thecurrentpage) {
echo '<span class="box_two">' . $i .'</span> ';
}
else {
echo '<a class="box_two" href="' . $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $i . '">' . $i . '</a> ';
}
}
// Next
if ($thecurrentpage < $totalpages) {
echo '<a class="box_three" href="' . $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $thenextpage .'">Next Page >></a> ';
}
}
?>

php if statement not sure how to do 2 if's

I'm trying to do a condition within an if statement.
I have a current if statement which determines odd and even for css styling.
Within this I want to add another if statement for the reference number. (outlined as Ref in the code).
I'm not sure how to go about this but I want to add something along the lines of
if ref == 1 then echo ref, I've tried putting an if within an if and I just keep getting syntax errors. Can someone point me in the right direction please?
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
the bit I tried to do with 2nd if statement:
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates1 ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
$sch = $row1['schuedled'];
else if ($sch == '1') {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " .$row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
else {
echo "<blockquote class='example-right'>" . $row1['update'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='assyst-ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
Try something like this:
while($row1 = mysql_fetch_array($result1)){
$html = '';
if (++$x % 2 ){
$html .= "<blockquote class='example-right'>";
} else {
$html .= "<blockquote class='example-obtuse'>";
}
$html .= $row1['update'];
if($row1['schuedled'] == '1'){
$html .= "<div class='ref'>Ref: " .$row1['ref'] . "</div>";
}
$html .= "more html";
$html .= "more html";
$html .= "more html";
$html .= "</blockquote>";
echo $html;
}

php pass parameter issue

This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>

Categories