0

I have a booking system which has a dropdown list which you click on. When you click on this dropdown list the options are as follows:

1 person 2 person 3 person 4 person .....

I would like the first option to say "1 person" but all remaining options to say "people" like follows:

1 person 2 people 3 people 4 people .....

The PHP file has the following section which controls the output of a selector:

public function getFilterPlaces () {

    $places = array();
    $bookingSystem = $this->getParam ('bookingSystem','tables');

    if ( $bookingSystem == 'tables' ) {

        $query = $this->_db->getQuery ( true );
        $query->select ('a.places AS max, a.minim AS min');
        $query->from ('#__tbb_table AS a');

        $this->_db->setQuery ( $query );
        $tables = $this->_db->loadObjectList();

        if ( $tables ) {
            foreach ( $tables as $table ) {
                for ( $i = $table->min; $i<= $table->max; $i++ ) {
                    if ( !in_array($i, $places ) ) {
                        $places[$i] = $i . ' people';
                    }
                }
            }
        }

    } elseif ( $bookingSystem == 'places' ) {
        $max = $this->getParam ('restaurantPlaces', 0);
        if ( $max ) {
            for ( $i = 1; $i<= $max; $i++ ) {
                $places[$i] = $i . ' ' .     JText::_('COM_TABLEBOOKING_FILTER_PLACES');
            }
        }
    }

    return $places;
} 

How would I code the array to populate the first entry with "1 person" and the following with 2 people, 3 people etc.?

Thanks,

4

2 回答 2

1

Apply if else condition as the following code tells:

$places = array();
$bookingSystem = $this->getParam ('bookingSystem','tables');

if ( $bookingSystem == 'tables' ) {

    $query = $this->_db->getQuery ( true );
    $query->select ('a.places AS max, a.minim AS min');
    $query->from ('#__tbb_table AS a');

    $this->_db->setQuery ( $query );
    $tables = $this->_db->loadObjectList();

    if ( $tables ) {
        foreach ( $tables as $table ) {
            for ( $i = $table->min; $i<= $table->max; $i++ ) {
                if ( !in_array($i, $places ) ) {
                    if ($i==1) {
                                                $places[$i] = $i . ' person'; // APPLY `IF ELSE ` CONDITION HERE.
                                            }
                                            else {
                                                $places[$i] = $i . ' people';
                                            }
                }
            }
        }
    }

} elseif ( $bookingSystem == 'places' ) {
    $max = $this->getParam ('restaurantPlaces', 0);
    if ( $max ) {
        for ( $i = 1; $i<= $max; $i++ ) {
            $places[$i] = $i . ' ' .     JText::_('COM_TABLEBOOKING_FILTER_PLACES');
        }
    }
}

return $places;

}

于 2013-10-01T16:50:01.973 回答
0

Solution! I had to add the if-else logic to the second part as well!!! Thank you!!!!! You rock!

$places = array();
$bookingSystem = $this->getParam ('bookingSystem','tables');

if ( $bookingSystem == 'tables' ) {

$query = $this->_db->getQuery ( true );
$query->select ('a.places AS max, a.minim AS min');
$query->from ('#__tbb_table AS a');

$this->_db->setQuery ( $query );
$tables = $this->_db->loadObjectList();

if ( $tables ) {
    foreach ( $tables as $table ) {
        for ( $i = $table->min; $i<= $table->max; $i++ ) {
            if ( !in_array($i, $places ) ) {
                if ($i==1) {
                                            $places[$i] = $i . ' person'; // APPLY `IF ELSE ` CONDITION HERE.
                                        }
                                        else {
                                            $places[$i] = $i . ' people';
                                        }
            }
        }
    }
 }

} elseif ( $bookingSystem == 'places' ) {
$max = $this->getParam ('restaurantPlaces', 0);
if ( $max ) {
    for ( $i = 1; $i<= $max; $i++ ) {
       if ($i==1) {
                                            $places[$i] = $i . ' person'; // APPLY `IF ELSE ` CONDITION HERE.
                                        }
                                        else {
                                            $places[$i] = $i . ' people';
                                        }
            }

    }
}


 return $places;

}
于 2013-10-01T18:12:34.027 回答