Forums

Question about PHP code in CF6

Fredolino 07 Apr, 2019
Hi,
I had used a PHP code in CF5. But it looks as if the CF6 does not work anymore.
I have to submit the code in a PHP action in "Design" in front of Save Data of the table in which the data should be stored.
What do I have to change there so that values of lat and lon are stored in the other table under latitude and longitude?
$db = JFactory::getDBO();
$app = JFactory::getApplication(); $query = $db->getQuery(true);
$query
->select(array('c.lat','c.lon')) 
->from($db->quoteName('mtb_blaetter', 'c'))
->where($db->quoteName('c.mtb') . ' = "' . $form->data['addresstext'] . '"')
->setLimit(1);
$db->setQuery($query);
$db->execute();  if ($db->getNumRows())
{
$c = $db->loadObject();
$lat = $c->lat;
$lon = $c->lon;
$form->data['latitude'] = $lat;
$form->data['longitude'] = $lon;
}
Max_admin 08 Apr, 2019
Nothing should be changed if your "data provider" in "Save data" is {data:}

Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
healyhatman 08 Apr, 2019
1 Likes
Well except it should be $this->data("field") not $form->data["field"]
Fredolino 30 Apr, 2019
Hi,

I tried the code as "Custom code" and as "PHP" before the action "Save Data" in the "submit-section", but it does not work.
The script should read from the DB table "mtb_blaetter" the record which belongs to the "mtb" selected in the dropdown field and these values (lat and lon) in the DB table "#__markers" in the column "latitude" and " longitude "store.

Saving "mtb" values to the database via the dropdown selector works.
But the values of "lat" and "lon" are not saved.

Does anyone know where the problem is?

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select(array('c.lat','c.lon'))
->from($db->quoteName('mtb_blaetter', 'c'))
->where($db->quoteName('c.mtb') . ' = "' . $this->data['addresstext'] . '"')
->setLimit(1);
$db->setQuery($query);
$db->execute(); if ($db->getNumRows())
{
$c = $db->loadObject();
$lat = $c->lat;
$lon = $c->lon;
$this->data['latitude'] = $lat;
$this->data['longitude'] = $lon;
}
Max_admin 30 Apr, 2019
Not sure but you can use a v6 Read Data for that!

Best regards
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
healyhatman 30 Apr, 2019
$this->data("field") not square brackets. It's a function, not an array.
Max_admin 01 May, 2019

$this->data("field") not square brackets. It's a function, not an array.


its both actually!🙂
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
healyhatman 01 May, 2019
I reject your reality and substitute my own

Also OP just do it in a read data action
Fredolino 01 May, 2019
Hi,
many thanks for the info.

I also tried "read data".
read_data20
Model: Data20
DB-table: mtb_blaetter
Select type: Return an array of key...
Fields to retrieve: mtb

in the dropdown:
name: Article[addresstext]
ID: addresstext
Option:
=auswählen
{var:read_data20}

The dropdown will show the correct list (mtb) as a selection.

If I select "mtb", then "mtb" will be stored correctly in the database.

When I put in "read_data" in "Fields to retrieve":
mtb
lat
lon
I'll be in the dropdown mtb, lat, lon displayed. But I would like to have only "mtb" to choose from.

I have 2 hidden fields for "lat" and "lon" in form where these values could be inserted and stored in the database. Has not worked yet.
I tried 2 dropdown events but did not work either.

I thought 2 events in dropdown might work:
Triggering event: Change
Triggering action: z.B. Reload oder Add to oder Function
Possible values: lat
Affected elements: hidden94 (is the ID from hiddenfield, Name: Article[latitude], Value: {var:read_data20})

Triggering event: Change
Triggering action: z.B. Reload oder Add to oder Function
Possible values: lon
Affected elements: hidden95 (is the ID from second hiddenfield, Name: Article[longitude], Value:{var:read_data20})

I did not get it though.
healyhatman 01 May, 2019
I'm really struggling to understand what you're trying to achieve.
Fredolino 01 May, 2019
:-)
I have a table "mtb_blaetter" with the columns: mtb, lat, lon

If I select a value from the column "mtb" in the dropdown, then the corresponding values from the columns "lat" and "lon" together with "mtb" should be stored in the table #_markers.
"mtb" will be saved if I have selected a value in DropDown. "lat" and "lon" but not, not even if I try it with 2 hidden fields in the form.

First, I tried the PHP code. Did not work.
Then I tried it with 2 events in DropDown, which should save "lat" and "lon" in the hidden fields.
Did not work either.

Example:
From the "mtb_blaetter" table, the 1st row, ie the mtb number: 2448, is selected from the "mtb" column in the dropdown. (see Screen1)

The corresponding values from the columns "lat" and "lon",also 53.5384795 and 13.7315124, are stored together with the mtb number (2448) in the table "#_markers". (see Screen2).

​[file=12154]screen1.jpg[/file]
​[file=12155]screen2.jpg[/file]
healyhatman 01 May, 2019
If the lat and long are associated with an mtb, why do you need to save them at all?
Fredolino 01 May, 2019
The question is legitimate. :-)
I need the coordinates lat and lon, because the respective center coordinates are in a mtb quadrant and the point on a distribution map (divided into quadrants) is thus displayed exactly in the middle of the quadrant.
Is a bit complicated, because I use the input form to access an OS Map and GeoCoding.
healyhatman 01 May, 2019
Ok but why not just use the mtb and when you go to display your map, read the database again to get the lat and long? Which you will access using {var:read_data#.model.field} with the read set the return first matching
This topic is locked and no more replies can be posted.