Categories > TinyButStrong general >

tricky question

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: michael
Date: 2004-09-01
Time: 12:18

tricky question

Hi

Sorry for the long question - this is tricky .. I am setting up a page to capture shop open & closing days/times. Basically I have a block that displays text inputs and checkboxes for each day. The days are stored in a small table ("days") like

dayID day
------------
1        Sun
2        Mon
3        ...
     ..
7        sat
8        Public holiday

in this way the number of input rows (one for each day of the week plus special days) can be controlled by this table, and I have HTML like so,


                    <tr>
                        <td>[days.day;block=tr]</td>
                        <td>
                            <div align="center">
                                <input type="checkbox" name="tradingdays[[days.#]]" value="1"></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="text" name="opentime[[days.#]]" size="10"></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="text" name="closetime[[days.#]]" size="10"></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="checkbox" name="open24hours[[days.#]]" value="1"></div>
                        </td>
                    </tr>

as you may see, I am using the array style to label the input fields so I can access the posted data via array methods in PHP. I use the 'trading days' checkbox to identify which fields (rows representing each day) had data entered. The shop open/close time data is stored in a second table ("openinghours"). This so far works ok.

So now the question.. how can I convert this HTML to allow editing the data (i.e. loading previously entered data into this form based on this HTML, and editing & updating it).

It is common that shops are not open all of the 7 or so days offered by "days" table. This is significant since I still want to display all possible rows despite perhaps only one or two open/close times were entered. If I do a block merge against the previously entered data, I may get only 2 rows of entry fields instead of the max available as dictated by the "days table" (indicated earlier).

This is one idea I have:

                    <tr>
                        <td>[days.day;block=tr;onsection=populateHours]</td>
                        <td>
                            <div align="center">
                                <input type="checkbox" name="tradingdays[[days.#]]" value="1" [days.dayID]></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="text" name="opentime[[days.#]]" size="10" value="[days.opentime]"></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="text" name="closetime[[days.#]]" size="10" value="[days.closetime]"></div>
                        </td>
                        <td>
                            <div align="center">
                                <input type="checkbox" name="open24hours[[days.#]]" value="1" [days.open24hours]></div>
                        </td>
                    </tr>

and I used a onsection function

function populateHours($BlockName,&$CurrRec,&$DetailSrc,$RecNum) {
    $dayID = trim( $CurrRec['dayID'] );

    global $db, $locationID;

    $result = $db->get_row("SELECT opentime, closetime, open24hours FROM openinghours WHERE locationID = $locationID AND dayID = $dayID");

//    echo $RecNum . ': ' . $CurrRec['dayID'] . ' open = ' . $result->opentime . '<br>';

    $CurrRec['opentime'] =  $result->opentime;
    $CurrRec['closetime'] =  $result->closetime;

    if ($result->open24hours == 1)
        $CurrRec['open24hours'] = 'checked';
    else
        $CurrRec['open24hours'] = '';

    // klugdgy way to enable the trading day checkbox

    if ($dayID && $result->opentime)
        $CurrRec['dayID'] = 'checked';
    else
        $CurrRec['dayID'] = '';

}

I think this is on the right track - but I feel I am missing an easier way. Can you suggest a better approach?

I hope you can see what I am tying to do.

Thanks for any help.

Michael.
By: Skrol29
Date: 2004-09-03
Time: 10:10

Re: tricky question

Why don't you merge the 'day' block using a LEFT JOIN query?

By: michael
Date: 2004-09-03
Time: 10:35

Re: tricky question

Hi Skrol29

Thanks for your replies & suggestion. I am embarrassed to say I tried to create a left join SQL statement, and could not get it to work.. so I did it this way. I might give it another go at a later time.

Best regards

Michael.