SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the long message but I just tried to be as clear as I can. Quick replies are appreciated.


    I am working on a project where I need to add detailed information about multiday activities. Each activity is connected to a different number of days. Which means for example, activity number 1 is 5 days length, activity number 2 is 8 days length, activity number 3 is 15 days length etc...

    The user enters the number of days, then the admin screen displays as many textareas for details and text input boxes for summaries as days.

    Each text input box is named as daysummary1, daysummary2, daysummary3 etc. and each textarea is named as daydetail1, daydetail2, daydetail3 etc. where the number at the end of the variable names indicates the day number.

    What I am trying to do is to insert the "content" of the variable $daysummary1 and $daydetail1 into the corresponding fields of the table day_info and repeat it for all days. However the code below inserts the expression $daysummary1 as it is but not the content of this variable. For example, if $daysummary1 has a content of "Visit the mayor" the code doesn't insert "Visit the mayor" but "$daysummary1" itself. The same problem occurs with day details.

    I read the corresponding section in Kevin's tutorial at http://www.webmasterbase.com/article.php/228/715 but haven't been able to come up with a solution. Any help is appreciated including links to some documentation.

    thank you very much in advance

    regards



    $total_days is the variable which indicates the number of days and it is externally submitted

    <?php
    $day_number = 1;
    while ($day_number < $total_days ){
    $day_sum = "daysummary$day_number";
    $day_det = "daydetail$day_number";

    $add_day_info = "insert into day_info set ".
    "summary = '$$day_sum', ".
    "detail = '$$day_det'";

    if ( mysql_query($add_day_info) ) {
    echo("<P>Record added successfully!</P>");
    }
    else {
    echo("<P>Error: " .
    mysql_error() . "</P>");
    }

    $day_number = $day_number +1;
    } ?>

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $add_day_info = "insert into day_info set ".
    "summary = '$$day_sum', ".
    "detail = '$$day_det'";

    Should be:
    Code:
    $add_day_info = "insert into day_info set ". 
    "summary = '${$day_sum}', ". 
    "detail = '${$day_det}'";
    Hope this helps
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not really a variable variable issue as what you have will work. ${$var} works too and like Son I prefer using braces with such things. Anyway, this needs to be rewritten. I recommend using arrays in your forms. Also, I was confused that a user selects the number of days, shouldn't they be choosing the activity? Assuming so, try something like the following. Another question, how will you know what summary/descriptions go with what activity?

    The following isn't in [ code ] because it was giving strange regex related parse errors, really strange. Not going through [ code ] is working though, here goes :


    <?php
    // User chooses this, we'll assume swimming for now
    $chosen = 'swimming';

    // Our activity/page ratios
    $activities = array('football' => 8,
    'swimming' => 5,
    'foosball' => 11,
    'tennis' => 3);

    // If not sent, create form.
    if ($process != 1) {

    echo '<form method="post" action="'.$PHP_SELF.'">'. "\n";
    echo '<h3>'.$chosen.'</h3>';

    for ($a = 1; $a <= $activities[$chosen]; $a++) {

    echo $a;
    ?>
    <input name="summary[]" size="40"><br>
    <textarea name="details[]" rows="6" cols="50"></textarea><br><br>
    <?php
    }
    ?>
    <br><input type="submit" name="submit_info">
    <input type="hidden" name="process" value="1">
    </form>
    <?php
    // Else insert into database
    } else {

    $conn = mysql_connect('host','user','pass');
    mysql_select_db('dbname');

    foreach ($summary as $k => $v) {

    mysql_query("INSERT into day_info (summary,detail)
    VALUES ('$v','{$details[$k]}'",$conn);
    }
    }
    ?>


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
  •