I have the following query. I want to show the time if number of Adults is less than 10.
$result = mysql_query("SELECT sum(Adult) AS Number1, Time FROM orders WHERE confirmed = 'Y' and BookDate = '2013-04-01' GROUP BY Time");
while($row = mysql_fetch_array($result)) {
if ($row['Number1'] < '10')
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
so it will come out like this...
<div class="result">18:00:00 (8)</div>
<div class="result">18:30:00 (5)</div>
<div class="result">19:00:00 (6)</div>
However I also want to show that 18:15:00 has 0 Adults.
I have tried adding some if statement like this
if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10')) {
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else {
echo '<div class="result">18:15:00 (0)</div>';
}
but it keeps looping. Can someone show me where I am going wrong?
If you want to break out of the loop statement, you can use the break statement:
while(condition){
if(conditionMet){
break;
}
}
I'm not really sure what you're trying to do, but maybe this is it:
if ($row['Number1'] < '10') {
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
} elseif ($row['Time']) == '18:15:00') {
echo '<div class="result">18:15:00 (0)</div>';
}
Change:-
if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10'))
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else
{
echo '<div class="result">18:15:00 (0)</div>';
}
to
if ($row['Number1'] < '10')
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
elseif(($row['Time']) == '18:15:00')
{
echo '<div class="result">18:15:00 (0)</div>';
}
Related
I have a while loop from database, but i want to wrap every 2 rows into a div.
Currently i have this but is only wrapping first row.
<?php
$get = $connect->query("SELECT * FROM sabiaque ORDER by ordem");
$num = 0;
while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
if ($num % 2) {
echo '<div class="divisor-slide">';
}
echo'
<a href="'.$f['link'].'"><div class="item-a">
<div class="box1-item-a">
<div class="i-wrap"><h1 class="i-white">'.utf8_decode($f['titulo']).'</h1><p>'.utf8_decode($f['subtitulo']).'</p></div>
</div>
<div class="box1-item-b">
<img src="../'.$f['ficheiro'].'" style="max-width: 100%; height: 100%;" />
</div>
</div></a>
';
if($num % 2) {
echo '</div>';
}
$num++;
}
?>
change this :
if($num%2) {
echo '<div class="divisor-slide">';
}
By this :
if( ($num % 2) == 0) {
echo '<div class="divisor-slide">';
}
And this :
if($num %2) {
echo '</div>';
}
To :
if( ($num % 2) == 0) {
echo '</div>';
}
I often find I get into a muddle using these special $num%2 whatsits (I'll get all excited in being clever in using them, then forget later what they do when I revisit the code). Why not simplify it like this (although it uses a little more code):
$num = 1;
while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
if ( $num == 2 ) echo '<div class="divisor-slide">';
// rest of code
if ( $num == 2 ) echo '</div>';
$num++; if ( $num == 3 ) $num = 1;
}
My code is below:
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
$i++;
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
}
echo '</div>';
}
}
portfolio_gallery();
I wanted to echo </div><div> for every after 9th item of the loop but every time I executed the code, the first echo only happened after 8 items instead of 9, but the rest was every 9th.
You have to increment
$i
after
if($i % 9 == 0)
follow the syntax example i worked out its working
<?php
$j=0;
for($i=0;$i<=50;$i++)
{
if($j==9)
{
echo $j.'hioiiiiiii<br/>'; //echo "</div><div>";
$j=-1;
}
$j++;
}
?>
Please try this :)
<?php
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++;
}
echo '</div>';
}
}
declare $i = 1 and Write $i++ at the end of while loop.
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 1; // declare $i = 1 here
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 1){ // 10 , 19 ,28
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++; // increment at end of the loop
}
echo '</div>';
}
I was able to solve it by modifying the condition:
So instead of: if($i % 9 == 0) {...}
I used: if($i!=0 && $i % 9 == 0) {...}
And also placing $i++ at the end of while loop.
So I am trying to wrap a div around my database results. The div must appear before first row and after the 6th row, it needs to close the div and loop it again. I tried to do it, but I can't manage to make it work properly. Any help would be appreciated.
for ($i=0; $i<count($row); $i++) {
if (($i) % 6 == 0) {
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6">
<img src="images/preview.jpg">
</div>';
if (($i) % 6 == 0) {
echo '</div>';
}
}
Also the code sample was not working at all (for/if/else not closed), see if this helps:
echo '<div class="swiper-slide">';
for ($i=0; $i<count($row); $i++)
{
if (($i>0) && ($i%6==0)) {
echo '</div>'."\n".'<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
}
echo '</div>';
test at eval.in
try this
<div class="swiper-slide"><!-- open it using HTML out of <??> php tags-->
<?php
for ($i=0; $i<count($row); $i++) {
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if ($i % 6 == 0) {
//close and open new div for each group of 6
echo '</div>';
echo '<div class="swiper-slide">';
}//every 6th ... (you may need to change itto ( $i % 5 == 0)
}//for loop
echo '</div>';//close last opened, in case no more data or data less that 6
?>
Your second modulo operator compares the result to 9 instead of 0, and you have a } missing:
for ($i=0; $i<count($row); $i++) {
if (($i) % 6 == 0) {
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if (($i) % 6 == 0) { // Corrected 9 to 0
echo '</div>';
}
}
Try this:
foreach ($row as $key=>$result) {
if ($key==0 || $key == 5) { //If this is the first or sixth row.
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if ($key==0 || $key == 5) {
echo '</div>';
}
}
I'm not exactly sure how to explain this but basically I have an x variable equal to the number of rows in a database and each time it is equal to 4 or less, run a piece of code.
So say I have 5 rows, I want to execute the code twice because x is equal to 4 once and then equal to 1. Same if I had 6 rows (4+2) 7 rows (4+3) or 8 rows (4+4).
Then, when I reached 9 rows (4+4+1), it would execute the code thrice. Etc...
The code I'm trying to run is a piece of HTML with some more PHP inside it. I want to create a new <ul> with the said code inside it each time the number of rows is equal to 4 or less as explained above.
The code as it is right now:
<div class="row-fluid well">
<?php
$num_rows = mysql_num_rows($result);
if ($num_rows <= 4) {
?>
<ul class="minigames">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<li class='span3'>";
if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
}
?>
<img src="<?php echo $row['app_preview']; ?>">
<div class="minigames-caption">
<h4><?php echo $row['app_name']; ?></h4>
<p>By <?php echo $row['app_dev']; ?>.</p>
</div>
</a>
<?php
echo "</li>";
}
?>
<?php
}
?>
If the number of rows are limited, I would put the results into one array and then use array_chunk() to split them into portions.
$results = array();
while ($row = mysql_fetch_array($result)) {
$results[] = $row;
}
foreach (array_chunk($results, 4) as $chunk) {
echo '<ul>';
foreach ($chunk as $row) {
echo '<li>', /*....*/, '</li>';
}
echo '</ul>';
}
Divide x by 4 and round it up. Eg 4/4 = 1, 5/4 = 2, 9/4 = 3. Then, use a for loop.
var noOfTimes = //x/4 rounded up
for (var i = 0; i < noOfTimes; i++) {
//insert code here
}
Use the ceil function.
http://us3.php.net/manual/en/function.ceil.php
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < ceil($num_rows / 4); $i++) {
// do stuff
}
I'm trying to accomplish this, with the 360 grid system: http://imgur.com/4ZFll
From a database i'm getting products which will be displayed in lines with 4 on each.
It's working perfectly if there is exactly 4 products under each category, but if there is less than 4 products in a category, the design is messed up, because the div's not closed properly.
Problem is that sometimes there's only 3 or less products on a line.
Is there any of you who knows how to accomplish this?
for($i=0 ; $i<$countprod ; $i++){
$prevprod = $products[$i-1]['name'];
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i == 0){ echo '<div class="grid_3 '; }
if ($i % 4 == 0) { echo ' alpha">'; }
elseif($i % 4 == 3) { echo '</div><div class="grid_3 omega">'; }
else{ echo '</div><div class="grid_3">';
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
echo '<div class="grid_3';
}
}
(sorry about the title, i didnt know what to call this question :) )
echo '<div class="grid_3';
You aren't closing this tag.
Have a try with
$countprod = count($product);
$prevprod = '';
$close_div = false;
for ($i=0; $i<$countprod; $i++){
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
if ($close_div) echo '</div>';
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i % 4 == 0) {
echo '<div class="grid_3 alpha">';
$close_div = true;
}
elseif ($i % 4 == 3) {
echo '</div><div class="grid_3 omega">';
$close_div = true;
}
else {
echo '</div><div class="grid_3">';
$close_div = true;
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
$close_div = false;
}
$prevprod = $curprod;
}
$p = 10; // Current number of products
$ppr = 4; // Products per row
$x = $i % $ppr;
if($x != 0){
$countprod = $p + ($ppr - $x);
}
echo $countprod; // 12 (4 * 3)
Loop with FOR if there is no product just print empty DIV, if that's what you have asked...