Php Loop While Condition = True [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 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);
}

Related

Check if a string conains a specific word [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 1 year ago.
Improve this question
I need to check if an URL address contains some text. so I created this code.
function url_mapping_name( $urlname ) {
if (str_contains($urlname, 'amazon.de')) {
echo "amazon;
}
if (str_contains($urlname, 'brickset')) {
echo 'brickset';
} else {
echo 'no URL';
}
I am trying to say.
Look for "amazon.de" in $urlname, if the URL contains amazon.de return amazon, if the URL contains brickset.com return brickset, if nothing found return no URL
But something is wrong and I do know where I did a mistake
Thanks for helping me.
The line
echo 'amazon';
is missing a quotation mark. Note the color coding changes. That is usually caused by a missing quotation mark.
Assuming by "return" you mean having the function assign that value to a variable, all the output strings should be
return 'string';
instead of
echo 'string';
A potential additional issue is having 2 if statements instead of using else if. If you do intent to echo the strings instead of returning them, it should be like this so when it's equal to 'amazon' it doesn't also echo 'no url'
function url_mapping_name( $urlname ) {
if (str_contains($urlname, 'amazon')) {
echo 'amazon';
} else if (str_contains($urlname, 'brickset')) {
echo 'brickset';
} else {
echo 'no URL';
}
Thanks for the help, I used the following improvements and it works now.
I fixed the closing quote
replaced echo with return
used elseif - it returned "amazonno URL" before as #aynber wrote
function url_mapping_name( $urlname ) {
if (str_contains($urlname, 'amazon.de')) {
return 'amazon';
}
elseif (str_contains($urlname, 'brickset')) {
return 'brickset';
} else {
return 'no URL';
}
}
Thank you very much for helping me.

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.

How to convert a global script into a function? [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
Hi guys i'm having a small issue when i try and convert my exisiting script into a simple function. I'll leave my code below.
PHP:
function service_check($postcode) {
$op_postcodes = array(... "DG10","DG3","DG4" ...);
if(isset(htmlentities($postcode))) {
$postcode_checker = trim(strtoupper(htmlentities($postcode)));
$trim_postcode = trim(substr(htmlentities($postcode_checker, 0, -3)));
if(empty($postcode_checker)) {
$error = "We require your postcode to check our service in your area.";
} else if(!valid_postcode($postcode_checker)) {
$otp = "The postcode you entered is invalid.";
} else if(!in_array($trim_postcode, $op_postcodes)) {
$otp = "Sorry, but we don't provide our service's in your area, just yet.";
} else {
$otp = "Great news! We're in your area and you are eligible to order our services!";
$_SESSION['customer_postcode'] = $postcode_checker;
}
} else {
$otp = "To get started please enter your postcode.";
}
}
My current usage of the function is <?php service_check($_POST['service_check']); ?>
My error is:
Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in /home/domain.com/public_html/controller/application/functions/locale.SM.php on line 27
change this
if(isset(htmlentities($postcode))) {
to this
$pc = htmlentities($postcode);
if(isset($pc)) {
read this if you get sometime - http://php.net/manual/en/function.isset.php http://php.net/manual/en/function.htmlentities.php
Since your questions wasn't complete i assume so now an edit. The better approach could be to use !empty() instead of isset() on our if condition.
Even better, reove htmlentities method call from your if block and then later use html entities when you actually need it.

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'];
}
}

Should conditions go inside or outside of function/method [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is preferable: A function that decides itself if it should do anything, or deciding whether to call that function?
function x() {
if ($userIsLoggedIn) {
alwaysHelloUser();
}
if ($visitorRegion != 'US') {
alwaysDisplayNoInternationalShipping();
}
if ($day == 'Sunday') {
alwaysLockDownStore();
}
}
function alwaysLockDownStore() {
//close store
//offer alternative store
//show opening hours
//display form for user leaving order for next day
exit("Sorry, we are closed!");
}
or
function y() {
perhapsSayHelloUser($user);
maybeDisplayNoInternationalShipping($region);
sometimesLockDownStore($day);
}
function sometimesLockDownStore($day) {
if ($day == 'Sunday') {
//close store
//offer alternative store
//show opening hours
//display form for user leaving order for next day
exit ("Sorry, we are closed!");
}
}
The example is trivial, but imagine enabling functions using configuration or access rights. Is there an accepted standard? My gut tells me to go for y() when there is something like exit() involved, breaking the normal flow. Otherwise, encapsulation and DRY would favor x(). What do you think?
Edit I've expended the example. My question is really: If there is a condition on which a function/method should be executed, should that control take place in the function (tidy but outcome is uncertain) or outside it (calling is verbose, but expected output is always the same)?
I'd prefer to keep the main functionality in the function. Extra logic outside. Because the function will do just that what is intented. Or you would everytime run the function needed or not. Depending how complex your problem will be.
function lockStoreOnDay($day) {
// do locking
return "Sorry, we are closed on $day!";
}
echo lockStoreOnDay('Sunday'); //will print "Sorry, we are closed on Sunday!"
echo lockStoreOnDay('Saturday'); //will print "Sorry, we are closed on Saturday!"
Generally: Why not use a Class?
If you have access rights or a configuration this information could be stored in an Array or Object. So you iterate through the Array or search for your keyword and than process whatever should be processed. I. e. make a function call.
function lockStoreOnDay()
{
//do locking
return "Sorry, we are closed!";
}
$config_closeOnDay = array (
'Sunday'
);
foreach ($config_closeOnDay as $dayToCloseOn) {
echo lockStoreOnDay();
}
Maybe this helps.
You shouldn't have such constraints hardcoded inside function. You should aim for something like:
function isStoreLocked($dayToCheck, $daysLocked)
{
// You can replace "locking" logic with database or something
// There shouldn't be an raw array of days inside code
return in_array($dayToCheck, $daysLocked);
}
Then in your code:
$curentDayName = date('l');
$lockedDays = array('Saturday', 'Sunday');
if(isStoreLocked($currentDayName, $daysLocked))
{
die('Sorry, store is locked!');
}
To give context to my answer: if you do it using a framework (my choice is Symfony2) isStoreLocked should be a service or a repository method and it should be called inside controller while returning Response (rather than simple die()).

Categories