PHP Calculation using If logic function not working in Value field - php

This is current formula for calculating a value
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box"
value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div>
Now when we change it to a very basic calculation purpose with if syntax based on price range, it does not work in
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.($a = $this->prodDet->v_price);
$b=.02889;
$z=.02307;
if ($a > 500000){
$c=$a*$z;
} else {
$c=$a*$b;
}
echo(round($c) . "<br>");.'" readonly=""/>
</div>
Could someone help in - what is wrong.
This may be basic for you - but am learning php - Appreciate your help !!
Edit
Below is the complete code
<?php
define( '_JEXEC', 1);
$database = &JFactory::getDBO();
<2 different tables been called>
after that below:-
if(isset($_GET["ncvd"])){
$NewToyVariantDetail=$_GET["ncvd"];
$sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='".$NewToyVariantDetail."'";
$database->setQuery($sql);
$rows = $database->loadObject();
$list=' <div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box"
value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div>
die($list);
}
?>
may advise on !!

Your code has problem at :
echo(round($c) . "<br>");
Here is the working one:
<?php
$v_price = 500;
echo '
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.($a = $v_price);
$b=.02889;
$z=.02307;
if ($a > 500000){
$c=$a*$z;
} else {
$c=$a*$b;
}
echo round($c).'" readonly=""/>
</div>';
?>
My suggestion would be to write PHP code (as you did calculation) separately , not inside html input code.
For example:
<?php
$v_price = 500;
$a = $v_price;
$b=.02889;
$z=.02307;
if ($a > 500000){
$c=$a*$z;
} else {
$c=$a*$b;
}
$val = round($c);
?>
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="<?php echo $val;?>" readonly=""/>
</div>
Issues in your updated complete code:
- <div class="common-box"> is not closed
- <div class="common-box-right"> is missing '; at the end
<?php
define( '_JEXEC', 1);
$database = &JFactory::getDBO();
if(isset($_GET["ncvd"])){
$NewToyVariantDetail=$_GET["ncvd"];
$sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='".$NewToyVariantDetail."'";
$database->setQuery($sql);
$rows = $database->loadObject();
$list=' <div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box"
value="'.(($rows->v_price*3.41)/100) .'" readonly=""/></div></div>';
echo $list;
}
?>

Try this
value = "<?php echo ($rows->v_price*3.41/100) ?>"

Inline PHP works better for this ... Although, you might want to make a method a part of the class, that will calculate this for you, so you don't have so much logic put into such a small space.
<div class="common-box">
<div class="common-box-left">Approx Value</div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="<?=round(($this->prodDet->v_price > 500000) ? $this->prodDet->v_price * 0.02307 : $this->prodDet->v_price * 0.02889));?>" readonly=""/></div>
This is the direct fix for your problem, but really there are many many issues that are being compounded into this. It's style that we need to talk about.
<?php
define('_JEXEC', 1);
$database = &JFactory::getDBO();
if(isset($_GET["ncvd"]))
{
$NewToyVariantDetail=$_GET["ncvd"];
$sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='" . $NewToyVariantDetail . "'";
$database->setQuery($sql);
$rows = $database->loadObject();
$list ='
<div class="common-box">
<div class="common-box-left">Approx Value </div>
<div class="common-box-right"><input name="idv" type="text" class="text-box" value="'.(($rows->v_price*3.41)/100) .'"readonly=""/></div>';
die($list);
}
?>
<?php
define('_JEXEC', 1);
$database = &JFactory::getDBO();
if(isset($_GET['ncvd']))
{
$database->setQuery("SELECT * from toy_newtoy_variants where v_status='1' and v_id='{$_GET['ncvd']}'");
$rows = $database->loadObject();
echo '<div class="common-box">';
echo ' <div class="common-box-left">Approx Value</div>';
echo ' <div class="common-box-right"><input name="idv" type="text" class="text-box" value="' . ($rows->v_price * 3.41) / 100 . '"readonly=""/></div>';
echo '</div>';
}
?>
This is better, however, I can't even begin to tell you how bad it is to not filter your inputs. $_GET['ncvd'] is a ticking timebomb. We need to fix that ...
I'm going to take the guess that v_id in the database is an int. As such, we use the built in filter functions to clean the input.
<?php
define('_JEXEC', 1);
$database = &JFactory::getDBO();
if(isset($_GET['ncvd']))
{
$nvcd = filter_input(INPUT_GET, 'ncvd', FILTER_SANITIZE_NUMBER_INT);
$sql = "SELECT * from toy_newtoy_variants where v_status='1' and v_id='{$ncvd}'";
$database->setQuery($sql);
$rows = $database->loadObject();
echo '<div class="common-box">';
echo ' <div class="common-box-left">Approx Value</div>';
echo ' <div class="common-box-right"><input name="idv" type="text" class="text-box" value="' . ($rows->v_price * 3.41) / 100 . '"readonly=""/></div>';
echo '</div>';
}
?>
This is the version that you should use. I don't understand why you are using die(), so you might want end the code execution there. For that, you can simply call exit if you must.

Related

PHP Search now not returning anything but no errors [duplicate]

This question already has answers here:
PHP SQL STMT SELECT multiple LIKE ? is it possible?
(2 answers)
Closed 2 years ago.
I'm building an online shop (not a real one) using PHP. I have a search results page and a full specs page, the full specs page is working perfectly however when I created a shopping cart page that also works perfectly the search results page no longer returns anything. I have found when commenting out the if statement and while statement the "no items found" message does work but cannot get it to return anything if this is a valid search. After a couple of days, I still cannot figure out what the problem is. I will include the adding to basket and form code as the form is also included on the full specs page which does work. If you can point me in the right direction it would be greatly appreciated.
**Search Bar code**
<?php
global $ConnectingDB;
if (isset($_GET["SearchButton"])) {
}
?>
<form class="mt-5" method="GET" action="searchResults.php" style="width: 100%;">
<div class="form-group">
<input class="form-control mb-2" type="text" name="Search" placeholder="Search for Item" value="">
<button class="btn btn-success" name="SearchButton" style="width: 100%;">Search</button>
</div>
</form>
Search Results Code
$db = mysqli_connect("localhost", "root", "", "computerStore") or die (mysqli_error());
require_once("includes/functions.php");
require_once("includes/sessions.php");
include("addingToBasket.php");
?>
<html>
<head>
<title>Search Results</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
include("navbar.php");
?>
<div style="height: 15%;"></div>
<div class="container mt-5">
<?php
echo ErrorMessage();
echo SuccessMessage();
?>
<div class="row">
<div class="col-lg-12">
<?php
if (isset($_GET["productNumber"])) {
$Search = $db->real_escape_string($_GET["Search"]);
$sql = $db->query("SELECT * FROM products WHERE productName LIKE '%$Search%' OR productNumber LIKE '%$Search%' OR briefProductInfo LIKE '%$Search%'");
$result = new mysqli($db, $sql) or die("bad query: $sql");
$row = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
echo $count;
//stmt = $db->query($sql);
//$row = $stmt->fetchAll();
//echo count($row).' rows selected';
if ($sql->mysqli_num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$ProductName = $row["productName"];
$ProductNumber = $row["productNumber"];
$ProductType = $row["productType"];
$BriefProductInfo = $row["briefProductInfo"];
$FullProductInfo = $row["fullProductInfo"];
$Image = $row["image"];
$Price = $row["price"];
$Quantity = $row["quantity"];
include("form.php");
}
} else {
?> <div class="container"><h2><?php echo "Item has not been found, please try another search"; ?></h2></div>
<?php
}
}
?>
</div>
</div>
</div>
</body>
</html>
Adding to Basket Code
<?php
if (!(isset($_SESSION['cart']))) {
$_SESSION['cart'] = array();
} else {
echo "";
}
if (isset($_GET['clear'])) {
$_SESSION['cart'] = array();
}
if (isset($_GET['productNumber']) && isset($_GET['quantity'])) {
$ProductNumber = $_GET['productNumber'];
$Quantity = $_GET['quantity'];
if ($Quantity > 0) {
if(isset($_SESSION['cart'][$ProductNumber])) {
$_SESSION['cart'][$ProductNumber] += $Quantity;
} else {
$_SESSION['cart'][$ProductNumber] = $Quantity;
$_SESSION["SuccessMessage"] = "Item has been successfully added to your basket, feel free to continue shopping if there are more items to wish to purchase. However if you
do wish to buy now, amend the item quantity or you added the item by mistake and wish to remove it, please select the view basket link.";
}
}
}
?>
The Form
while ($row = mysqli_fetch_assoc($result)) {
?>
<h2 class="mb-5"><?php echo $row["productName"] ?> - <?php echo $row["briefProductInfo"] ?></h2>
<p class="text-center mb-5"><?php echo $row["productNumber"] ?></p>
<img class="img-fluid mx-auto d-block mt-5 mb-5" src="images/<?php echo $row["image"] ?>" alt="Product Image">
<p style="text-align: justify;"><?php echo $row["fullProductInfo"] ?></p>
<h1 class="mt-5 mb-5" style="color: red;">£<?php echo $row["price"] ?></h1>
<p style="text-align: center;" id="numberAvailable"><?php echo $row["quantity"] ?> Available</p>
<form method="get" action="<?php $_SERVER["PHP_SELF"] ?>">
<div class="row mt-5 mb-5">
<div class="col-lg-2">
<p style="font-size: 170%;" id="numberRequested">Quantity</p>
</div>
<div class="col-lg-2 mt-1">
<select class="pt-2 pb-2" name="quantity" class="form-control" id="numberRequestedPulldown">
<?php
if ($row["quantity"] >= 5) {
for ($i = 1; $i <= 5; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
} else {
for ($i = 1; $i <= $row["quantity"]; $i++) {
?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
}
?>
</select>
<input type='hidden' name='productNumber' id='productNumber' value='<?php echo $row['productNumber'] ?>'>
</div>
<div class="col-lg-4 mt-1">
<input type="submit" class="btn btn-success btn-block" id="addToBasketLink" value="Add to Basket">
</div>
<div class="col-lg-4 mt-1">
Return to Items List
</div>
</div>
</form>
<?php
}
?>
Your SQL statement for the search may not be formatted correctly:
$sql = $db->query("SELECT * FROM products WHERE productName LIKE '%$Search%' OR productNumber LIKE '%$Search%' OR briefProductInfo LIKE '%$Search%'");
I can tell you from experience that just placing a double apostrophe(") at the start and including the wildcard(%) in your statement may be the wrong way to go try this:
$sql = $db->query('SELECT * FROM products WHERE productName LIKE "%'.$Search.'%" OR productNumber LIKE "%'.$Search.'%" OR briefProductInfo LIKE "%'.$Search.'%"');
This separates the wildcard(%) from the search term that the SQL is looking for. By including the % inside of the statement the SQL statement may run but come up with zero results and move onto your next statement because it is looking for '%book%', when it should be just looking for 'book'. This would give you the "no items found" message, because it moves to the next statement.
This is new information 10/30/2020
You are using the search as your name in this input:
<form class="mt-5" method="GET" action="searchResults.php" style="width: 100%;">
<div class="form-group">
<input class="form-control mb-2" type="text" name="Search" placeholder="Search for Item" value="">
<button class="btn btn-success" name="SearchButton" style="width: 100%;">Search</button>
</div>
</form>
You are not carrying over the productNumber though that I see to get start the statement.
if (isset($_GET["productNumber"])) {
Maybe try doing something like this instead:
$Search = filter_input(INPUT_GET, 'Search');
/*use the preg_replace to remove any special characters*/
$Search = preg_replace('/[^a-zA-Z0-9]/', '', $Search);
echo $Search; /*Just to see if it appears*/
/*instead of if (isset($_GET["productNumber"])) { use */
if($Search != ''){
$sql = $db->query('SELECT * FROM products WHERE productName LIKE "%'.$Search.'%" OR productNumber LIKE "%'.$Search.'%" OR briefProductInfo LIKE "%'.$Search.'%"');
continue with code

How to validate checkbox in a for loop

I am creating a seat reservation system. In my system, the code check number of seats a bus contains then pass it inside a for loop. When a user pick 2 passengers, it means two seats will be booked. How can I validate the checkbox in the for loop depending on the number of passenger(s) selected.
Using the GUI for more explanation.
on the main page, 2 there indicates number of passenger(s) selected.
When you come to the second page where the values are passed to, you can see 2 Adults as the selected number of passengers. When you click on Submit Button it does not validate the checkbox based on the number of passenger(s) selected. And if I should put required in the checkbox it validates the whole checkbox since it is in a loop
$_SESSION['seat_no'] is the number of seat(s) a bus contains. Let assume a user that want to book a seat selected two passengers which means two seats is booked, how can I validate the checkbox based on the number of seat(s) selected?
Here is my code:
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>";
}else{
echo "<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>";
}
}
?>
The full source code:
<?php include("header.php"); error_reporting(0); ?>
<?php
if(isset($_POST['submit'])){
$from = $_POST['from'];
$to = $_POST['to'];
$date = $_POST['d_date'];
$nop = $_POST['nop'];
$_SESSION['from'] = $from;
$_SESSION['to'] = $to;
$_SESSION['date'] = $date;
$_SESSION['nop'] = $nop;
$get = mysqli_query($mysqli,"SELECT * FROM routes WHERE present_loc = '$from' and destination = '$to' ");
while($roys = mysqli_fetch_array($get)){
//get bus details
$bno = $roys['bus_no'];
$ploc = $roys['present_loc'];
$des = $roys['destination'];
$time = $roys['dept_time'];
$_SESSION['time'] = $time;
$amt = $roys['amount'];
$_SESSION['amt'] = $amt;
$b = str_replace( ',', '',$_SESSION['amt'] );
if( is_numeric($b) ) {
$a = $b;
}
$bus = mysqli_query($mysqli,"select * from bus where bus_no = '$bno'");
while($bu = mysqli_fetch_array($bus)){
$_SESSION['model'] = $bu['model'];
$_SESSION['seat_no'] = $bu['seat_no'];
$_SESSION['ac'] = $bu['bus_type'];
$_SESSION['excess_luggage'] = $bu['excess_luggage'];
$_SESSION['more_legs'] = $bu['more_legs'];
$_SESSION['id'] = $bu['id'];
}
$coun = mysqli_query($mysqli, "select count(booking_id) as seat, seats from booking where bus_no = '$bno' and seats !='' GROUP by booking_id" );
$mseat = array();
while($e = mysqli_fetch_array($coun)){
$bseat = $e['seat'];
$mseat[] = $e['seats'];
}
//$seatss = array();
$seat_string = implode(",",$mseat);
//get seats
$couns = mysqli_query($mysqli, "select sum(counter) as seat from booking where bus_no = '$bno' and seats !='' GROUP by bus_no" );
$rseats = mysqli_fetch_array($couns);
$lseat = $rseats['seat'];
if($_SESSION['seat_no'] == $lseat){
$tell = " No more seat(s) available.";
}else{
$tell = $_SESSION['seat_no'] - $lseat. " Seat(s) remaining.";
}
}
}
?>
<!--Main layout-->
<main class="mt-5">
<!--Main container-->
<form action="details" method="POST">
<!--Grid row-->
<div class="row">
<div class="col-lg-12 title-header mb-3 mx-auto z-depth-1">
<div class="row">
<div class="col-lg-8">
<?php echo '<h2> '.$_SESSION['from']. ' to '. $_SESSION['to']. '</h2>'; ?><br/>
<b><?php echo $_SESSION['date']; ?> :: <?php if($_SESSION['nop'] < '2') { echo $_SESSION['nop'] . ' Adult'; }
elseif($_SESSION['nop'] > 1) { echo $_SESSION['nop'] . ' Adults'; }
?></b>
</div>
</div>
</div>
<div class="col-lg-12 mbody"> <label style="margin-left: 4%; font-weight:bolder; font-size:20px; color:#000;">Details </label> </div>
<div class="col-lg-12 mbody bg-white ">
<table class="table table_view" style = "width: 100%; margin-left: 4%; margin-right:4%;">
<tbody>
<tr>
<td><b><?php echo $_SESSION['model']; ?></b><br/><?php echo $_SESSION['from']. ' to '. $_SESSION['to']; ?>
<br/><?php if($_SESSION['ac'] == 'AC') { echo '<span class="alert-info ac">'. $_SESSION['ac'] .'</span>'; }
else{ echo '<span class="alert-warning">No AC</pan>'; } ?>
<?php if($_SESSION['more_legs'] == 'Yes') { echo '<span class="alert-info ac">More Leg Room</span>'; }
else{ echo '<span class="alert-warning no">More Leg Not Available</pan>'; } ?>
</td>
<td><b>Departing Time</b><br/><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo $_SESSION['time']; ?></td>
<td> <img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> <?php echo $tell; ?></td>
<td>Adult <b>₦<?php echo $_SESSION['amt']; ?></b></td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-12">
<div class="col-lg-12 mbody"> <label style="margin-left: 3%; font-weight:bolder; font-size:20px; color:#000;"><img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> Select Seat</label> </div>
<div class="row detail">
<!--Grid column-->
<div class="col-lg-7 animation slideUp" >
<div class="well" id="bus_seats_layout" >
<table class="table table-bordered" cellspacing = "1" id="seatstable">
<tr>
<td><img id = "driverimg" src="../images/seatsLayout/steering.png" class="img-responsive" width="25" height="25"></td>
<td colspan="2" rowspan="3">
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "
<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>
";
}else{
echo "
<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>
";
}
}
?>
</td>
</tr>
</table>
</div>
</div>
<div class="col-lg-5">
<ul class="bt">
<li><img src="../images/seatsLayout/seat_available.png" class="img-responsive"> Available</li>
<li><img src="../images/seatsLayout/picked.png" class="img-responsive"> Selected</li>
<li><img src="../images/seatsLayout/seat_booked.png" class="img-responsive"> Booked</li>
</ul>
</div>
</div>
<div class="col-lg-12">
<input type="hidden" name="bus_no" value="<?php echo $bno; ?>">
<input type="hidden" name="to" value="<?php echo $to; ?>">
<input type="hidden" name="from" value="<?php echo $from; ?>">
<input type="hidden" name="amt" value="<?php echo $nop*$a; ?>">
<input type="hidden" name="nop" value="<?php echo $nop; ?>">
<div class="form-group">
<div align="right">
<input type="submit" name="submit" class="bme" value="Continue">
</div>
</div>
</div>
</div>
</div>
</form>
</main>
<?php include("footer.php"); ?>

After 500 results displayed from DB html/css loses formatting

I have a page that runs off a local webserver that is uses SQLite as its database. As its used local I am not worried about listing all results on one page as they load super fast. I am having an issue with it though as after 500 results are displayed from SQLite3 the formatting goes all wonky and starts stacking them on top of each other. Everything before that is fine. Its written in php. Info was entered into the database using htmlspecialchars so I dont believe that is the issue. The code that builds each record in the loop is
$list = '';
while($row = $results->fetchArray()) {
$id = $row["id"];
$MovieTitle = $row["MovieTitle"];
$MovieYear = $row["MovieDate"];
$MovieRes = $row["MovieRes"];
$FileName = $row["FileName"];
$Summary = $row["Summary"];
$Genres = $row["Genres"];
$PictureLocation = $row["PictureLocation"];
$Rating = $row["Rating"];
$ReleaseDate = $row["ReleaseDate"];
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}
and i just echo them them in the html as such
<html>
<body>
<div id="main">
<br>
<?php echo $list; ?>
</div>
</body>
</html>
Your HTML is wrong, you did not close <div class="box"> and <span class="play"> tags properly.
Correct HTML is:
<div class="box">
<div class="movie">
<div class="movie-image">
<span class="play">
<a href="movielist.php?movie='.$FileName.'">
<span class="name">'.$MovieTitle.'</span>
<img src="'.$ThumbnailPic.'" alt="">
</a>
</span>
</div>
<div class="rating">
<p>
RATING: '.$Rating.'
</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span>
</div>
</div>
</div>
Aso, you can have some tags or quotes in your database records. So you have to use escaping your variables before output http://php.net/manual/en/function.htmlspecialchars.php
Something like this:
$list = '';
while($row = $results->fetchArray()) {
$id = htmlspecialchars($row["id"]);
$MovieTitle = htmlspecialchars($row["MovieTitle"]);
$MovieYear = htmlspecialchars($row["MovieDate"]);
$MovieRes = htmlspecialchars($row["MovieRes"]);
$FileName = htmlspecialchars($row["FileName"]);
$Summary = htmlspecialchars($row["Summary"]);
$Genres = htmlspecialchars($row["Genres"]);
$PictureLocation = htmlspecialchars($row["PictureLocation"]);
$Rating = htmlspecialchars($row["Rating"]);
$ReleaseDate = htmlspecialchars($row["ReleaseDate"]);
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}

Delay on div after new picture

ok i have a script which change the image everytime you rate it, and it is working great, but the problem is that people can spam the rating system so i thought it could be possible to do some sort of delay before the div which contains the rating shows up after the new image was set
code to the ratingbar which should be delayed:
<div id="button" onclick="changeSrc2()">
<div class="rate_widget" id="<? echo $id;?>">
<div class="star_1 ratings_stars"></div>
<div class="star_2 ratings_stars"></div>
<div class="star_3 ratings_stars"></div>
<div class="star_4 ratings_stars"></div>
<div class="star_5 ratings_stars"></div>
<div class="total_votes">vote data</div>
</div>
</div>
code to change the image onclick on the rating bar:
$number="1";
$wrongnumber="2";
$random = mysql_query("SELECT * FROM images ORDER BY RAND()");
$place="upload/";
echo '<script type="text/javascript"> ';
while($wor = mysql_fetch_array($random))
{
$ids=$wor['id'];
$name = $wor['name'];
$images = $place . $wor['name'];
$number=$number + 1;
$wrongnumber=$wrongnumber + 1;
echo 'function ' . 'changeSrc' . $number . '() '; ?>
{
document.getElementById("rand").src="<? echo $images;?>";
document.getElementById("button").onclick=changeSrc<? echo $wrongnumber;?>;
document.getElementsByClassName('rate_widget')[0].id = <? echo $ids;?>;
}
<?
}
?>
</script>
and code to display the first images:
/*Display images*/
$r = mysql_query("SELECT * FROM images ORDER BY RAND() LIMIT 1");
while($wor = mysql_fetch_array($r))
{
$place="upload/";
$id=$wor['id'];
$name = $wor['name'];
$image = $place . $wor['name'];
echo '<img id="rand" src="'.$image.'" style="max-height:330px;">';
}
Try to use sleep() functions. It would look like this:
<?php
sleep(2);
echo '
<div id="button" onclick="changeSrc2()">
<div class="rate_widget" id=' . $id . '">
<div class="star_1 ratings_stars"></div>
<div class="star_2 ratings_stars"></div>
<div class="star_3 ratings_stars"></div>
<div class="star_4 ratings_stars"></div>
<div class="star_5 ratings_stars"></div>
<div class="total_votes">vote data</div>
</div>
</div>
';
?>

How do I group a div in some class on MySQL while

I want add a different class on my MySQL result.
<?php while ($fetch = $db->fetch($query)) { ?>
<div class="result"><?php echo $fetch['title']; ?></div>
<?php } ?>
Output should be like this
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
Here, every 6 results will have <div class="group"></div>.
Let me know
<div class="group">
<?php
$count = 0;
while ($fetch = $db->fetch($query)) {
if (++$count == 6) {
echo '</div><div class="group">';
$count = 0;
}
echo '<div class="result">' . $fetch['title'] . '</div>';
}
?>
</div>
create a counter that increments every time the loop is ran, then at the beginning of each loop check the value. if the value == 6 then close the current div and open a new one with the class change (you could make 2 counters to flip flop back and forth). Reset your counter after the div change.
--edit added code--
Make yourself 2 'group' div classes, 'group1' and 'group0' for the flip-flop
<div class="group1">
<?php
$count = 0;
$divstyle = 1;
while ($fetch = $db->fetch($query)) {
if (++$count == 6) {
echo '</div><div class="group'.(++$divstyle % 2).'">';
$count = 0;
}
echo '<div class="result">' . $fetch['title'] . '</div>';
}
?>
</div>
This might be a bit bad...
<?php $cnt=0;while ($fetch = $db->fetch($query)) { ?>
<div class="result"><?php if($cnt%6==0){echo "<div class=\"group\">";} echo $fetch['title'];if($cnt%6==0){echo "</div>";} $cnt++;?></div>
<?php } ?>

Categories