This topic is locked
[SOLVED]

 Get data from another table

8/23/2019 10:52:30 AM
PHPRunner General questions
U
united001 author

Greetings all. Noob here. All I am seeking to do is to fetch data from another table's field column and put it into the field of a current table. I assume this can be done with a code snippet inserted into the table's add page as a "before record added" action. Add() seeks to add another record, I don't need that. Lookup wizard is a possibility, but wondering if another method is better. Suggestions would be helpful and thanks ahead of time!
Randy J.

N
Nir Frumer 8/23/2019

Assuming that that you need the data from a specific record

Take a look at the "lookup wizard" in "fields -> edit as"

It includes an autofill button that let you populate fields from the "looked up" table or view.

good luck,

U
united001 author 8/23/2019

Lookup wizard does work, yes, but seems awkward a bit. Are there any php code snippet solution(s) that avoid lookup wizard?
Randy J

lefty 8/23/2019



Lookup wizard does work, yes, but seems awkward a bit. Are there any php code snippet solution(s) that avoid lookup wizard?
Randy J


You can query the values in Before Record Added
$rs = CustomQuery("SELECT * FROM . " ");

$record = db_fetch_array($rs);
$values['[color="#ff0000"]Field2'] = $record['Field2'];

//Make Field2 Read Only
Assuming Field1 is in both tables as well as field2.
Once you get the hang of Lookup Wizard , it is an easier method in my opinion. Depends on structure and what you need to accomplish. Lookup wizard fills as the user types or options are selected, where the query data is not seen until record is added.

U
united001 author 8/23/2019

Thanks for the code! I thought I was missing something obvious or a simple function or method that escaped my eye. I was not. Your solution is very nice, but the kicker for me is that 1) I can't have field2 as read only, needs to be a text area and 2), I just figured out I need the Autofill function most of all. I think I'll make lookup wizard work.
Randy J

D
david22585 8/24/2019

Just to add to this that if you want it to work with a read only field, you can get the variable and set it as a session. On before process, use a code like this:

// Used to set owner data on other pages

global $strTableName, $conn;

$strSQLExists = "select email, phone from TABLENAME where id=3";

$rsExists = DB::Query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

$_SESSION["email"] = $data["email"];

}


Then set the field to read only, and the default value to the session variable:

$_SESSION["phone"]


That will populate the field as a read only.