php break not working as intended? [closed] - php

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.

Related

Getting JSON data from Rest API [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 am using a Rest API to get data from a website and I would like to use that data in my webshop. I have never worked with API's before.
I now have the following code:
<?php
$url = 'https://api.floraathome.nl/v1/products/get?apitoken=[MY_API_TOKEN]&type=json';
$json = file_get_contents($url);
$retVal = json_decode($json, TRUE);
for ($x = 0; $x < count($retVal); $x++) {
echo $retVal['data'][$x]['dutchname']."<br>";
echo $retVal['data'][$x]['purchaseprice']."<br>";
echo $retVal['data'][$x]['promotionaltext']."<br><br>";
}
My problem is that it only shows the first 2 products, but I have 9 products selected. If I print $retVal, it does output all 9 products.
That's why i always prefer foreach(), do like below:-
foreach($retVal['data'] as $retV){
echo $retV['dutchname']."<br>";
echo $retV['purchaseprice']."<br>";
echo $retV['promotionaltext']."<br><br>";
}
Note:- your code will also work if you change count($retVal) to count($retVal['data'])
My problem is that it only shows the first 2 products, but I have 9
products selected. If I print $retVal, it does output all 9 products.
Thats because you are looping over the wrong count of your elements:
for ($x = 0; $x < count($retVal); $x++)
^^^^^^^^^^^^^^
as you can see, the problem is in here count($retVal) which I assume that the structure is like something like that:
Array (
[data] => Array ()
[xxxx] => 'xx'
)
so to solve this, -and if you want to go with for loop rather than the other right solution using foreach- you will need to get the prober count count($retVal['data']):
for ($x = 0; $x < count($retVal['data']); $x++) {
echo $retVal[$x]['dutchname']."<br>";
echo $retVal[$x]['purchaseprice']."<br>";
echo $retVal[$x]['promotionaltext']."<br><br>";
}

Php Loop While Condition = True [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 7 years ago.
Improve this question
I have a php file which basically reads a page using JSON and a variable is returned to it (pending/paid/expired). now my simple requirement is if the returned variable is paid/expired the respective actions for them should be taken and the loop should not be repeated but if the response is pending the code should recheck after 10 seconds. A similar cycle then happens again and so on till the loop is stopped(by receiving either a expired/paid response.
here is the code i prepared for it using the examples i found at various places but this doesnt seem to work correctly.. pls guide..
while($end==0) {
$json = file_get_contents('http://old.kbourbaki.com/xlisteningpageprestashop.aspx?yyyyyyy');
$obj = json_decode($json);
$result=$obj->response;
echo($result);
if ($result=='paid') {
p('Order Successfull');
$end=1;
} elseif ($result=='expired' ) {
p('status = expired');
$end=1;//ex
} else {
d('status = Pending');
sleep(10);
}
}
CODE UPDATED extra '{' removed
You have one too many curly braces.
The proper elseif and else block should look like this
} elseif ($result=='expired' ) {
p('status = expired');
$end=1;//ex
} else {
It would help you a lot if you properly formatted your code.
You close the IF condition before the last ELSE just delete the closing clasp.
if($result=='paid'){
p('Order Successfull');
$end=1;
}else if($result=='expired'){
p('status = expired');
$end=1;//ex
}else{
d('status = Pending');
sleep(10);
}

Php string If contain echo that string [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 7 years ago.
Improve this question
My Php mysql out put is array i need if string contain particular character i need print that string only
while($row2 = mysqli_fetch_array($result2)) {
echo $row2['link'];
}
Output is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
i need like this
if (strpos($a,'videoweed') !== false) {
echo $row2['link'];
}
it show out put is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
I need output only;
http://www.videoweed.es/file/b38300afda3e6
You have answer in your code. Just need to assemble it. check this:-
while($row2 = mysqli_fetch_array($result2)) {
if (strpos($row2['link'],'videoweed') !== false) {
echo $row2['link'];
}
}

Loop not counting [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 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>");
?>

PHP objects with count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can i do something like this?
class record
{
public $count;
}
$i = 0;
foreach($entry as $item) {
$i++;
$record$i = new record();
$record$i->count = $item['count'];
print $page$i;
}
Specifically, this part here is giving me errors.
$record$i = new record();
Note: the loop works fine if i just do print $item['count']
The error is: Parse error: syntax error, unexpected T_VARIABLE
see http://3v4l.org/lB4sR
${'record'.$i};
you can create a string that holds a varname and use it as a variable later - see the example
**Edit: but like #h2ooooooo sais in the comment to your question: use an array.
Hope is that what you asking, but I do not understand why to use such bad methods to count...
class record
{
static $countq = 0;
public function count(){
self::$countq++;
}
}
$entry = array(1, 4, 6, 7);
$obj = new record();
foreach($entry as $item) {
$obj->count();
}
echo record::$countq;

Categories