Template Engine Problem

I need some help. I am trying to modify a website which is based on template engines and I am not sure how to do the following.

I have added a drop down box to the header.tmp and the form action posts the user selection (user_loc) to another form (loc.php). Loc.php updates the database with the user selection and then queries the same database to 'look up' the current value of user_loc so that it can be inserted as the default selection in the listbox for the current session and all subsequent sessions. So in effect, whenever the user makes a selection it is written to the database and that selection becomes the users default setting forever unless he changes it.

The problem is that I do not know how to get the looked up value of user_loc back into the header.tmp so it can be set as that users default selection. If I tell loc.php to process the header.tmp it does not have all the required variables because it was only designed to parse the user_loc variable.

EJ, I take it you know how to cause a particular option in a < SELECT > menu to be selected?

HTML
< option value="xxx" selected >

XHTML
< option value="xxx" selected="selected" >MyText< /option >

You need to build your select menu dynamically, inserting selected into the appropriate option. Do this by whatever means available within the language/templating system you are using (typically a combination of string handling and variable/array setting).

You will note that I have commented out the penultimate line because its causing me problems. I do not really want to process the whole header because loc.php does not have all the required variables. But I need the dropdown to remember the users selected location (without resorting to cookies).

Note protection against malicious code injection by use of htmlspecialchars(), and handling the case where $_POST['user_loc'] is not set (it is tested for null below).
You might also like to do some range checking here. You are expecting a single uppercase character, so make sure it is.

2.)

$update_sql_loc = $db->query(.....);

You are currently doing this unconditionally, therfore when the page is called with $_POST['user_loc'] not set (ie. when an update has not been demanded), then the database will be updated with an invalid value.
Wrap in if($user_loc != null){ }

Also, you need to handle the case where the db update fails, eg.

if(!$update_sql_loc = $db->query(.....)){ die(.....); }

3.)

$loc_details = $db->......

You only need to do this if the db has not just been updated (line above), otherwise you already know the value you are looking up ie. $selected_loc == users.user_loc if the db has just been updated.
Wrap in else{ }

Within this else block:-
After the db query, set $user_loc to $user_details['user_loc']. Hence, $user_loc is either the new value just written to the db or the previous value retrieved from the db. Either way, it's the value yo want.
Also, would $user_details be a better variable name than $loc_detals? After all, it's a whole array of user data, not just the loc.
Again, you need to handle the case where the db query fails, eg.

if(!$user_details = $db->......){ die(.....); }

4.)

$template->set('user_loc', $loc_details);

I confess I don't know this particular templating system but would guess that this line is incompatible with the code in the template file. Suggest:

My amendments to loc.php are set out below but I am getting the following error message (repeated several times) : No such file or directory, failed to open stream at line 47 in class_template when I select an option from the dropdown and the dropdown is not remembering the user selection.

Any suggestions would be most welcome. On the subject of which, is there a problem trying to process the whole of a header.tmp with a php page (loc.php) when loc.php was only designed to work with one section of it (namely, my drop down)?

And I am confused by what code collects the user_loc variable from the database when the user starts a session? The drop down is coded to work 'onchange' so how does the page know which variable the user selected as his default in a previous session if the dropdown is not amended i.e. so there is no 'onchange' event to post to loc.php?

As I understand it, your script needs to work on many different pages, so it will eventually be moved to a separate .php file and brought into each page as necessary using include() or require().

(By making the script an include, you will avoid the need for a dedicated .php page to do the updating, and also avoid the necessity to do that double-shuffle with window.setTimeout(). By making your script an include it becomes available for inclusion on as many pages as necessary and will work on all of them.)

However, for testing it is easier to have the code directly on a test page; lets call it test.php .

There's an implicit assumption in the way your original php is written, that the same script should handle update and retrieval of user_loc (to/from the db), and the composition of a corresponding < select > menu which displays to each user their current user_loc setting and also allows them to change it.

Consequently, your script must either update OR retrieve user_loc, AND must always build the menu.

The script is instructed whether to update or retrieve by the presence/absence of $_POST['user_loc' ] . Hence the if(){}else{} structure at the top of the script.

OK.

The code that retrieves the required value from the database is your line:

$user_loc = $db->get_sql_row("SELECT ........

However, as currently written, it is probably not correct. On the assumption that $db->get_sql_row returns an array (I use a db class with a different set of commands), try replacing with the following two lines:

Now for the template. I've looked at a Brian Lozier article on templates but think the class he describes is different from the one you pasted above. I cannot therefore really check your $template code but from the error message you report, I must assume that either $template in instantiated incorrectly (wrong path passed to the constructor) and/or the file 'header.tpl.php' does not exist. Check both.

What I think you need now is a strategy for testing the various elements of your code without others interfering, for example:

Ensure that the form method (in the template) points back to your test page (test.php).

Suppress all the $_POST and database stuff in your script and get your template working. It is central. Hard code $user_loc and see if it is correctly reflected in the rendered template.

Reintroduce the $_POST code and ensure that 'user_loc' is being passed in correctly and that $user_loc is set accordingly. Use 'View Source' in your browser to inspect the generated HTML.

Finally reintroduce the $db code and ensure that that (a) the selected value of user_loc is being saved and (b) that the stored value gets correctly retrieved. Inspect the database directly using eg. phpMyAdmin to see the value that is actually stored and to change it manually if necessary.

This is a pretty typical development strategy for a web page with client side scripting. Most web developers will have done similar many times over.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

I'm working in Excel to extract content of a table from a web page and populate Excel sheet cells with it. I'm trying to extract data from a table that gets loaded to a web page after show results method gets called. I have an error that says "Run-time error ...