The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Echo out how many Ones that is generated via simple PHP script
sanoj96
post Jan 14 2018, 06:40 PM
Post #1


Advanced Member
****

Group: Members
Posts: 100
Joined: 18-September 12
Member No.: 17,803



Hello,


I have made a race that finds all numbers that 7 can be divided up to 1000. The task is to find out how many of these numbers exist, that is the number. Thus I put all the values ‚Äč‚Äčinto a common number that was 1.

What I'm wondering is how I can show, with simple php coding, how many 1s are being generated, ie something with count or something

Here is the PHP Code i have writen!


CODE

    <?php
  
    for ($number=1; $number<=1000; $number++)
    { if ($number % 7 == 0) {
        //print"$number <br>";}
        $antall = $number%$number == 0;
      
    }
     $enere = array("1");
    echo count ($enere);
    }
  
    ?>


Thanks in advanced!
Sanoj96
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 15 2018, 05:11 AM
Post #2


.
********

Group: WDG Moderators
Posts: 7,814
Joined: 10-August 06
Member No.: 7



You mean you want to find all integers that can be divided evenly with 7 (I don't remember much of modulus)? Create an empty array before the loop, then populate the array with items inside the IF statement. After the loop you can get the number of array items with count().

(BTW I might generally start such counters with $number=0, just for good practice, but in this case I guess $number=7 is lowest possible integer.)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
sanoj96
post Jan 15 2018, 11:04 AM
Post #3


Advanced Member
****

Group: Members
Posts: 100
Joined: 18-September 12
Member No.: 17,803



The script is posting lots of "1" to the website. is there a way to count em? Like if it post 15 "1"s it should be a way that the site says "There is 15 1s generated."

I have tried a few things out now and i cant get it working.

(Long time since i have been working with PHP so i have forgoten allot of it.)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 15 2018, 11:43 AM
Post #4


.
********

Group: WDG Moderators
Posts: 7,814
Joined: 10-August 06
Member No.: 7



QUOTE
(Long time since i have been working with PHP so i have forgoten allot of it.)

Same here. tongue.gif

If you add an item to the array instead, you can count the array items after the loop has finished with count().

Or you could increment the value of a counter variable, something like:

CODE
$matches=0;
for(...)
{
    if(...)
    {
        $matches++;
    }
}
echo "There are $matches 1s generated";
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 21st May 2018 - 05:06 PM