Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
Defining row id with foreign keys
09/09/2009
13:37
Avatar
LittleVince
Member
Members
Forum Posts: 4
Member Since:
09/09/2009
sp_UserOfflineSmall Offline

Hello,

I'm new to jqGrid, and I still have some difficulties to use this great plugin. Here's a summary of my problem.

I'm using two tables:

drivers(driverID, driverName)

cars(carID, carName)                 (of course, driverID and carID are single primary keys)

So, showing and editing “drivers” in jqGrid is not a problem:

PHP side: When processing the XML file to display the table, I put driverID in the <row id=''> part, and driverName in a <cell> part to set a single column 'driverName' in jqGrid.

When a driver name is edited in this column, I use the PHP/MySQL query: UPDATE drivers SET driverName='$driverName' WHERE driverID='$id'

Ok… But now I need one more table: drivers_cars(#driverID, #carID) which is made of two foreign keys, to allow the user of the webpage to associate any driver to any car (several cars per driver, and several drivers per car). New associations will be generated by creating new rows in a dedicated jqGrid datagrid.

What to put in the XML file (row and cells) to display the content of the table ?

I tried the following, with PHP/MySQL: SELECT driverID, carID FROM drivers_cars

My XML looks like that: <row id=''> <cell>driverID</cell> <cell>carID</cell> </row>

In jqGrid there are two columns “driverID” and “carID”, their content is ok.

But when the user wants to edit a line, what updating query shoud I write?

UPDATE drivers_cars SET driverID='$driverID' WHERE … ?

I hope it wasn't too confusing to read, and maybe I'm not using the right method to do that. But at this time I don't understand how to show/edit a table that uses only foreign keys, because I always identify my rows with XML, using <row id='primary key'>.

Thanks for any help you could bring to me.

Vincent

(ps: I'm french, please forgive any bad english in this post)

09/09/2009
21:08
Avatar
MamaliFrenchi
Member
Members
Forum Posts: 62
Member Since:
22/12/2008
sp_UserOfflineSmall Offline

What Exactly you like to achieve? what the relation between Car & drivers? many to many? or 1 to many? I am guessing you want to seletct a Driver, then associate a car to the driver. You need 2 dropdows one to select driver another for car, then save just IDs. for presentation take a look at subgrid, or Tree grid to get some idea. A propos pour un francais tu ecris tres bien en englais.

10/09/2009
01:38
Avatar
LittleVince
Member
Members
Forum Posts: 4
Member Since:
09/09/2009
sp_UserOfflineSmall Offline

Thank you for this first step in helping me...

The relation would be "many to many": each row in the jqGrid would show an association, and each driver or car could appear several times.

The dropdown lists are a good idea, I did that at the beginning and it works perfectly.

Now stands the same problem... How to edit an existing row? In other words: what's the id of an existing row (what id pass to php to make an UPDATE query...) ?

If anybody as an answer to this, it'll be welcome...

10/09/2009
02:23
Avatar
LittleVince
Member
Members
Forum Posts: 4
Member Since:
09/09/2009
sp_UserOfflineSmall Offline

Ok... I found a first solution, not really clean but working:

PHP-side, during XML creation, I put the following string:  "driverID - carID" (minus symbol is important) in the row cell, as a rowID.

When user edit line, jqGrid sends $driverID and $carID (as edited by user), and $id containing something like: "8-32".

PHP-side, before execution of the editing query, I split content of $id (using the "minus" symbol inserted previously) into $driver and $car.

So I can do my UPDATE query, with "SET" using $driverID and $carID and "WHERE" using $driver and $car...

It works perfectly, but if anyone has a... cleaner... solution, that would be of interest.

10/09/2009
09:03
Avatar
MamaliFrenchi
Member
Members
Forum Posts: 62
Member Since:
22/12/2008
sp_UserOfflineSmall Offline

If I understand correctly your problem, you have a relation table between 2 other talbes. Why not adding a Primary Key in your realtion table something like RealionID, CarID, DriverID where RelationID is your primary key and DriverID, CarID foreigh key to your other tables. Then you can get all your records with RealionID key. Je te souhaite une exellente journee.

10/09/2009
10:47
Avatar
kris
Guest
Guests

you can always append to posted data two pass both keys

10/09/2009
10:47
Avatar
kris
Guest
Guests

you can always append to posted data two pass both keys

10/09/2009
11:30
Avatar
LittleVince
Member
Members
Forum Posts: 4
Member Since:
09/09/2009
sp_UserOfflineSmall Offline

MamaliFrenchi >

   Yes, you correctly understood the problem ^^   Adding a primary key... Why not?

   At the beginning I didn't want to add anything else, because I was trying to create short and simple tables, but this may be the most simple solution to setup and to maintain (PHP "split" used this way is not a clean solution to manage data, I think...).

   I'm gonna try your idea and probably go this way, thank you for this simple and working proposal. (Bonne soirée et sûrement à bientôt ^^)

kris >

   I tried it in my early tests, but it didn't work: when edited by user, values of both keys change and it's impossible to find the original row... It may be possible with the use of events (saving values before editing and passing them) but I was looking for a solution more academical than technical. Thanks anyway for this suggest that could work.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
41 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information