This topic is locked

Getting labels from field values in another table

11/13/2008 12:56:57 PM
PHPRunner General questions
T
thesofa author

Hi

I have a survey to set up, with 39 questions. The survey will be repeated at regular intervals but the questions will change. The style of answers will stay constant, some are radio buttons, some are tick boxes.

To save rebuilding the project each time the questions change, I have labelled the fields in the tables for the answers, q01, q02, q03, q04 .........

The text of the question, e.g. "Do you find the teaching style suits your way of learning?" is held in a field called `q` in another table called `v_q1`, indexed on an auto increment field of `q_id`.

I would like to be able to use the text from the field `q` as the question on the Add page for each record.

I have read the post about using Session Variables as field labels and I understand that I could load the questions into an array as a session varible, or at least I think i can??

is there a way i can reference the record from the `v_q1` table and use "echo" to put it onto the page instead of having the field title on the Add or Edit form?
So in the field label space in visual editor, I will have

echo `v_q1`.`q`

but it references the field index 1 for question 1 and field index 2 for question 2 etc?

Do i need something along the lines of

echo `v_q1`.`q` where `v_q1`.`q_id`=`q01`

??
Later on
While the board was down, I have played around and used this

global $conn;

$str1 = "select * from `v_q1` where `q_id`='1'";

$rs1 = db_query($str1,$conn);

$data1 = db_fetch_array($rs1);

$question=$data1["q"];

echo $question;



and it has worked for one field, I have 39 fields to do, this means that for each page of questions I will have 6 lookups going on at a time, is there a way to load all 6 records at once to speed up loading times and reduce the hard disc thrashing on the server?

Is there a way I can get the numbers from the label, and link the number to the record number in the questions table and just have some neater code?

J
Jane 11/14/2008

Hi,
you can select all labels in one event (Add page: Before process for example) and save it in the session array.

Here is just a sample:

global $conn;

$str1 = "select * from `v_q1`";

$rs1 = db_query($str1,$conn);

while ($data1 = db_fetch_array($rs1))

{

$q_number = $data1["q_id"];

$SESSION["q".$q_number] = $data1["q"];

}


Then print this $_SESSION in any place you want:

echo $_SESSION["q_1"];

T
thesofa author 11/14/2008

Thank you for that, will you please clarify something for me?

If I set a session variable from one page, is that variable availble throughout the application or do I need to make it glabal in each page on which I need to use it?

In other words, I have split the list of questions up into 3 pages using custom views, I Add a new record from the first page, then i use a page redirect to the edit page for the second view, to add the values for the second set of questions.

The redirect reads as follows

//********** Redirect to another page ************

header("Location: v_ans_2_edit.php?editid1=".$keys["id_ans"]);

exit();



on the edit page, I have a field of "id_ans"

This does not seem to be populated nor can I save from the edited page, the key does not seem to get passed from the first Add page to the subsequent edit pages.

J
Jane 11/17/2008

Hi,
session variables are global.
If you key field is auto-incremented use following code:

$id = mysql_insert_id();

header("Location: v_ans_2_edit.php?editid1=".$id);

exit();

T
thesofa author 11/17/2008

Spot on again, muchos thankyous

made it a session variable and have used it to redirect to all 7 pages, survey coming on well, if I get a decent one working, how easy is it to set it up as a template? For others on here to use, I am sure a survey template could be handy!

J
Jane 11/17/2008

I suppose you need to use Save project as template option.

T
thesofa author 11/17/2008

D'oh, never saw that one

<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=35187&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' />