Jump to content

[SOLVED] I'm an idiot stuck in endless loop


JasonGreen

Recommended Posts

Seriously, I'm not that bright. My whole life random stragners have taken pity on me. You only have to meet me once to see I am incapable of even dressing myself. I pass off as hip, but the truth is I just forgot to wear a belt.

 

Here is my endless loop. Probably best not to run, it keeps freezing my PC

 

$breed = rtrim(strtok($pet_spa, " \n"));

$appointment = rtrim(strtok(" \n"));

$day = rtrim(strtok(" \n"));

$time = rtrim(strtok(" \n"));

$charge = rtrim(strtok(" \n"));

 

printf ("%9s\n", "Breed  App.  Day  Time  Charge");

 

while($breed){

    echo $breed, "  ";

    echo $appointment, "  ";

    echo $day, "  ";

    echo $time, "  ";

    echo "\$" . number_format ($charge,2);

}

 

I have to use While. I know there are other ways of doing this. But can't I use While?

 

 

Link to comment
Share on other sites

The variable that you are testing in your while loop is not being changed in that loop, so if you get into that loop and $breed is not false, then it never will be false, and your while loop will continue for ever.

 

I think you need to be calling strtok within your while loop, maybe somthing like this at the end of the loop

$breed = rtrim(strtok(" \n"));

then your loop will exit when all the lines or spaces in $petspa have been used up.

 

Also you might like to change while($breed) to while($breed !== false) as $breed could come back as zero, not as null, depending on what is in $petspa

 

 

 

 

Seriously, I'm not that bright. My whole life random stragners have taken pity on me. You only have to meet me once to see I am incapable of even dressing myself. I pass off as hip, but the truth is I just forgot to wear a belt.

 

Here is my endless loop. Probably best not to run, it keeps freezing my PC

 

$breed = rtrim(strtok($pet_spa, " \n"));

$appointment = rtrim(strtok(" \n"));

$day = rtrim(strtok(" \n"));

$time = rtrim(strtok(" \n"));

$charge = rtrim(strtok(" \n"));

 

printf ("%9s\n", "Breed  App.  Day  Time  Charge");

 

while($breed){

    echo $breed, "  ";

    echo $appointment, "  ";

    echo $day, "  ";

    echo $time, "  ";

    echo "\$" . number_format ($charge,2);

}

 

I have to use While. I know there are other ways of doing this. But can't I use While?

Link to comment
Share on other sites

I'm using include 'petspainfo.php' and then spitting out the info, have to use While and still need assigned variables for more data manipulation.

 

while(echo) only gives an error

 

including $breed = rtrim(strtok(" ")); repeats the same info (I took out \n because it was placing each item on a separate line

Link to comment
Share on other sites

there is not much else to it, but here it is ($petspa comes from the information within petspainfo.php)

 

 

 

 

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">

<html>

<head>

      <title>Spoiled Pets</title>

      <h5>Pet Spa Super Spa</h5>

</head>

<body>

<pre>

<?php

// File Name: PetSpaRus

// Author: me

// Purpose:etc

 

include 'petspainfo.php';

 

$breed = rtrim(strtok($pet_spa, " \n"));

$appointment = rtrim(strtok(" \n"));

$day = rtrim(strtok(" \n"));

$time = rtrim(strtok(" \n"));

$charge = rtrim(strtok(" \n"));

 

printf ("%9s\n", "Breed  App.  Day  Time  Charge");

 

while($breed){

    echo $breed, "  ";

    echo $appointment, "  ";

    echo $day, "  ";

    echo $time, "  ";

    echo "\$" . number_format ($charge,2);

}

 

 

?>

</pre>

</body>

</html>

Link to comment
Share on other sites

Thanks for posting the code.  Does $pet_spa contain all of your data?  And you want to keep calling strtok() to get more items until there's no more data?  In that case you could do this.

 

nclude 'petspainfo.php';

$breed = rtrim(strtok($pet_spa, " \n"));
$appointment = rtrim(strtok(" \n"));
$day = rtrim(strtok(" \n"));
$time = rtrim(strtok(" \n"));
$charge = rtrim(strtok(" \n"));

printf ("%9s\n", "Breed  App.   Day   Time  Charge");

while($breed){
    echo $breed, "   ";
    echo $appointment, "   ";
    echo $day, "   ";
    echo $time, "   ";
    echo "\$" . number_format ($charge,2);

    $breed = rtrim(strtok(" \n"));
    $appointment = rtrim(strtok(" \n"));
    $day = rtrim(strtok(" \n"));
    $time = rtrim(strtok(" \n"));
    $charge = rtrim(strtok(" \n"));
}

 

You might have some issues here - it depends on what the contents of $pet_spa actually is.  I'm making a few assumptions about that.

 

You can use "break" from inside the while loop and it ought to work.  It won't work outside the while loop though, as there's nothing to break out of.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.