By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

php / mysql / array

P: 3
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="createtables2.php" target="main" method="get">
<table border="0" width="260">
<tr>
<td height="26">
Table ';
echo '<td height="26" width="102"><input 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)
Feb 12 '08 #1
Share this Question
Share on Google+
8 Replies


dlite922
Expert 100+
P: 1,584
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="createtables2.php" target="main" method="get">
<table border="0" width="260">
<tr>
<td height="26">
Table ';
echo '<td height="26" width="102"><input 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_THIRD_PAGE.php" method="POST">
<table border="0" width="260">

<?php

for($i = 0; $i < $tableno; $i++)
{
echo "<td height="26">Table ", $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

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

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

foreach ($tableNamesArray 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
Feb 12 '08 #2

P: 3
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_THIRD_PAGE.php" method="POST">
<table border="0" width="260">

<?php

for($i = 0; $i < $tableno; $i++)
{
echo "<td height="26">Table ", $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

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

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

foreach ($tableNamesArray 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\createtable.php on line 16

line 16 is;
echo "<td height="26">Table ", $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
Feb 13 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Double quotes within a double quoted string for height. Either use single quotes or escape them:[php]
echo "<td height='26'>Table $i><input type='text' name='tableName[]' /></td>";[/php]
Ronald
Feb 13 '08 #4

Markus
Expert 5K+
P: 6,050
The quotes around
td height="26"
need to be escaped with backslashes.
[php]
td height=\"26\"
[/php]

EDIT: Darn you ronver!
Feb 13 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
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
Feb 13 '08 #6

Markus
Expert 5K+
P: 6,050
Next time I'll wait until you had your say.

Ronald
Let that be a lesson to all of you!
Feb 13 '08 #7

P: 3
Hi All,

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

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

John
Feb 14 '08 #8

ronverdonk
Expert 2.5K+
P: 4,258
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
Feb 14 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.