www.webdeveloper.com
Results 1 to 6 of 6

Thread: adding to arrays

Hybrid View

  1. #1
    Join Date
    Jun 2003
    Location
    The Alps
    Posts
    36

    adding to arrays

    I have a <select> with the <option> created by sorting through some arrays. this works fine but leaves gaps in the <select> which look a bit strange when they get big.
    The <option> is a date, used in a booking process.
    Is there a way of filling in these gaps with a single word like "Booked"?
    I have read about array_push(), but this only seems to add to the end of an array and what I seem to need is something to fill in the gaps. Maybe something like:

    $booked="Booked";
    if ($select_date[$i]==""){
    $select_date[$i]=$booked;
    }
    or even an array that contains the word "Booked" as many times as needed (never more than 100).

    I can't quite figure out how to get this to work or where to put the code.

    Any suggestions gratefully recieved.

    Here is how I am sorting the arrays (it might be ugly but it works):

    $existing1[]=array($existing1);
    $existing2[]=array($existing2);
    $existing3[]=array($existing3);
    $existing4[]=array($existing4);
    $existing=array_merge($existing1, $existing2, $existing3, $existing4);/*creates the definitive existing array*/
    $existing=array_unique($existing);/* removes duplicates */
    $option2_date[]=$option_date;
    $select_date=array_diff($option2_date,$existing);/* subtracts the existing dates from the selected dates */
    $select_date=array_unique($select_date);/* removes any posible duplicates */

    for ($i=0;$i<=100;$i++)
    {
    echo"<option value=\"$select_date[$i]\">$select_date[$i]<br /></option>";
    }

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Why are you doing this:

    PHP Code:
    for ($i=0;$i<=100;$i++) 
    I would do something more like:

    PHP Code:
    for ($i=0;$i<count($select_date);$i++) 
    That will only loop through all the values of $select_date.

  3. #3
    Join Date
    Jun 2003
    Location
    The Alps
    Posts
    36
    Ok, I was using the:

    for ($i=0;$i<=100;$i++)

    because I am generating 100 <option>s in the <select>, but your solution:

    for ($i=0;$i<count($select_date);$i++)

    is prettier. Thank you, Pyro.

    Trouble is, I have still got the gaps in the <select>.
    The <option>s are a list of dates (the difference between one that are in the DB, ie booked, and the ones that are not) and for the sake of readability and useability I want to keep the gaps.
    The gaps are currently blank, which is acceptable but not perfect.
    I don't want to remove the gaps because I think that users would expect to find a four "week" gap between January and March (if all of the weeks (4) in February are booked.
    Really I just want to fill in those gaps with the word "Booked" and wondered if you could do it by creating another array(), maybe:

    $booked=array('Booked', + 'Booked', another 99 times);

    and slotting it in the gaps in $selected_date.

    or maybe using array_splice() or in_array() somehow.

    Any input gratefully recieved.

    Andy
    Last edited by awebb; 07-08-2003 at 02:45 AM.

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    I'm assuming that this didn't work?

    PHP Code:
    $booked="Booked"
    if (
    $select_date[$i]==""){ 
        
    $select_date[$i]=$booked

    Note that it would have to go inside your for loop...

  5. #5
    Join Date
    Jun 2003
    Location
    The Alps
    Posts
    36
    Er...yes, actually it does work.
    I had it in the wrong place ie before the for loop, trying to fill the flipping holes before it outputted.
    Many thanks for your help.
    Andy

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    You're welcome...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles