SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form fields and variable length tables

    Hi

    If this has already been covered, I would appreciate links to the threads. I looked but it might be listed differently...

    I am populating a table with fields from a MySQL table...

    PHP Code:
    <form action=update_comm.php method=post>
    <table align=center bordercolor=black cellpadding=10 cellspacing=0 
    border=1 borderwidth=0>
    <tr>
    <th>Sales Person</th>
    <th>Client</th>
    <th>Commission</th>
    <th>End Date</th>
    <th>End Imm</th>

    </tr>
    <?
    $query
    ="SELECT * FROM SalesStaff WHERE SKey = '$skey'";

    $result=mysql_query($query);
    $name mysql_result($result$x'Name');

    $query="SELECT * FROM SalesStaffDetails WHERE SalesStaffKey = '$skey'
     and Active = 'Y'"
    ;

    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $i=0;

    while(
    $i $num) {
    $clientkey=mysql_result($result,$i,"ClientKey");
    $comm_percent=mysql_result($result,$i,"Percent");

    $query1="SELECT * FROM gen_Client WHERE ClientKey='$clientkey'";

    $result1=mysql_query($query1);
    $client=mysql_result($result1$m"ClientName");

    ?>
    <tr>
    <td><? echo $name ?></td>
    <td> <? echo $client ?></td>

    <td align=center><input size=5 type=text name=comm_percent 
    value="<? echo $comm_percent ?>" >%</td>
    <td> 
    <? echo $end_calendar->CreateCalendar(date('d/m/Y'),'end_date',date('d/mY'),1); ?> 
    </td>
    <td align=center><input type="checkbox" name="endimmed" 
    value="checkbox"></td>
    </tr>

    <?
    $i
    ++;
    $name='';

    // end of while
    ?>
    <td colspan=9 align=center><input type=submit value="Submit"></td>
    </table>
    </form>
    As the fields are filled in the while loop, they will all have the same variable name.

    I need to be able to pass all the possible fields but at the moment the receiving script only gets the last field - I'm sure this is caused by the variable names.

    How do I send multiple fields for date and check boxes from the table to the receiving script?

    (I'm not great with arrays)

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Egad
    (I'm not great with arrays)
    Here's your chance to use some and improve that

    Having a bunch of inputs in the form with the same name won't work, so you need to use arrays.

    Use the primary key of the row as the index of the arrays for that row, and you'll be able to match them up when you receive the form and do whatever you do with it.

    Ex:
    PHP Code:
    <input size="5" type="text" name="comm_percent[<?php echo mysql_result($result$i"Your PK Column"); ?>]" value="<?php echo $comm_percent?>" />
    Your code would be a lot cleaner if you fetched each row as an array instead of calling mysql_result everywhere:

    PHP Code:
    while ($row = mysql_fetch_array($result)) {

      //all the stuff inside your current loop
      //using $row['column'] instead of calling mysql_result to access values

      //ex
    ?>

      <input size="5" type="text" name="comm_percent[<?php echo $row['pk']; ?>]" value="<?php echo $row['Percent']; ?>" />

    <?php

    }


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
  •