how to merge veriables?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    how to merge veriables?

    This seems relatively simple, and I'm sure I've done it in the past, but
    I can't seem to come up with a solution.

    the following variables are sent via form using textbox inputs:
    $var_1 = 'var1';
    $var_2 = 'var2';
    $var_3 = 'var3';

    I want to use the variable values to create mySQL column names like so:

    $arr = array($var_ ????)
    foreach ($arr as &$value) {
    mysql_query ("ALTER TABLE table_name ADD $value varchar(100) NOT
    NULL");
    }

    The "????" indicates where I think I am getting stuck on how to capture
    the form data into an array. The variables are identical character
    length and numbered sequentially, so it is possible to do something like:

    $i = 1;
    while ($var_???? >= 1) {
    mysql_query ("ALTER TABLE table_name ADD $value varchar(100) NOT
    NULL");
    i++
    }

    Again, the "????" indicates where I think I'm getting stuck on how to
    cycle through the $var_x list.

    I know this probably rudimentary and I feel dumb for not being able to
    find the solution without coming here. I would really appreciate some
    direction.

    Thanks in advance
    -Dave
  • Jerry Stuckle

    #2
    Re: how to merge veriables?

    Dave wrote:[color=blue]
    > This seems relatively simple, and I'm sure I've done it in the past, but
    > I can't seem to come up with a solution.
    >
    > the following variables are sent via form using textbox inputs:
    > $var_1 = 'var1';
    > $var_2 = 'var2';
    > $var_3 = 'var3';
    >
    > I want to use the variable values to create mySQL column names like so:
    >
    > $arr = array($var_ ????)
    > foreach ($arr as &$value) {
    > mysql_query ("ALTER TABLE table_name ADD $value varchar(100) NOT
    > NULL");
    > }
    >
    > The "????" indicates where I think I am getting stuck on how to capture
    > the form data into an array. The variables are identical character
    > length and numbered sequentially, so it is possible to do something like:
    >
    > $i = 1;
    > while ($var_???? >= 1) {
    > mysql_query ("ALTER TABLE table_name ADD $value varchar(100) NOT
    > NULL");
    > i++
    > }
    >
    > Again, the "????" indicates where I think I'm getting stuck on how to
    > cycle through the $var_x list.
    >
    > I know this probably rudimentary and I feel dumb for not being able to
    > find the solution without coming here. I would really appreciate some
    > direction.
    >
    > Thanks in advance
    > -Dave[/color]


    <input type="text" name="name[]" ... >

    For each of the inputs. The brackets indicate all the values are to be
    passed in the array named "name".


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Dave

      #3
      Re: how to merge veriables?

      Dave <???@???.???> wrote in news:TpWdnZjPTM 9lCufeRVn-ig@giganews.com :
      [color=blue]
      > This seems relatively simple, and I'm sure I've done it in the past,
      > but I can't seem to come up with a solution.
      >
      > the following variables are sent via form using textbox inputs:
      > $var_1 = 'var1';
      > $var_2 = 'var2';
      > $var_3 = 'var3';
      >
      > I want to use the variable values to create mySQL column names like
      > so:
      >
      > $arr = array($var_ ????)
      > foreach ($arr as &$value) {
      > mysql_query ("ALTER TABLE table_name ADD $value varchar(100)
      > NOT
      > NULL");
      > }
      >
      > The "????" indicates where I think I am getting stuck on how to
      > capture the form data into an array. The variables are identical
      > character length and numbered sequentially, so it is possible to do
      > something like:
      >
      > $i = 1;
      > while ($var_???? >= 1) {
      > mysql_query ("ALTER TABLE table_name ADD $value varchar(100)
      > NOT
      > NULL");
      > i++
      > }
      >
      > Again, the "????" indicates where I think I'm getting stuck on how to
      > cycle through the $var_x list.
      >
      > I know this probably rudimentary and I feel dumb for not being able to
      > find the solution without coming here. I would really appreciate some
      > direction.
      >
      > Thanks in advance
      > -Dave
      >[/color]

      I forgot to give the example I was thinking of when I typed the subject,
      but I have a variable set for the actual number of variables. In
      otherwords, if there are going to be 3 columns in the table, there is a
      variable set to "3" like so:


      $col_num = "3";


      $i = 1;
      while ($col_num >= i) {
      $col_name = ('$col_num_' . $i);
      mysql_query("AL TER TABLE table_name ADD $col_name varchar(100) NOT
      NULL");
      i++;
      }

      When I echo $col_name I am getting "$col_num_1 ", "$col_num_2 " and
      "$col_num_3 ". It's not returning the value of $col_num_1 but the actual
      text "$col_num_1 ".

      Again, thanks in advance.

      Comment

      • Dave

        #4
        Re: how to merge veriables?

        Jerry Stuckle <jstucklex@attg lobal.net> wrote in
        news:VrOdnRn0_v n2PefeRVn-qw@comcast.com:[color=blue]
        >
        > <input type="text" name="name[]" ... >
        >
        > For each of the inputs. The brackets indicate all the values are to
        > be passed in the array named "name".
        >
        >[/color]



        This got it!

        Thanks Jerry!

        Comment

        Working...