Loop not counting [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to get the following code to add how many people can retire and not. the code displays but does not add. What am I doing wrong?
<?php
$canRetire= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired + 1;
}
else{
$notretired = $notRetired + 1;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>

Shouldn't it be while(!feof(...))?
You want to stop when you reach the end of file.
Plus it looks like you've got some spelling errors:
$notretired = $notRetired + 1;
Notice how the R is lower case on the left and upper case on the right.
Also at the beginning you have $canRetire and in the if condition inside the loop you have $canRetired.
And just another little hint for you: $notRetired = $notRetired + 1; is the same as $notRetired++;

Made a couple modifications to cases, and what appears to be a type based on the variables you have defined. Looks like the variable $canRetire does not match $canretired.
Using while(feof($fh)) is basically saying, do this for the EOF. Which renders that entire loop useless. Using while(!feof($fh)) will allow you to loop until the EOF.
<?php
$canRetired= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (!feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired++;
}
else{
$notRetired = $notRetired++;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>

Related

php break not working as intended? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a for loop that loops through xml. I then fetch results from the database and look for the id.
What happens is that when an id is found and the break happens, it stops both the while loop and the parent for loop. I just want to stop the while loop. How do I get this to work as intended?
$xml = simplexml_load_file('file/path/here');
$articles = $xml->article;
$total_articles = count($articles);
// Cycle through the list of articles
for($a=0; $a<$total_articles; $a++)
{
// Check if article already exists
// MySQLi select statement goes here
$exists = false;
while($a = $article_result->fetch_assoc())
{
if($a['article_id'] == $id)
{
$exists = true;
break 1;
}
}
}
you're setting $a to $article_result->fetch_assoc().
Here:
...
while($a = $article_result->fetch_assoc())
...
[] < 1 returns false for me, at least for php7.

mysql php - check if last variable is the same [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I tried to echo out the $rinse variable, I get nothing
But I do get one for the $rang['email'], any clues would be good
I also tried doing $rinse == $rang['email'];
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] = $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}
my code updated:
echo $rinse;
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email']
This is still not working for me
You need two = in your if statement.
if ($rang['email'] == $rinse){
You should add double ==. This is comparion, a single = means set to
while($rang = mysql_fetch_assoc($results))
{
if ($rang['email'] == $rinse){
echo $rang['email'];
}
$rinse = $rang['email'];
}

How to define an Array property in a PHP class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a following simple code but it produces syntax error on line this->players[] = 'Tom':
<?php
class club {
var $clubID = 0;
var $players=array();
function __constructor($clubID = '') {
$this->clubID = $clubID;
}
function populatePlayers() {
$this->players[] = 'Tom';
}
}
$myClub = new club(1);
$myClub->populatePlayers();
var_dump($myClub->players);
?>
It should be
$this->players[] = 'Tom';
instead of
$this->$players[] = 'Tom';
You need to add $ before the this keyword and the players variable does not need one.
Demo
try this
function populatePlayers() {
$this->players[] = 'Tom';
}

Warning: Cannot use a scalar value as an array in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
I am seeing the following error when executing my status script:
Warning: Cannot use a scalar value as an array in
$result[$array[$i*2]] = $array[$i*2+1];
What am I doing wrong?
I have included the full code below:
<?php
// Set the host and port
$host = "ip_goes_here";
$port = port_goes_here;
// Open the socket connection to the server
$fp = fsockopen("udp://".$host, $port);
// Set the string to send to the server
$string = "\xff\xff\xff\xffgetinfo";
// Set the socket timeout to 2 seconds
socket_set_timeout($fp, 2);
// Actually send the string
fwrite($fp, $string);
// Read the first 18 bytes to get rid of the header and do the error checking here
if(!fread($fp, 18)) {
die("Oh God, the pain!");
}
// Get the status of the socket, to be used for the length left
$status = socket_get_status($fp);
// Read the rest
$info = fread($fp, $status['unread_bytes']);
// Explode the result of the fread into another variable
$array = explode("\\", $info);
// Loop through and create a result array, with the key being even, the result, odd
for($i = 0; $i < count($array)/2; $i++) {
$result[$array[$i*2]] = $array[$i*2+1];
}
// Print the result for error checking
print_r($result);
// Close the file pointer
fclose($fp);
The line I mentioned is causing the errors. I have no idea what I am doing wrong here...
You can try declaring the variable $result, as an array, before using it.
$result = array();
// Loop through and create a result array, with the key being even, the result, odd
for($i = 0; $i < count($array)/2; $i++) {
$result[$array[$i*2]] = $array[$i*2+1];
}

Display 10 array cards [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://plnkr.co/edit/06lu6r34eGNfRq1fygNy
I want to only display 10 "cards" from my array, how would I do so?
I tried using unset() but it is very inefficient and doesn't even work for multiple cards.
Also how can I make the code display the "cards" horizontally instead of vertically?
If you’d like to use unset, the correct syntax would be unset($cards[1]); unset($cards[2]); unset($cards[3]); ... and so on.
However, for your particular situation, I’ll recommend you to use array_slice:
$cards = array(
"Messi", "Ronaldo", "Ibrahimovic", "Ribery", "Robben", "Neymar", "Rooney", "Casillas",
"Falcao", "Van Persie", "Hazard", "Iniesta", "Xavi", "Schweinsteiger", "Silva", "Fabregas",
"Lahm", "Aguero", "Cavani", "Vidic", "Ozil", "Mata", "Bale", "ThiagoSilva",
"Kompany", "Tevez", "Toure", "Ramos", "Suarez", "Pirlo", "DiMaria", "Neuer",
"Pique", "Buffon", "Lewandowski", "Gomez", "Chiellini", "Cole", "Pedro", "Busquets",
"Cech", "Muller", "Hummels", "Alonso", "Navas", "Modric", "Cazorla", "Gotze",
"Benzema", "Vidal", "Lavezzi"
);
shuffle($cards);
$cards = array_slice($cards, 0, 10);
For the horizontal display, you may simply omit the <br> at the end of every iteration of your loop, but, depending on the resolution of the user’s browser, the images may occupy more than one line. For a strictly horizontal arrangement, use an HTML table with 10 columns:
print("<table><tr>");
foreach($cards as $card){
$img = "http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/".$card.".png";
print("<td><img src=\"".$img."\"/></td>");
}
print("</tr></table>");
The following works too:
for ($i = 0; $i < 10; $i++) {
echo $cards[$i] ." <br>"; // put the name above the card
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'> <br>";
}
If you want them horizontally, and without names, don't put in the <br>:
for ($i = 0; $i < 10; $i++) {
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'>";
}

Categories