Total count inside an if of while loop - php

I'm trying to figure out how to do the totalCount inside an if of a while loop
but so far are not successfull in doing so
How can i reach my goal with this code
$rowCount = 1;
while ($clientrows = $statement->fetch()) {
// should return 1000 rows
if ($clientrows['company'] != $current_client) {
echo $rowCount++;
$rowCount++;
// return clientX (one time)
}
if ($clientrows['time'] != NULL ) {
// returns X rows of clientX(from the previews count
// could be any number but lets say here it should be 100 rows
echo $rowCount++;
$rowCount++;
// i need the total counts inside this IF($clientrows['time'] != NULL )
}
echo $rowCount++; // doesn't show anything
// create here the IF total count of previews IF function
// if($totalCounts) {
// echo ' Do something here ';
// }
}
echo $rowCount++; //shows the total rows of while loop while ($clientrows = $statement->fetch())
if i put the count++ right after the IF function it doesn't show anything if i do it at the end then it counts the total of everything
EDIT
Number of counts returns all the rows of my while loop of while ($clientrows = $statement->fetch() (that is about 1000)
what i need to achieve is to get the total rows of this IF statment
if ($clientrows['time'] != NULL ) { }
Which could be 100 or 101 or 200 of the 1000 rows
Help?

"++" is incrementation. It will add + 1 into your variable. It's equal as $a = $a + 1;
Intead of
echo $rowCount++;
$rowCount++;
write
echo $rowCount;
$rowCount++;
Edit.
I just read your comment in script
//shows the total rows of while loop while ($clientrows = $statement->fetch())
Can not you just simply use $statment->num_rows()?

When you do echo $rowCount++; you increment the $rowCount even if you only echo it.

Related

If rowCount empty, try again

I am running a query from a table whose data gets refreshed (deleted and re-inserted) every 30 seconds.
I want my query (which outputs row count) to detect that the row count is null (or zero) and re-run the query. Maximum retries: 5.
And after 5th retries, if it's still zero, I want the row count to print "0".
I know it's a loop but I don't know how to loop it from within the if for row.
<?php
$con=mysqli_connect("HOST","USERNAME","PASS","TABLENAME");
$sql="SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$number = 0; //init count for loop
if ($result=mysqli_query($con,$sql))
{
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
$number = $number+1; //increment number for loop
}
mysqli_close($con);
?>
Use a do while loop and only leave the loop if the rowcount is higher than 0 or 5 retries have been reached.
<?php
$sql = "SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$count = 0;
$retry = 5;
do {
// Count the rows
if ($result = mysqli_query($con, $sql)) {
$count = mysqli_num_rows($result);
}
// Wait for 10ms
if ($count === 0) {
usleep(10000)
}
} while ($count === 0 && --$retry > 0);
mysqli_close($con);
This is however very bad practice, I would recommend looking for another approach. Why would you clear and then refill the table?
Still new to StackOverflow but can you try this?
<?php
$con=mysqli_connect("HOST","USERNAME","PASS","TABLENAME");
$sql="SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$number = 0; //init count for loop
if ($result=mysqli_query($con,$sql)) {
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
$number = $number+1; //increment number for loop
if ($number = 5) {
printf(0);
break 2;
}
}
mysqli_close($con);
?>
$i = 0;
do {
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
} while ($count == 0 && $i++ < 5)
As i understood you are trying to write a Web Based Game. Actually your method is probably going to make database a little busy. You don't have to loop queries. If you are going to do it, we are talking about milliseconds here. Let's say your queries are generally giving results in 0.05 seconds that means for 5 query you are just going to have the data of 0.25 seconds.
You can write your game or whatever easily with COMET Programming. But if you decided to go with loop queries , there is only one thing you will do and it's for loop.
$rowcount=0;
do {
if ($result=mysqli_query($con,$sql)) {
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
}
} while(++$number<5 && $rowcount!=0);
that is basicaly the answer to your request, though it needs some more explanation to be useful.

Creating a loop that checks several tables and counts the results depending on value

I'm not sure what to do when my if statement is true and how to count each hit.
My code first checks a table to get all the companies that are active.
I then want to go to each table for that company that is active, get the last entry, and count how many values for Status are either not equal to 3(!=3) or equals 2(==2).
I have no problems with this code giving me values from the row for each company but I'm not sure how to count this.
// Get a list of active companies
$sql_companyinfo="SELECT Name FROM Company_Info Where Active=True ORDER BY Name";
$result_companyinfo = mysqli_query($conn, $sql_companyinfo);
// Create variables and Loop for each active Company
while($prop_info = mysqli_fetch_assoc($result_companyinfo)) {
$prop_name = $prop_info["Name"];
$company_table = ("Company_" . "$prop_name");
// Query Table and get the last record added
$result_company = mysqli_query($conn,"SELECT * FROM $company_table WHERE ID=(SELECT MAX(ID) FROM $company_table)");
$row1 = mysqli_fetch_array($result_company);
//Status from Last Row Inserted
$Row_Status = $row1['Status'];
// This is where I get lost. For each company I need to know how many companies where $Row_Status != 3 or == 2
if ($Row_Status != '3') {
// Start with 0 and add 1 for each match
for($n3 = 0; $array[$n3]; $n3++) {
$n3_result = $n3;
}
} elseif ($Row_Status == '2') {
// Start with 0 and add 1 for each match
for($e2 = 0; $array[$e2]; $e2++) {
$e2_result = $e2;
}
{
echo "ERROR: Could not execute";
}
}
}
echo $n3_result;
echo $e2_result;
How to creating a loop that checks several tables and counts the results depending on value?
See you can do like this na:-
$Row_Status = '2';
if ($Row_Status != '3') {
echo 'ok1';
} else {
if ($Row_Status == '2') {
echo 'ok2';
} else {
echo 'ok3';
}
}

How to remove rows below a certain row number phpexcel

I would like to remove the bottom records. When I reach row 6 anything below that I would like to remove in phpexcel.
for($tot=2; $tot<$highestRowEMDist; $tot++){
$chkven = $objPHPExcel->getActiveSheet()->getCell('C'.$tot)->getValue();
if ($chkven!="Test" ) {
$objPHPExcel->getActiveSheet()->removeRow(7,0);
}
}
All I want is if I reach row 6 anything below that needs to be deleted
I have found the solution
for($row=2; $row < $highestRowEMDist; $row++){
$value = $objPHPExcel->getActiveSheet()->getCell('C'.$row)->getValue();
if ($value != "Quest") {
$objPHPExcel->getActiveSheet()->removeRow($row, $row);
}
}

php mysql - echo clear division after 3rd result

I echo results in DESC order from MySQL table with while loop. I've pagination system implemented already and its size is 9 records per page. The problem is, that if I do:
// ECHO CSS BREAK
if($row['id'] % 3 == 0){
echo '<li class="clear"></li>';
}
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
echo '<li>...echo code...</li>';
// problem = implement that echo css break in ASC order
}
Use a loop variable, e.g.
$i = 0;
Then instead of
if ($row['id'] % 3 == 0) {
do
if (++$i % 3 === 0) {
This makes sure it always happens are the third [sixth, ninth, ...] time.
You may want to get arbitrary rows from the database at another point in time, or shuffle the results -- relying on row IDs is not a good idea.
Is this what you are looking to implement?
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
if($row['id'] % 3 == 0){
echo '<li>...echo code...</li>';
echo '<li class="clear"></li>';
} else {
echo '<li>...echo code...</li>';
}
}

PHP mysql_fetch_array

$n=mysql_num_rows($rs);
$i=0;
while($n>0)
{
while(($row=mysql_fetch_array($rs))&&$i<=5)
{
echo $row['room_name'];
$i=$i+1;
//echo $n."<br>";
}
echo "<br>";
//echo "n1=".$n;
$n=$n-5;
//
$i=0;
}
Output:101102103104105106
108109110
The row for roomname 107 is missing....
anybody please tell me what is the problem while reentering the loop again...
When $i becomes 6 you fetch a row but do nothing. Because fetching happens before the $i<=5 check, the fetched row gets skipped.
Change the order of conditions in the while loop.
while(($row=mysql_fetch_array($rs))&&$i<=5)
To
while($i<=5 && ($row=mysql_fetch_array($rs)))
Just to follow up on my comment, this whole chunk of code could have been written much more clearly as follows. (assuming you meant to put in a <br> after every 5 records, right now you're doing it after 6 but I think that's probably a mistake)
$rownum = 1;
while ($row = mysql_fetch_array($rs))
{
echo $row['room_name'];
if ($rownum % 5 == 0)
echo '<br>';
$rownum++;
}
here if are checking $i<=5 condition so array stats from 0 , so your database values stats from 101,102,..106, so it will 6 elements .
$i<=5 condition remove this condition in while keep the condition if($i%5==0) echo ""; it will works

Categories