i am exploring some old php files for the nostalgia, and i noticed this is what ive written before to fetch some records:
<?php
for ( $counter = 0; $row = mysqli_fetch_row( $result );++$counter )
{
print( "<tr>" );
foreach ( $row as $key => $value )
{
if($key == '6')
{
if($value == '1'){print( "<td>12/9/15 6:00PM - 7:00PM</td>" );}
if($value == '2'){print( "<td>12/9/15 7:00PM - 8:00PM</td>" );}
if($value == '3'){print( "<td>12/9/15 8:00PM - 9:00PM</td>" );}
if($value == '4'){print( "<td>12/10/15 6:00PM - 7:00PM</td>" );}
if($value == '5'){print( "<td>12/10/15 7:00PM - 8:00PM</td>" );}
if($value == '6'){print( "<td>12/10/15 8:00PM - 9:00PM</td>" );}
}
else
{
print( "<td>$value</td>" );
}
}
print( "</tr>" );
}
mysqli_close( $database );
?>
i changed it to while ($row = mysqli_fetch_row( $result )) and it works as well, as expected. i am just trying to understand the logic behidn how the for loop method works when $counter isnt being used...
i am scrambling to understand why i used this kind of logic, and how is it even working when $counter isnt even used for indexing or anything.
Easiest way to understand how this is working is by dissecting the for loop.
for (<initialization>; <condition>; <update expresion>)
In your case you have a counter that is being set initially, and incremented.
for ( $counter = 0; $row = mysqli_fetch_row( $result );++$counter )
You aren't using the counter, but it actually doesn't matter since the only thing that determines if the loop continues is if the condition is true. Since eventually mysqli_fetch_row stops returning results, the loop will terminate. The $counter variable is unused and ignored for the purpose of the loop.
You could also rewrite your for loop to look like:
for (;$row = mysqli_fetch_row( $result );) and this would also work the same. But then, what is a while loop? It's just a for loop without an initialization and update expression. Thus you could rewrite this again as:
while ($row = mysqli_fetch_row( $result ))
I hope this clears things up.
Related
When I try to delete, I can't delete it properly. It will stuck at number 2
if(isset($_GET['action'])) {
$mahasiswaa = simplexml_load_file('input.xml');
$nim = $_GET['nim'];
$index = 0;
$i =1;
$i++;
foreach($mahasiswaa->mahasiswa as $mahasiswa){
if($mahasiswa['nim']==$nim){
$index = $i;
break;
}
}
unset($mahasiswaa->mahasiswa[$index]);
file_put_contents('input.xml', $mahasiswaa->asXML());
}
your $i variable is constantly 2, the i++ should probably be inside the loop?
also, if your condition is never met, $index is 0 and its use will likely lead to an error.
The code will act only on the first match, whats ok, if that is what you expect.
My goal is to iterate over all rows in a specific ColumnFamily in a node.
Here is the php code (using my wrapper over phpcassa):
$ring = $cass_db->describe_ring();
foreach ($ring as $ring_details)
{
$start_token = $ring_details->start_token;
$end_token = $ring_details->end_token;
if ($start_token != null && $end_token != null)
{
$i = 0;
$batch_size = 10;
$params = array(
'token_start' => $start_token,
'token_finish' => $end_token,
'row_count' => $batch_size,
'buffer_size' => 1000
);
while ($batch = $cass_db->get_range_by_token('myColumnFamily', $params))
{
var_dump('Batch# '.$i);
foreach ($batch as $row)
{
$row_key = $row[0];
$row_values = $row[1];
var_dump($row_key);
}
$i++;
//Just to stop infinite loop
if ($i > 14)
{
die();
}
}
}
}
get_range_by_token() uses default parameters overwritten by $params.
In each batch I get the same 10 row keys.
How to iterate over all existing rows in a large Cassandra DB?
I am not a PHP developer so I may misunderstand something in your code. More, you did not specify which cassandra version you are using.
Iteration on all rows is generally done starting and ending with an empty token, and redefining the start token in each iteration. In your code I can't see where you redefine token_start in each iteration. If you don't redefine it you're querying cassandra everytime for the same range of tokens and you will get always the same resultset.
Your code should do something like this ...
start_token = '';
end_token = '';
page_size = 100;
while ( get_range_by_token('cf', start_token, end_token, page_size) {
// here I should get page_size rows (unless I'm in last iteration or table rows is smaller than page_size elements)
start_token = rows[rows.size()].getKey();
}
HTH,
Carlo
I need to use the goto operator in my code as I can't seem to think of a way around it. However the problem is my host only has PHP version 5.2.17 installed.
Any ideas?
Below is my code:
if ($ready !=="y")
{
$check=mysql_query("SELECT `inserted` FROM `team`");
$numrows = mysql_num_rows($check);
$i="0";
while ($i<$numrows && $row = mysql_fetch_assoc($check))
{
$array[$i] = $row['inserted'];
$i++;
}
if (in_array("n", $array))
{
goto skip;
}
else
{
mysql_query("
UPDATE game SET ready='y'
");
}
}
skip:
There are a few anti-patterns in your code. Let's clean it up. I'll explain what's been changed in a jiffy.
if($ready !== "y") {
$sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
if(mysql_num_rows($sth) > 0) {
mysql_query("UPDATE game SET ready = 'y'");
}
}
First things first: There is no need to perform a query, fetch all of the results, then loop through those results (in_array) looking for a specific value. Let the database do that for you by expressly looking only for rows where inserted is the string literal "n".
Because we know that we're only getting "n" records back, we just need to check if there are any results. If so, run the query. If there are no "n" records, the UPDATE isn't run.
If you need to know that the UPDATE ran, add a check for it:
$ran_update = false;
if($ready !== "y") {
$sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
if(mysql_num_rows($sth) > 0) {
mysql_query("UPDATE game SET ready = 'y'");
$ran_update = true;
}
}
if($ran_update) {
// ...
}
You want to use the correct control word to break from the loop:
if ($ready !=="y")
{
$check=mysql_query("SELECT `inserted` FROM `team`");
$numrows = mysql_num_rows($check);
$i="0";
while ($i<$numrows && $row = mysql_fetch_assoc($check))
{
$array[$i] = $row['inserted'];
$i++;
}
if (in_array("n", $array))
{
break;
}
else
{
mysql_query("
UPDATE game SET ready='y'
");
}
}
The break keyword will do exactly what you want: End the execution of the while loop. Never ever ever use a goto!
As a direct response to the title of this post:
do{
if (!$condition){
break;
}
// Code called if conditions above are met
}while(0);
//Code always called
In some circumstances, this, or a goto, can make for very tidy and readable code.
First, you could use a break to exit your initial loop. Second, if you need to test for anything, set a variable (must be global not local) as a flag or indicator before calling break, then do a conditional test statement where your skip line is to perform any additional steps you need.
I'm running the following query but $new returns 0:
$count = mysql_query("SELECT COUNT(*) FROM flagged WHERE status=0") or die(mysql_error());
$new = mysql_fetch_row($count);
if ($new != 0) {
echo "<script language=\"javascript\">$.titleAlert(\"New Logs - ($new[0])\");</script>";
}
Problem is the if-condition keeps getting met when it shouldn't when $new != 0
I even tried:
if ($new > 0) {
//update title about new logs
}
Either way, the title is still updated and I'm not sure why.
New Logs - (0)
just do
var_dump($new);
and you'll see why your if doesn't work
Not sure what you mean. $new should be an array not a scalar value per the PHP documentation.
mysql_fetch_row documentation
this code
$new = mysql_fetch_row($count);
makes $new becomes an array
try to compare it like this:
if ($new[0] != 0)
or even better, so You would be 100% sure it's what You need.
if (count($new) == 1 && intval($new[0]) != 0)
mysql_fetch_row should return an array or FALSE.
Maybe adjust your conditional to:
if ( !$new ) {
// some code here
}
$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