Click to See Complete Forum and Search --> : adding to arrays


awebb
07-07-2003, 01:50 AM
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>";
}

pyro
07-07-2003, 07:47 AM
Why are you doing this:

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

I would do something more like:

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

That will only loop through all the values of $select_date.

awebb
07-08-2003, 01:38 AM
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

pyro
07-08-2003, 05:58 AM
I'm assuming that this didn't work?

$booked="Booked";
if ($select_date[$i]==""){
$select_date[$i]=$booked;
}

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

awebb
07-08-2003, 09:54 AM
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

pyro
07-08-2003, 10:33 AM
You're welcome... :)