I have a prepared query with the following result:
12
12
12
14
14
14
14
14
11
11
2
2
2
I simply want to return the number and its frequency:
12 3
14 5
11 2
2 3
My code is as follows:
$count =0;
while($DNB -> fetch())
{
// echo "<br/>";
$nomatch = 0;
if($count ==0)
{
array_push($array, array($postalcode ,1));
// echo $array[0][1];
}
else
{
for($i = 0; $i < count($array);$i++)
{
if($array[$i][0] != $postalcode)
{
$nomatch ++;
if($nomatch == count($array) -1)
{
array_push($array, array($postalcode ,1));
//echo $array[$i][1];
}
}
else
{
$array[$i][1]++;
}
}
}
$count = $count +1;
}
I am not sure what I am doing wrong.
You should use "SELECT id, COUNT (*) FROM table GROUP BY id" as the initial sql query.
Related
can you guys please help me for this? I want to loop numbers from 1 to 100..
if the number is multiple of 4 it will print 'NEW', if number is multiple of 7 it will print 'TEST', and if the number is multiple of both 4 and 7 it will print 'NEWTEST'.
I've output the multiple of 4 and multiple of 7 but in both 4 and 7 I cant print the 'NEWTEST'.
Here's my code.
Thank you guys
function primeno($n){
for($i = 1; $i < 100; $i++){
if ($i % 4 == 0){
echo 'easy<br>';
}else if($i % 7 == 0){
echo 'EMPLOYER<br>';
}
else if($i % 4 == 0 && $i % 7 == 0){
echo 'easyEMPLOYER<br>';
}else{
echo $i."<br>";
}
}
}
primeno(100);
Here's my output:
1
2
3
NEW
5
6
TEST
NEW
9
10
11
NEW
13
TEST
15
NEW
17
18
19
NEW
TEST
22
23
NEW
25
26
27
NEW ----> it should be NEWTEST
29
30
No. 28 should be output 'NEWTEST' but instead it output NEW
You just need to swap your conditions in your "if" and your last "else if" statements
<?php
function primeno($n) {
for ($i = 1; $i < 100; $i++) {
if ($i % 4 == 0 && $i % 7 == 0) {
echo 'easyEMPLOYER<br>';
}
else if ($i % 7 == 0) {
echo 'EMPLOYER<br>';
}
else if ($i % 4 == 0) {
echo 'easy<br>';
}
else {
echo $i . "<br>";
}
}
}
primeno(100);
?>
I am stuck with this php loop. Then n = 3, k = 5, s = 21.
Can anyone help me please ?
Rows 3. In the first row 5 chairs:
1 row: ⑁⑁⑁⑁⑁ (5 chairs)
2 row: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
3 row: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Chairs in total: 21
<?php
for ($i=0; $i<=6; $i++) {
for ($j=0; $j<$i; $j++) {
if($i == 1) {
echo '';
}else{
echo '⑁';
}
} echo ' ';
}
?>
If I understand correctly what you want,
$n is number of row you needed,
$k number of chair at row 1.
Each row chair is added by two.
<?php
$n = 3;
$k = 5;
for($i = 0; $i < $n; $i++) {
if($i >= 1) {
echo PHP_EOL;
}
echo str_repeat('-', $k + ($i * 2));
}
Example: When n = 3 and k = 8, it must be obtained that order s = 30 chairs.
Create a PHP solution that specifies the N-row queue in the variables, K-how many chairs should be in the first row.
For example: N = 3; K = 5;
After loading the page (after performing the program actions with the variables available) the following should be displayed:
Rows 3. First row 5 chairs:
Queue 1: ⑁⑁⑁⑁⑁ (5 chairs)
Queue 2: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
Queue 3: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Total required chairs: 21
I think you want to use 'if' syntax. Is it right?
If it's not correct, You should describe your work more detail
<?php
// ...
for($i = 0; $i< sizeof($rows); $i++ ){
if ($i % 2 == 0) {
print "It's even";
}
}
// ... If a row is an Array, use this one.
for($i = 0; $i< sizeof($rows); $i++ ){
for($j = 0; $j< sizeof($rows[$i]); $j++ ){
if ($j % 2 == 0) {
print "It's even index of a row";
//do Something.
}
}
}
?>
I tried to do a while inside a while to print a multiplication table like,
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But I got only 1, 2, 3, 4, 5.
Code:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
This is happening because you're not resetting $x when the inner loop completes its iteration. Try this instead:
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
You need to reset $x, so:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
$x = 1; // added this line
echo "<br>";
$i++;
}
Output:
12345
246810
3691215
48121620
510152025
You can then do what ever you want to format it.
More elabrate explanation:
First run:
It enters both outer and inner loops, showing the desired output for the first line. You end up with $i = 2 and $x = 6.
Second run:
Since $i is 2, it doesn't leave the outer loop, but $x is 6, so it doesn't enter the inner loop again.
Last* run:
It then keeps adding 1 to $i until it doesn't match the outer loop condition anymore and leaves you with that unwanted result.
Use this
This is because you have not initialized your $x after external while loop completes its one cycle. so after one cycle inner loops does not run
<?php
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
DEMO ONLINE
php code:
$i = 1;
while($i <= 5){
$x = 1;
while($x <= 5){
echo $i * $x." ";
$x++;
}
echo "<br/>";
$i++;
}
result:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
This is my thought process of how I think it should work.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) && ($row % 11 != 0)) {
$counter++;
if ($counter % 10 == 0) {
// Do something.
}
}
}
Example of how it should work:
Row 1 = Counter 1
Row 2 = Counter 2
Row 3 = Counter 3
Row 4 = Counter 4
Row 5 = Counter 5
Row 6 = Counter 6
Row 7 = Counter 7
Row 8 = Counter 8
Row 9 = Counter 9
Row 10 = Counter 10
Row 11 = Skip
Row 12 = Counter 11
Row 13 = Counter 12
Row 14 = Counter 13
Row 15 = Counter 14
Row 16 = Counter 15
Row 17 = Counter 16
Row 18 = Counter 17
Row 19 = Counter 18
Row 20 = Counter 19
Row 21 = Counter 20
Row 22 = Skip
Row 23 = Counter 21
etc.
For further reference, here is the full piece of code I've written to remind users on my web-store that if they have 10 tickets in their cart, they'll get the next one free if they add it. Thanks Jakar
$fancyCounter = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$rawCounter++;
if ($rawCounter % 11 !== 0) {
$fancyCounter++;
}
else {
$fancyCounter = 1;
}
}
}
$result->close();
if ($fancyCounter % 11 == 0) {
$freeTicketAvailable = 1;
} else {
$freeTicketAvailable = 0;
}
Here, there is a $rawCounter which counts the rows iterated and a $fancyCounter which keeps the row count just how you posted.
while($row=$result->fetch_assoc()){
$rawCounter++;
if ($rawCounter%11!==0){
$fancyCounter++;
}
if ($rawCounter%10===0){
//Do Something
}
}
Here is an example that give your example output.
If you need the row number and the counter number separate, you could also use a $counter and a $skipCount, and always increment $counter and only increment $skipCount on $counter%11==0 and to get the counter count you want (ie Row 12 = Counter 11, Row 23 = Counter 21), you'd use echo "Row {$counter} = Counter ".($counter-$skipCount)
Try this,
<?php
$counter = 1;
while ($counter <= 33) {
if ($counter % 10 == 0) {
// On 10.
echo "Do Something<br/>";
} else if ($counter % 11 == 0) {
// On 11.
echo "Skip<br/>";
} else {
// Other numbers.
echo $counter . "<br/>";
}
$counter++;
}
?>
You need to replace the condition inside the while(); to suit you.
Output
1
2
3
4
5
6
7
8
9
Do Something
Skip
12
13
14
15
16
17
18
19
Do Something
21
Skip
23
24
25
26
27
28
29
Do Something
31
32
Skip
This should do it. You were trying to divide an array ($row) by a number in your while condition, which is probably undefined even in PHP ;)
if ($result->num_rows > 0) {
$skipCounter = 0;
$tenCounter = 0;
$counter = 0;
while($row = $result->fetch_assoc()) {
++$skipCounter;
if ($skipCounter == 11) {
$skipCounter = 0;
continue;
}
++$counter
++$tenCounter;
echo "Counter: ".$counter;
if ($tenCounter == 10) {
$tenCounter = 0;
echo "Do something";
}
}
}
What i want to print is
1
3 5
7 9 11
With my current code , that is ...
<?php
function Odd($limit='20'){
$c = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c ;
echo "<br/>";
}
$c++ ;
}
}
Print Odd();
?>
i am getting
1
3
5
7
9
11
Can someone please guide me the right way ?
Aaah ... ok.^^ Now i got it.
Its pretty easy: You need another variable which counts up and one which limits the breakposition. Looks like this:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
}
$c++ ;
}
}
Print Odd();
?>
Output till 40:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39
Edit: Code for your new request:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
$c++ ;
}
}
Print Odd();
?>
So if I understand correctly you want to output something like that:
1
3 5
7 9 11
13 15 17 19
Here is my solution:
function Odd($limit='20'){
$c = 1;$some_array = array();
while($c <= $limit){
if ($c % 2!=0){
$some_array[]=$c;
}
$c++ ;
}
return $some_array;
}
$array = Odd();
$nr =0;
$j=1;
foreach ($array as $key => $value) {
echo $value.' ';$nr++;
if($nr==$j){
echo '<br />';
$nr=0;
$j++;
}
}
Hope this helps!
From your question it Seems you are really new to programming so before writing any program first of all observe the question properly:
For example for the question above it is clear that is an triangle of odd numbers.
now the number of odd numbers on each row is equal to the row
i.e 1st row contains 1 number ,2nd contains 2 and it continues...
Now what we do is take an variable to count the no of rows say $row and the other will be $limit .
<?php
function odd($limit){
$row=1;
$current_number=1;
while($current_number<=$limit){
for($i=1;$i<=$row;$i++){
echo $current_number." ";
$current_number=$current_number+2;//incrementing numbers by 2 if you want to increment by 1 i.e print all numbers replace 2 by 1
}
$row++;
echo "<br/>";//for new line
}
}
To run above function you need to call it and pass the value of $limit.To do it just type anywhere outside of this function.
odd(20);
Watch this running here: