Jump to content

wildteen88

Staff Alumni
  • Posts

    10,480
  • Joined

  • Last visited

    Never

Posts posted by wildteen88

  1. On line 67 in form_proceessed2011-NEW.php there is some form of output therefore header() will not work. The solution would be to modify your script so there is no output before the use of the header() function. Or for a simple fix revert to using output buffering.

  2. The only way I got it to work is to use type casting. Change the foreach loop to

    foreach($XML->xml->establishments->land as $value)
    {
        $key    = (int) $value->id;
        $cost   = (int) $value->cost;
        $income = (int) str_replace(array('$', ','), '', (string) $value->income);
    
        $properties[ $key ]['cost']   = $cost;
        $properties[ $key ]['income'] = $income;
    
        $ratios[ $key ] = $income / $cost;
    }

  3. The only places you use explode on are lines 58 and 69

    58.	  $temp = explode(' ', $this->numbers[$j]);
    59.	  for ($a =0; $a <$temp.length; $j++);
    ...
    69.	  $temp1 = explode(' ', $this->numbers[$j]);
    70.	  for ($a =0; $a <$temp1.length; $j++);

    On lines 58 and 69 you use explode to get the individual numbers from $this->numbers[$j]. But you are not doing anything with them afterwards (lines 59 and 70). The for loop is not constructed incorrectly. Variables do not have properties such as .length.

  4. only problem is your code is quite different from mine, and at present because I am learning I don’t want to simply cut and paste someone else’s code

    That fine. I just through you may have forgotten about your previous post as you didn't reply to it last time.

     

    I want them placed in an array in that format, not splitting them up into single integers?

    If you want to add a string of numbers, eg

    $numbers = '1, 3, 18, 27';

    And you want to add them into an array you can simply do

    $array[] = $numbers;

    That will add the string of numbers to the array.

  5. Rather than assign each properties values to seperate variables ($id10_code $id10_value $id10_id etc). You'll want to assign them to an array instead within your foreach loop

            $key = $value->id;
            $properties[ $key ]['name']   = $value->name;
            $properties[ $key ]['cost']   = $value->cost;
            $properties[ $key ]['income'] = $value->income;

     

    After calculate the ratio and add it to a separate array for storing the ratios. When adding the ratio to the array assign its key as the property id eg

    $ratios[ $key ] = $value->income / $value->cost;

     

    Now after the foreach loop you then use max to get the highest ratio from the $ratios array.

    $highestRatio = max($ratios); // get the highest ratio

     

    Next use array_search to get property details for the highest ratio.

    $propertyKey = array_search($highestRatio, $ratios); // get the property key
    $property = $properties[ $propertyKey ]; // get the property with the highest ratio

     

    $property will be associative array of the properties details (name, cost, income).

     

    Complete code

    $XML = simplexml_load_string("property_list.xml");
    
    $ratios = array();
    $properties = array();
    
    foreach($XML->xml as $value)
    {
            $key = $value->id;
            $properties[ $key ]['name']   = $value->name;
            $properties[ $key ]['cost']   = $value->cost;
            $properties[ $key ]['income'] = $value->income;
    
            $ratios[ $key ] = $value->income / $value->cost;
    }
    
    $highestRatio = max($ratios); // get the highest ratio
    $propertyKey = array_search($highestRatio, $ratios); // get the properties ID
    $property = $properties[ $propertyKey ]; // get the property with the highest ratio
    
    echo $property['name'] . ' has the best ratio of ' . $highestRatio;

×
×
  • 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.