php / mysql / array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dislins
    New Member
    • Feb 2008
    • 3

    php / mysql / array

    My intention;

    create 3 pages of code;

    Page 1: two questions are asked
    a. What is the database name ($dbname)
    b. How many tables to create ($tableno)

    and passed onto P2 using $_GET

    Page 2: creates (using a while loop) the chosen number of txt boxes (given by $tableno) and creates a unique label for each table needed.

    passes onto P3

    Page 3 creates the tables with no fields. (yet! lol)

    Achieved;

    done page 1 ok..
    page 2 am suffering with,
    a. dynamic creation of txt boxes - OK
    b. dynamic labelling - NOT OK (ie. table0, table2, etc)
    c. passing onto page 3 - NOT OK

    anyone help? Here is the code for page 2;

    <html>
    <body>
    <?php
    include ('dbop.php');
    $dbnamec = $_GET['create'];
    $tableno = $_GET['tableno'];
    $tables0 = 0;
    $name[0] = 0;

    $padding = '<font color="#FFFFFF" >2</font><body>';

    while ($tables <> $tableno)
    {
    echo '<form action="createt ables2.php" target="main" method="get">
    <table border="0" width="260">
    <tr>
    <td height="26">
    Table ';
    echo '<td height="26" width="102"><in put type "text" name="$name[$tables]" /></td></tr></table>';
    $tables++;
    }
    echo '<input type="submit" value="Create"/><BR />';
    ?>
    </body>
    </html>


    (I was trying to use arrays as you can see!!)

    thanks in advance.

    using php5, mysql 5 on
    vista with apache 2.2 (installed xampp)
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by dislins
    My intention;

    create 3 pages of code;

    Page 1: two questions are asked
    a. What is the database name ($dbname)
    b. How many tables to create ($tableno)

    and passed onto P2 using $_GET

    Page 2: creates (using a while loop) the chosen number of txt boxes (given by $tableno) and creates a unique label for each table needed.

    passes onto P3

    Page 3 creates the tables with no fields. (yet! lol)

    Achieved;

    done page 1 ok..
    page 2 am suffering with,
    a. dynamic creation of txt boxes - OK
    b. dynamic labelling - NOT OK (ie. table0, table2, etc)
    c. passing onto page 3 - NOT OK

    anyone help? Here is the code for page 2;

    <html>
    <body>
    <?php
    include ('dbop.php');
    $dbnamec = $_GET['create'];
    $tableno = $_GET['tableno'];
    $tables0 = 0;
    $name[0] = 0;

    $padding = '<font color="#FFFFFF" >2</font><body>';

    while ($tables <> $tableno)
    {
    echo '<form action="createt ables2.php" target="main" method="get">
    <table border="0" width="260">
    <tr>
    <td height="26">
    Table ';
    echo '<td height="26" width="102"><in put type "text" name="$name[$tables]" /></td></tr></table>';
    $tables++;
    }
    echo '<input type="submit" value="Create"/><BR />';
    ?>
    </body>
    </html>


    (I was trying to use arrays as you can see!!)

    thanks in advance.

    using php5, mysql 5 on
    vista with apache 2.2 (installed xampp)

    I'm gonna help, only cause this is your homework BUT you've attempted to get something...(as horrible as it is)

    [PHP]
    <?php

    include ('dbop.php');
    $dbnamec = $_GET['create'];
    $tableno = $_GET['tableno'];
    // your good thus far. you got the values from the last page. now display your page header: You don't have to echo it, end the php tag, and start it later...

    ?>

    <form action="YOU_THI RD_PAGE.php" method="POST">
    <table border="0" width="260">

    <?php

    for($i = 0; $i < $tableno; $i++)
    {
    echo "<td height="26">Tab le ", $i, "><input type='text' name='tableName[]' /></td>";
    }

    ?>

    </tr></table>
    <br />
    <input type="submit" value="Create"/><br />




    [/PHP]

    Notice You will have to use POST to make it easier. GET is a bit messier and harder to do. (so i think at this moment)

    PHP will automatically create an Array for fields that have double brackers [ ] on their names.

    so on the last page you do this

    [PHP]
    <?php

    $tableNamesArra y = $_POST['tableName']; // notice no bracket

    //to with this array of names as you wish: such as print them.

    foreach ($tableNamesArr ay as $tableName)
    {
    echo $tableName, "<br />";
    }

    ?>


    [/PHP]

    Sorry for any misspellings and syntax errors. THis is not tested and written on the fly.

    PLEASE WRAP YOUR CODE [PHP] and [/PHP] WHEN POSTING IT TO THIS FORUM.

    makes it easier to read for us and you won't get this annoying comment anymore!

    good luck

    Comment

    • dislins
      New Member
      • Feb 2008
      • 3

      #3
      Originally posted by dlite922
      I'm gonna help, only cause this is your homework BUT you've attempted to get something...(as horrible as it is)

      [PHP]
      <?php

      include ('dbop.php');
      $dbnamec = $_GET['create'];
      $tableno = $_GET['tableno'];
      // your good thus far. you got the values from the last page. now display your page header: You don't have to echo it, end the php tag, and start it later...

      ?>

      <form action="YOU_THI RD_PAGE.php" method="POST">
      <table border="0" width="260">

      <?php

      for($i = 0; $i < $tableno; $i++)
      {
      echo "<td height="26">Tab le ", $i, "><input type='text' name='tableName[]' /></td>";
      }

      ?>

      </tr></table>
      <br />
      <input type="submit" value="Create"/><br />




      [/PHP]

      Notice You will have to use POST to make it easier. GET is a bit messier and harder to do. (so i think at this moment)

      PHP will automatically create an Array for fields that have double brackers [ ] on their names.

      so on the last page you do this

      [PHP]
      <?php

      $tableNamesArra y = $_POST['tableName']; // notice no bracket

      //to with this array of names as you wish: such as print them.

      foreach ($tableNamesArr ay as $tableName)
      {
      echo $tableName, "<br />";
      }

      ?>


      [/PHP]

      Sorry for any misspellings and syntax errors. THis is not tested and written on the fly.

      PLEASE WRAP YOUR CODE [PHP] and [/PHP] WHEN POSTING IT TO THIS FORUM.

      makes it easier to read for us and you won't get this annoying comment anymore!

      good luck

      Thanks dlite922 much appreciated and this was (believe it or not) self inflicted homework as I felt it was high time I got to grips with some development work.

      page 2 now give me this error; Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in C:\pathname\cre atetable.php on line 16

      line 16 is;
      echo "<td height="26">Tab le ", $i, "><input type='text' name='tableName[]' /></td>";

      I have tried to look this up and I think its due to the fact that the variable inside the array is starting with a number. If that's right, can I pre-determine what the variable starts with?

      thanks

      John

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Double quotes within a double quoted string for height. Either use single quotes or escape them:[php]
        echo "<td height='26'>Tab le $i><input type='text' name='tableName[]' /></td>";[/php]
        Ronald

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          The quotes around
          td height="26"
          need to be escaped with backslashes.
          [php]
          td height=\"26\"
          [/php]

          EDIT: Darn you ronver!

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by markusn00b
            The quotes around

            need to be escaped with backslashes.
            [php]
            td height=\"26\"
            [/php]

            EDIT: Darn you ronver!
            Next time I'll wait until you had your say.

            Ronald

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by ronverdonk
              Next time I'll wait until you had your say.

              Ronald
              Let that be a lesson to all of you!

              Comment

              • dislins
                New Member
                • Feb 2008
                • 3

                #8
                Hi All,

                ($i = 0; $i < $tableno; $i++)

                just trying to understand this code... is $i just a counter?

                John

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Yes. This statement says (simple view):
                  - at the start of the operation: initialize the counter to 0
                  - perform this operation as long as the counter is lower than the value in $tableno
                  - at the end of each opperation increment the counter by 1

                  Ronald

                  Comment

                  Working...