+ Reply to Thread
Results 1 to 5 of 5

Thread: PHP: Problem with For Loops and If Statement

  1. #1
    PharaohInc's Avatar
    PharaohInc is offline x10Hosting Member PharaohInc is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    In Da 242/876/301/305
    Posts
    8

    Question PHP: Problem with For Loops and If Statement

    Ok,I'm having a problem. I have an array of arrays, the arrays inside the arrays are in that format shown below. What I want to do is take the shortest distance between the points but if they are in the same city the loop would discard them and move it to the next one that has the shortest distance to the point but not in the same city.

    This is the keys of each of the normal arrays
    $venue['city']
    $venue['country']
    $venue['xcoord']
    $venue['ycoord']

    The loops looks like it does below and there is a function that can calculate the distance between points already. The variable $startVenue has the same format as the normal arrays as well


    PHP Code:
    for($r=0;$r 5$r++)
    {
    $startVenue// starting location or the first array
    $venue1 $startVenue;
    $tour;
    $venues//contains the arrays with all the information of the venues
    $shortestDistance 999999999;
    $index;


    for(
    $i=0;$i<sizeof($venues);$i++)
    {
        
    $dist distance($venue1,$venues[$i]);
        

        if(
    dist<$shortestDistance)
             {
          if(
    $venue1['city']!=$venues[$i]['city'])
          {
             
    $shortestDistance dist;
             
    $index $i;
          }
             }

    }
         
    array_push($tour,$venue1);  
         
    $venue1 $venues[$index];
         unset(
    $venues[$index]);
         
    array_values($venues);

    The thing is if i take out the if statement with the city it works, giving points that are close but with the if statement for the city it gives me nothing, i have tried strcmp and it still does not work. I'm at wits end at what to do.
    Show me a man that has never made mistakes and I will show you a man that has never tried.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP: Problem with For Loops and If Statement

    The code you posted is all kinds of wrong, so I'm guessing it's heavily edited from your real code. It should also be properly indented to make it readable.
    Quote Originally Posted by PharaohInc View Post
    PHP Code:
    for($r=0;$r 5$r++) 
    Why 5?

    Quote Originally Posted by PharaohInc View Post
    PHP Code:
    {
        
    $startVenue// starting location or the first array
        
    $venue1 $startVenue;
        
    $tour;
        
    $venues//contains the arrays with all the information of the venues 
    Were these just to show us some of the variables you're using? If not, they all hold null values. The $venue1 = $startVenue; should be outside the loop.

    Quote Originally Posted by PharaohInc View Post
    PHP Code:
        $shortestDistance 999999999;
        
    $index;

        for(
    $i=0;$i<sizeof($venues);$i++) { 
    A foreach loop is easier and more readable here:

    PHP Code:
        foreach ($venues as $i => $venue) {
            
    $dist distance($venue1,$venue);
            ... 
    It's also more correct, since you're removing items from $venues but not reindexing. With a for loop, you'll need to check that an item exists for a given index.

    Quote Originally Posted by PharaohInc View Post
    PHP Code:
            if(dist<$shortestDistance)
            {
                if(
    $venue1['city']!=$venues[$i]['city'])
                {
                     
    $shortestDistance dist
    dist here is a bareword, which will be converted to a string, unless there's a constant with the same name. Either is wrong; you want $dist to get a variable.

    Here's a cleaned up version of your sample code:
    PHP Code:
    $venues = array(
        array(
    'city' => ...),
        array(
    'city' => ...),
        ...
    );
    $fromVenue $startingVenue;
    $tour = array();

    // this will repeat while $venues has at least one element
    while ($venues) {
        
    $minDistance PHP_MAX_INT;
        foreach (
    $venues as $i => $venue) {
            
    $dist dist($fromVenue$venue);
            if (
    $dist $minDistance
                
    && $fromVenue['city'] != $venue['city']) 
            {
                
    $minDistance $dist;
                
    $index $i;
            }
        }
        
    // when pushing a single element, empty index syntax is preferred over array_push
        
    $tour[] = $fromVenue $venues[$index];
        unset(
    $venues[$index]);

    Are you trying to find the shortest path that visits the venues? This is basically the traveling salesman problem (except that for the TSP you need to find a circuit rather than a path, which doesn't affect the complexity), and the only known way of solving it in general is to generate and check every possible path.
    Last edited by misson; 05-16-2010 at 02:40 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    PharaohInc's Avatar
    PharaohInc is offline x10Hosting Member PharaohInc is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    In Da 242/876/301/305
    Posts
    8

    Re: PHP: Problem with For Loops and If Statement

    I tried your suggestion but I get an error in return now

    Parse Error: Unexpected '{'

    This normally occurs when you have mismatched brackets, i doubel checked with my code and i dont see where the problem is. I then took the function out and tested it by itself once again and I got that same error.
    Show me a man that has never made mistakes and I will show you a man that has never tried.

  4. #4
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: PHP: Problem with For Loops and If Statement

    Quote Originally Posted by PharaohInc View Post
    I tried your suggestion but I get an error in return now

    Parse Error: Unexpected '{'

    .
    The error message usually includes the line number, which would help. Just work back from there and find the missing ( or ) or ;

    Especially if you would post the new code. From the top of the script. Cutting a snippit out of the middle does not help. Trying to guess how the new code looks is futile.
    Last edited by dlukin; 05-16-2010 at 10:57 AM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP: Problem with For Loops and If Statement

    In addition to what dlukin says, that error message is a good example of why it's important to properly indent your code, preferably with an editor that auto-indents. It becomes very easy to see which braces match, and thus where you're missing a brace.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP MYSQL WHERE statement help
    By Chris S in forum Programming Help
    Replies: 3
    Last Post: 10-23-2008, 02:13 PM
  2. A great place to find video loops for movie production
    By w4k3upn0w in forum Graphics & Webdesign
    Replies: 3
    Last Post: 10-22-2008, 07:48 AM
  3. MySQL and if statement...
    By chappill in forum Programming Help
    Replies: 1
    Last Post: 08-28-2008, 01:18 PM
  4. if statement question
    By votter in forum Programming Help
    Replies: 4
    Last Post: 08-25-2008, 05:24 PM
  5. Loops
    By taekwondokid42 in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 11-22-2007, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers