PHP for loop not starting at 0 - php

So, I have code like this at the start of a bigger set of loops
for ($k=1;$k<=60-$jrow['blocks'];$k += $jrow['blocks'])
{
for ($j=0;$j=(count($sarray)-1);$j++)
{
The size of $sarray is 2
Now, when I apply this:
if (isset($j) AND $k == 1)
{
echo "<h1>".$j."</h1>";
}
I get an output of 1. Now clearly $k is still in it's first iteration, but it seems like $j has somehow skipped onto it's second and isn't starting at 0. What am I doing wrong.
Whole code, if required:
for ($k=1;$k<=60-$jrow['blocks'];$k += $jrow['blocks'])
{
for ($j=0;$j=(count($sarray)-1);$j++)
{
if (isset($j) AND $k == 1)
{
echo "<h1>".$j."</h1>";
}
for ($l=$k;$l=($k+$jrow['blocks']-1);$l++)
{
$uid = $sarray[$j];
$staffquery = $hsdbc->prepare("SELECT * FROM user WHERE userID = :uid");
$staffquery->bindParam(':uid',$uid);
$staffquery->execute();
$staffid = $staffquery->fetch(PDO::FETCH_ASSOC);
if (isset($staffid['userid']))
{
echo "<h1>staff query orking</h1>";
die();
}
if ($staffid['complevel'] > $jrow['complevel'])
{
if ($l + ($jrow['blocks'] - 1) < 20 * $i)
{
$schedquery = $hsdbc->prepare("SELECT * FROM schedule WHERE slot = :sn");
$schedquery->bindParam(':sn',$l);
$schedquery->execute();
$schedrow = $schedquery->fetch(PDO::FETCH_ASSOC);
if ($schedrow['jobID'] == 0)
{
for ($m=$l;$m=($l+$jrow['blocks']-1);$m++)
{
$setquery = $hsdbc->prepare("UPDATE schedule SET jobID = :jid WHERE userID=:uid AND slot = :sn");
$setquery->bindParam(':jid',$jrow['jobID']);
$setquery->bindParam(':uid',$staffid['userid']);
$setquery->bindParam(':sn',$m);
$setquery->execute();
}
$cjobquery = $hsdbc->prepare("UPDATE job SET statusID = 1 WHERE jobID = :jid");
$cjobquery->bindParam(':jid',$jrow['jobID']);
$cjobquery->execute();
Break 6;
}
}
}
}
}
}

I think you meant to write. This is probably a typo.
for ($j=0;$j<=(count($sarray)-1);$j++)

$j = (count($sarray)-1)
this is an assignment, use the == operator for equality comparison, or <=, >= for order comparison.

Related

Distribute array value in table column

I have three column having limit of certain percentage of total.i want to distribute an array of values to three columns in such a manner first check customer 1 then 2 then 3 and start from customer 3,2,1 and again from 1,2,3 etc. And also check each value not crossing column limit.Remaining array value should be distribute in column having large percentage assigned.
Array : $stone_doll_inner_arr = array(1=>'67212','37256','32909','29847','28529','27643','25356','25274','23604','23058','18581');
I tried like
Table
for($m=1;$m<=1;$m++){
for($n=1;$n<=count($stone_doll_inner_arr);$n++){
if ($n % 2 == 0) {
$asc_desc = 'asc';
}else{
$asc_desc = 'desc';
}
//Query
$sql_main_table = "select * from create_nemix_tbl where create_id = '".$getid."' order by percentage ".$asc_desc."";
$qry_main_table = $con->query($sql_main_table);
$num_main_table = $qry_main_table->num_rows;
if ($n % 2 == 0) {
$l = $num_main_table-1;
}else{
$l = 0;
}
while($row_main_table = $qry_main_table->fetch_array()){
$stone_doll_val = $stone_doll_arr[$m];
$stone_doll_val_perc = ($stone_doll_val * $row_main_table['percentage']) / 100;
$pre_existing = isset($customer[$l]) ? array_sum($customer[$l]) : 0;
$first_value = $pre_existing+$stone_doll_inner_arr[$n];
$var = 0;
if ($first_value <= $stone_doll_val_perc){
$customer[$l][] = $stone_doll_inner_arr[$n];
unset($stone_doll_inner_arr[$n]);
$var =1;
}
if($var == 1){
break;
}
}
if ($n % 2 == 0) {
$l--;
}else{
$l++;
}
}
}
echo "<pre>";
print_r($customer);
Table Structure and output of script i wanted

Choose a string based on random number

I am working on a bit of PHP and I've come upon a bit of issues.
I am using PHP to randomly choose a number from 1-360. I am trying to compare the answer to a list of value determined by range.
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC = range(0,21) {
$result = "Orange";
}
elseif ($NumberC = range(22,42) {
$result = "Red";
}
elseif ($NumberC = range(43,63) {
$result = "Blue";
}
//This goes on for a while ...
else {
$result = "Green";
}
echo = $result;
Anytime i do this, the result always assigns the value of "Orange" to $result .
Im sure im doing something wrong here, please help!
First of all, you used just one '=' to compare while it should have been '=='. Second range() generates an array and you cannot compare an integer to an array. Third why generating the range every single time when you can check that $NumberC lies between the minimum and the maximum numbers of the range?
Change your code to:
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC >= 0 && $NumberC <= 21) {
$result = "Orange";
} elseif ($NumberC >= 22 && $NumberC <= 42) {
$result = "Red";
} elseif ($NumberC >= 43 && $NumberC <= 63) {
$result = "Blue";
} else {
$result = "Green";
}
echo $result;
Shall work. Hope this helps.

A Logical Algorithm In PHP

I'm a beginner learning PHP. I have tried to make a loop that has a different behaviour for both even and odd numbers. I've been playing around with it for a while, yet I still can't get it to work. Has anyone got a solution?
$count = 0;
$mod = $count % 2;
while ($count < 10)
{
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
A silly mistake, mod inside while() loop.
$count = 0;
while ($count < 10) {
$mod = $count % 2; //Here
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
$count = 0;
$mod = $count %2;
Is were your problem is.
You have to use the modulus (%) operator inside the for loop. Also, there is no need to store the value from the use of the modulus operator at all, it can be compared directly inside the for-loop.
for ($count = 0; $count < 10, $count++) {
if ($count % 2 == 0) {
echo "even, ";
} else {
echo "odd, ";
}
}
You can also switch the while to a for like this.
Welcome to PHP.
Edit #1:
As you are getting a new value of $count every execution of the for-loop the old value if $count % 2 will be incorrect. It has to recalculate for every $count. First it checks if 0 is divisible by 2, then onto 1 and so forth. For every value of $count you have to check the divisibility.
In most programming languages you aren't computing a variable onto another, instead you are taking the value of the variable. Like $a = $b + $c; in that case, if you change the value of $b or $c it does not automatically update $a. Instead you have to call $a = $b + $c again. It is the same with % operator.
$count = 0;
while ($count < 10) {
$mod = $count % 2;
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
use for loop instead of while loop
for($count=0;$count<10;$count++)
{
if(($count % 2) == 0)
echo "even,";
else
echo "odd,";
}

PHP/SQL script not working

I'm trying to generate the top 10 tags, I think it's pretty straightforward.
//generate 10 top tags
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
$topArray = array();
foreach($tagSQL as $poland)
{
if($poland["tagID"] == 1)
{
$topArray[0] = $topArray[1] + 1;
}
if($poland["tagID"] == 2)
{
$topArray[1] = $topArray[2] + 1;
}
if($poland["tagID"] == 3)
{
$topArray[2] = $topArray[3] + 1;
}
if($poland["tagID"] == 4)
{
$topArray[3] = $topArray[4] + 1;
}
}
function printTopTags()
{
$n = 0;
foreach($topArray as $buddha)
{
$n = $n + 1;
if(sizeOf($topArray) > $n)
{
$hersheyBar = " ";
}
else
{
$hersheyBar = "";
}
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'");
foreach($finalFinalEndArray as $waterBottle)
{
echo $waterBottle . $hersheyBar;
}
}
}
The only problem I'm having is that it thinks I have a syntax error somewhere in the code, but no matter which lines I omit the syntax error stays.
Also, is there an easier way to do my if statements for 10 different array spots, rather than the four?
You are missing a ")" at the end of this statement
It should be
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));
optimystique is right about the syntax error.
Regarding your second question, shmeeps' method works for this example, but if you happen to have another situation where you need to compare one variable to a number of different values, then you can use a switch statement instead of a whole bunch of ifs. For example (From the above linked PHP manual page):
<?php
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
?>
is the same as:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
The if-then statements should be able to be generalized as
$topArray[($poland["tagID"] - 1)] = $topArray[$poland["tagID"]] + 1;
with no if-then statements at all.
I don't see any syntax errors. Is there more to this script?
Edit: Saw the error now, this line
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'");
Should be
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));
Note the extra )

PHP foreach loop problem

I have the following code:
//generate 10 top tags
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
$topArray = array();
foreach($tagSQL as $poland)
{
if($poland["tagID"] == 1)
{
$topArray[0] ++;
}
if($poland["tagID"] == 2)
{
$topArray[1] ++;
}
if($poland["tagID"] == 3)
{
$topArray[2] ++;
}
if($poland["tagID"] == 4)
{
$topArray[3] ++;
}
}
function printTopTags()
{
$n = 0;
foreach($topArray as $buddha)
{
$n = $n + 1;
if(sizeOf($topArray) > $n)
{
$hersheyBar = " ";
}
else
{
$hersheyBar = "";
}
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));
foreach($finalFinalEndArray as $waterBottle)
{
echo $waterBottle . $hersheyBar;
}
}
}
I get the error Warning: Invalid argument supplied for foreach() on line 93
Line 93 is foreach($topArray as $buddha).
Any help?
http://ru.php.net/manual/en/language.variables.scope.php
Also
if($poland["tagID"] == 1)
{
$topArray[0] ++;
}
if($poland["tagID"] == 2)
{
$topArray[1] ++;
}
if($poland["tagID"] == 3)
{
$topArray[2] ++;
}
if($poland["tagID"] == 4)
{
$topArray[3] ++;
}
===
if ($poland["tagID"] >= 1 && $poland["tagID"] <= 4)
$topArray[$poland["tagID"] - 1]++;
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
This is very bad practice. If the query fails for any reason whatsoever, mysql_query returns boolean FALSE, which you then blindly pass to mysql_fetch_array, which will then fail in turn because it's expect a mysql result handle, not a boolean, and return a boolean itself.
You then use all this failed data in a foreach loop, and wonder why you're not getting anything but errors?
Looks like $topArray just not defined in printTopTags() function.
You can pass it as a parameter:
function printTopTags($topArray) {
...
}
$topArray is a global variable.
To use it inside a function you either have to pass it as a parameter or use the global keyword to import it:
function printTopTags()
{
global $topArray; // <---- Here!
$n = 0;
foreach($topArray as $buddha)

Categories