SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello I'm new to this forum, have been reading the sitepoint tribune for a while and enjoying it

    I've been having some problems with the following:
    I'm trying to create an admin script for travel journals.

    I've created the first form with the input fields like this:
    The first form has variables like city, country, theme and a variable 'days'. The variable 'days' stands for length of the trip.

    So when a user submits this form, the form2 is generated and the fields city, country, theme are already filled out.
    But now there are x fields for the days.
    I did this the following way :
    ---------
    $i=1;
    while ($i<=$days) {
    echo"
    <tr><td>Day$i/td><td><input type='Text' name='day$i' value='$day$i' SIZE=255 MAXLENGTH=255></td></tr>
    ";

    $i++;
    }
    ---------

    When the user submits this form, the values are inserted in the database.

    Until here everything works fine.

    I've created the fields in the database like this:

    place
    country
    theme
    day1
    day2
    .
    .
    .
    day10

    So up till 10 days can be stored in the database.

    I'm not satisfied with this solution, because it is not flexible, since one cannot store more then 10 days in the database. I could create like 50 'day' fields, but that's obviously not the way to go.

    I would appreciate it if someone could give me some ideas on how to create a more flexible solution so that the amount of days that can be stored to the database is not limited.

    Regards,

    JamesBond

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you seem to be comfortable with the PHP side of it let's hone in on the db side, it might better to have a days table with a journalid and then a day field and a description field

    Journals
    journalid
    place
    country
    theme


    Days
    id
    journalid
    day
    day_description


    That way you can put as many days in as you want and they will all correspond to the journal from the journal table, does this make sense?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it does make sense!
    Thanks for your help, I think i should be able to make this work now.

    I became interested in php/mysql a few months ago (without previous programming experience) so I've still a lot to learn

    JamesBond

  4. #4
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used the 2 tables journals and days, like freddydoesphp advised, but I've stumbled on another problem.

    After the user filled out the first form (place, country, theme, amount of days), the second form is generated.
    It looks something like this (assuming the journey lasted 4 days):

    place: Paris
    country: France
    theme: Environment
    day1:
    day2:
    day3:
    day4:

    Now the user has to fill out the day fields and press submit to add this journal to the database.

    So in this example I would have to insert 1 record in the journal table and 4 records in the days table.

    What is proper way to do this?

    JamesBond

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to run multiple INSERT queries which is alright just insert the stuff into the journal table then use mysql_insert_id() to get the id of the record you just inserted (http://php.net/mysql_insert_id) then put a record in the day table for each day using the id you retrieved from the journal table as the journalid for the day table. Does that make sense for you, if you need specifics on how to loop through the days and make a query for each, just ask
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you show me how to do this?

    I really appreciate your help.

    JamesBond

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make your day text fields in your form look like this:

    <input type='Text' name='day[$i]'SIZE=255 MAXLENGTH=255>

    $db = mysql_connect("host", "user", "pass");
    mysql_select_db("dbname");

    $journalresult = mysql_query("INSERT INTO journaltable set place = '$place', country = '$country', theme = '$theme'");
    $journalid = mysql_insert_id($db);
    foreach($day as $key => $val) {
    $dayquery = mysql_query("INSERT INTO daytable set journalid = '$journalid', day = '$key', day_description = '$val'");
    }


    Should do the trick. This is tested and staright out of my head so some minor tweaks may need to be made, but this should be a good starting point
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •