Page 1 of 1

How to get userid and query database table?

Posted: Mon Nov 12, 2007 5:51 pm
by SineMacula
I'm using Chronoforms with Joomla! 1.5 to allow users to submit some data for an assessment.

I've also created a php script that will "score" the submission and display a customized report based on the scoring.

The problem is, I haven't figured out how to get Chronoforms to add the submitted data to a database table AND post the data to the php script to do the scoring and report display.

I currently have it set up so that Chronoforms just goes directly to the php script - so the input is scored and the report displayed (works great), but the data isn't saved anywhere.

So, I'm thinking the easiest thing would be to set Chronoforms to insert the data into a database table, and then revise my script to pull the data from the database rather than from the POST action.

But, I don't know how to create a script that will access get the userid, and use that to query the database table.

I'm thinking what I probably need is to set it up so that users will get a list of all submissions associated with their userid, then can click on the one they want to have the data pulled from the database, scored, and report displayed.

Any hints on where to begin?

Re: How to get userid and query database table?

Posted: Mon Nov 12, 2007 10:08 pm
by SineMacula
OK, figured that part out.

Now my problem is that if a user submits more than once, it's always the first one that gets displayed.

So, I'm trying to create a submission list that will link to full reports. No luck yet.

See: http://forum.joomla.org/index.php/topic,232848.0.html

Re: How to get userid and query database table?

Posted: Mon Nov 12, 2007 10:15 pm
by SineMacula
Nope, I was wrong...

I've now got it so that the username is included in the submission to Chronoforms, and the script that Chronoforms redirects to after submission queries the database based on username... so what it's doing is displaying ALL submissions for the logged in user, in record order (which will be reverse chronological order).

So, I suppose I need to figure out how to add a cookie to the mix -- add it to the database record and use it as another query term upon redirect.

Sheesh, another thing to learn.

Re: How to get userid and query database table?

Posted: Tue Nov 13, 2007 1:32 pm
by CirTap
Hi,

thanks for pointing me to this component :) Guess I'll give it a try myself.

Now for your problem: I assume you only want to show the last submission made?
To do so, once you added the new record to the database, simply save its record-id to the user session. The target page should then test whether there is a "last submission id" entry in the active user session and add it to the WHERE clause.

Instead of adding the username to the form data, you should rely on the user information J! already provides. It's also in the session, so just get *the* current user, and you're save. The username inside the form can be easily faked, and you'd end up with wrong assignments, or even risk to present sensitive data to unauthorized "users".

Have fun,
CirTap

Re: How to get userid and query database table?

Posted: Wed Nov 14, 2007 5:00 am
by SineMacula
Thanks CirTap,

I think I'm part way there...

What I now have is the username for the currently logged in user automatically added to the database with the submission data. What I don't know how to do is pass the record-id to the user session and then back to the WHERE query.

For now I've got a reasonable workaround - instead of going directly to the report for the most recent submission, the user is taken to a list of all of his/her submissions with the most recent listed first. Then, clicking on any of the listed submissions displays the report for that particular submission.

Re: How to get userid and query database table?

Posted: Fri Nov 23, 2007 12:25 pm
by RedGerry
I've used chronoforms with a modified sql insert statement to run one of my own scripts. I've found it extremely useful.

I don't know if this suggestion is any use but why not include a timestamp field into your report record. Any query to retrieve can use the timestamp to sort and avoids having to store the record id in a session.

Re: How to get userid and query database table?

Posted: Tue Nov 27, 2007 6:07 pm
by CirTap
RedGerry wrote:I don't know if this suggestion is any use but why not include a timestamp field into your report record. Any query to retrieve can use the timestamp to sort and avoids having to store the record id in a session.
works only if there's only one person EVER editing data.
No way to separate the reports from different users just by using a timestamp (uness I'm missing something here )
No matter how: a user-id is required and there's one sitting ready to be use in the session (for authenticated users).

CirTap

Re: How to get userid and query database table?

Posted: Tue Nov 27, 2007 6:18 pm
by SineMacula
It occurs to me that there are two issues here:

1. how to display a report based on the just submitted data
2. how to associate a submission with a particular user

I've included the user id pulled from the session in the submitted data - so registered users can access a list of all of their submissions.

What I don't have is a way to display the report based on ONLY the just submitted data (i.e., display a report immediately upon submission). As I currently have it, using the user id as the query search term it displays reports for ALL submissions by that user.

I think adding a timestamp as well, and using that for the display upon submission would solve that issue. Then I would also still have the ability to present registered users with a list of all of their submisssions which would link to the individual submission report displays.

So, here's where my lack of know-how comes in -- what do I need to add to my submission form to add the timestamp to the session? I know how to add it to the form and therefore the submitted data, but not how to get it into, and back out of, the session.

Also, since chronoforms does include a submission data field automatically, is there a way to just use that?

Re: How to get userid and query database table?

Posted: Wed Nov 28, 2007 9:36 am
by CirTap
SineMacula wrote:So, here's where my lack of know-how comes in -- what do I need to add to my submission form to add the timestamp to the session? I know how to add it to the form and therefore the submitted data, but not how to get it into, and back out of, the session.

If you modify the chronoforms_x table to contain a native MySQL TIMESTAMP it will be populated by MySQL; this is an automatic behaviour. The query MUST not contain a value for the timestamp field in order to work.
However, you don't necessarily need that "magic" ...
SineMacula wrote:..., since chronoforms does include a submission data field automatically, is there a way to just use that?

If it's there, you can use it :)
Modify the query in your "custom code" to contain ORDER BY `recordtime` DESC and a LIMIT 1 to get the last entry made.
Also try using $database->insertid() to retrieve the cf_id of the most recently inserted record. This call must happen immediately after the data was written to the table, otherwise it's worthless.

I'm still due to take a closer look to the component's features and behaviour, so I'm not very familiar with the internals and if the "custom code" one may add is capable to access and manipulate the raw data as needed.

Have fun,
CirTap

Re: How to get userid and query database table?

Posted: Wed Nov 28, 2007 4:12 pm
by SineMacula
Thanks, CirTap -- I hadn't thought of adding the ORDER BY and LIMIT elements to the query... that does the trick perfectly.

I haven't tried the $database->insertid() yet -- perhaps I'll play around with that when I get a chance.

Thanks!