Skip to main content
Inspiring
January 16, 2007
Question

How to save data from a html table?

  • January 16, 2007
  • 1 reply
  • 579 views
Hi,

I have a table with 10 cols and 5 rows.
I want to save the table to a database with 2 fields (In the 1st field I will save the header of the table,in the 2nd field I will save the items of the first row and in the 3rd field I will save the values which are in the table.)

So I need to loop through the columns and for each column loop through all the rows.
Anyone has can give me the general structure of the code (loop,code for save button). I don't have an idea how to do it.

Urgent help required thx

    This topic has been closed for replies.

    1 reply

    Fernis
    Inspiring
    January 16, 2007
    Can you be a bit more specific with the question? Now you're asking to save data from a html table... Do you mean you have just a <TABLE> of data from a 3rd party website, which you want to extract? That's more of a regExp problem, and matter of arranging the extracted data into an array.

    I am deliberately assuming there are no form fields or input controls on your page, since you didn't hint anything about that.
    Like2FlexAuthor
    Inspiring
    January 16, 2007
    No maybe I explained it wrong. I'll try to re-explain it.

    I have populated the columns from a particular table fieldsfor example "names" in each column header)
    Then I have populated the first row of the table with subjects (from another table).

    Now for each column/row pair (name/subject pair), I am inputting a value in the table (in textboxes).
    See the code below which displays my html table.

    <table border="1">
    <tr>
    <td>Names/Subjects</td>

    <cfloop query="qNames">
    <td width="#variables.columnwidth#">#qNames.names#</td>
    </cfloop>
    </tr>


    <cfloop index="i" from="1" to="#qSubj.recordcount#">
    <tr>
    <td width="#variables.columnwidth#">#qSubj.subject #</td>
    <cfloop index="j" from="1" to="#qNames.recordcount#">
    <cfquery name="qrebvalue" datasource="mydbsource">
    select markets
    from tbl_marks
    where names = '#qNames.broker_name
    #'
    and subject = '#qSubj.subj#'
    </cfquery>
    <td><input type="text" width="#variables.columnwidth#" value="#marks#"></td>
    </cfloop>
    </tr>
    </cfloop>
    </table>





    So now I have a save button. How do I code to save the Names,Subject and Marks in a table with the following 3 fields (names,subject,marks).

    Could you help me with this pls.
    THx
    Fernis
    Inspiring
    January 16, 2007
    I would probably do this with CFGRID, but on the other hand, I'm a fan of these old-fashioned input grids as well.

    I would do this like follows: if you have numeric IDs for your subjects and names as well in the db, select those in the queries, so we can use them in the input field names. in qNames query, there would be ...", id as nameId" somewhere, and in qSubj query, ...", id as subjId".

    Now, make the row
    <input type="text" width="#variables.columnwidth#" value="#marks#">
    look like
    <input name="mark_#nameId#_#subjId#" type="text" width="#variables.columnwidth#" value="#marks#">

    All your input fields will now have a unique name, telling which subject and name it matches. (mark_31_6, mark_42_7, etc.).

    After submitting the form by clicking Save, loop through all form fields. If the field name contains "mark" and the list length ("_" as the separator) is 3, you can evaluate that field's value. You can now also extract the subjectId and nameId from the field name.

    It would be handier if your tbl_marks had numeric fields for the name and subject relation, because now you'll have to first check which name and subject matches the id number, and then do the appropriate database update or insert statements. Now the extra checks will require a bit more complex query, or some extra basic queries to the database.

    I hope you got the idea at least :)