...

View Full Version : making a php table gen



Rappa
08-05-2007, 04:32 AM
ugh i have such a headache from this. someone please give me some help:

its a basic table gen, im a novice coder, im doing my best, i tried this far without seeking any help but im out of ideas. heres my code:



<?php
$table = "<table";
$tableclose = ">";
$tdopen = "<td>";
$tdclose = "</td>";
$tropen = "<tr>";
$trclose = "</tr>";
$endtable = "</table>";

if ($cellpadding != "") {
$padding = " " . "cellpadding" . "=" . "\"" . $cellpadding . "\"";
}
else {
$padding = "";
}
if ($cellspacing != "") {
$spacing = " " . "cellspacing" . "=" . "\"" . $cellspacing . "\"";
}
else {
$spacing = "";
}
if ($bordersize != "") {
$border = " " . "border" . "=" . "\"" . $bordersize . "\"";
}
else {
$border = "";
}
if ($width != "") {
$tablewidth = " " . "width" . "=" . "\"" . $width . "\"";
}
else {
$tablewidth = "";
}
if ($bordercolor != "") {
$borderc = " " . "bordercolor" . "=" . "\"" . $bordercolor . "\"";
}
else {
$borderc = "";
}
if ($bgcolor != "") {
$backgroundc = " " . "bgcolor" . "=" . "\"" . $bgcolor . "\"";
}
else {
$backgroundc = "";
}

$tableopen= $table . $padding . $spacing . $border . $tablewidth . $borderc . $backgroundc . $tableclose;

if ($submit == "Generate!") {
$result = $tableopen . "\n" .

$i = 0;
for ($i = $rows; $i > 0; $i++) {
echo $tropen;
for ($i = $columns; $i > 0; $i++) {
echo $tdopen . $tdclose;
}
echo $trclose . "\n" .$endtable;
}
}

if ($reset == "Reset") {
$result = "";
$columns = "";
$rows = "";
$cellpadding = "";
$cellspacing = "";
$bordersize = "";
$width = "";
$bordercolor = "";
$bgcolor = "";
}
?>
<form method=post action=<?php print $PHP_SELF; ?>>
<table align="center"><tr><td>
<table cellspacing="5" border="0">
<tr align="left"><td>Number of Columns:</td><td><input type="text" size="8" name="columns" tabindex="1" value="<?php echo $columns ?>"></td>
<td>Number of Rows:</td><td><input type="text" size="8" name="rows" tabindex="2" value="<?php echo $rows ?>"><td></tr>
<tr align="left"><td>Cell Padding:</td><td><input type="text" size="8" name="cellpadding" tabindex="3" value="<?php echo $cellpadding ?>"></td>
<td>Cell Spacing:</td><td><input type="text" size="8" name="cellspacing" tabindex="4" value="<?php echo $cellspacing ?>"><td></tr>
<tr align="left"><td>Border Size:</td><td><input type="text" size="8" name="bordersize" tabindex="5" value="<?php echo $bordersize ?>"></td>
<td>Width (Pixels or %):</td><td><input type="text" size="8" name="width" tabindex="6" value="<?php echo $width ?>"></td></tr>
<tr align="left"><td>Border Color:</td><td><input type="text" size="8" maxlength="7" name="bordercolor" tabindex="7" value="<?php echo $bordercolor ?>"></td>
<td>Background Color:</td><td><input type="text" size="8" maxlength="7" name="bgcolor" tabindex="8" value="<?php echo $bgcolor ?>"></td></tr>
<tr align="left"><td><input type="submit" value="Generate!" name="submit" tabindex="9"></tr></td></table>
<textarea cols="50" rows="8" name="result" tabindex="10">
<?php echo $result; ?>
</textarea><BR>
<input type="submit" value="Preview" name="preview" tabindex="11">
<input type="submit" value="Reset" name="reset" tabindex="12"></td></tr></table>
</form>


the troubles in:



if ($submit == "Generate!") {
$result = $tableopen . "\n" .

$i = 0;
for ($i = $rows; $i > 0; $i++) {
echo $tropen;
for ($i = $columns; $i > 0; $i++) {
echo $tdopen . $tdclose;
}
echo $trclose . "\n" .$endtable;
}
}


it makes sense in my mind, knowing what i know about php. but im sure its completly wrong. its suppose to show table rows and columns according to the numbers imputted by the user.

im stuck, if someone could possible help it would be nice.


and one more question, on the generators ive used online, the page dosnt refresh when the generate code button is clicked, for mine it does, is it my code? or is it just because its in php? thanks

whizard
08-05-2007, 04:53 AM
Where does $submit come from?

Did you mean to say if($_POST['submit'])..

HTH
Dan

Rappa
08-05-2007, 05:09 AM
submits the name of the Generate button,

in otherwords

if ( Generate is Clicked ) {
do
}

or maybe thats the wrong way to do it, but its been working so ive kept it. b/c i dont know how to do an onclick without javascript.

StupidRalph
08-05-2007, 06:02 AM
Does your reset button actually work too!?! Doesn't seem as if either should work. Unless you're using an extremely old version of PHP where you don't have to explicitly name your variables they just take on the name of your form elements. (Even still your Reset button shouldn't work.)



@Whizard
Thanks for helping me get my good name back :thumbs:

Rappa
08-05-2007, 06:24 AM
lol no reset button works fine aswell, but if you dont mind me asking, how would you do it? or the correct way rather.

StupidRalph
08-11-2007, 09:54 AM
Well you are naming it "reset" but its actually a submit button. Instead of declaring the type as "submit" declare it to be "reset".
Lets indent your code so thats its easier to point out.


<form method=post action=<?php print $PHP_SELF; ?>> //<---you have an extra greater than sign.
<table align="center">
<tr><td>
<table cellspacing="5" border="0">
<tr align="left">
<td>Number of Columns:</td>
<td><input type="text" size="8" name="columns" tabindex="1" value="<?php echo $columns ?>"></td>
<td>Number of Rows:</td>
<td><input type="text" size="8" name="rows" tabindex="2" value="<?php echo $rows ?>">
<td>
</tr>
<tr align="left">
<td>Cell Padding:</td>
<td><input type="text" size="8" name="cellpadding" tabindex="3" value="<?php echo $cellpadding ?>"></td>
<td>Cell Spacing:</td>
<td><input type="text" size="8" name="cellspacing" tabindex="4" value="<?php echo $cellspacing ?>">
<td>
</tr>
<tr align="left">
<td>Border Size:</td>
<td><input type="text" size="8" name="bordersize" tabindex="5" value="<?php echo $bordersize ?>"></td>
<td>Width (Pixels or %):</td>
<td><input type="text" size="8" name="width" tabindex="6" value="<?php echo $width ?>"></td>
</tr>
<tr align="left">
<td>Border Color:</td>
<td><input type="text" size="8" maxlength="7" name="bordercolor" tabindex="7" value="<?php echo $bordercolor ?>"></td>
<td>Background Color:</td>
<td><input type="text" size="8" maxlength="7" name="bgcolor" tabindex="8" value="<?php echo $bgcolor ?>"></td>
</tr>
<tr align="left">
<td><input type="submit" value="Generate!" name="submit" tabindex="9">
</tr>
</td>
</table>
<textarea cols="50" rows="8" name="result" tabindex="10">
<?php echo $result; ?>
</textarea>
<BR>
<input type="submit" value="Preview" name="preview" tabindex="11">
<input type="submit" value="Reset" name="reset" tabindex="12"> //should set type to be "reset" not submit
</td>
</tr>
</table>
</form>

I never did like mixing forms with tables...to much hassle.

Inigoesdr
08-12-2007, 02:58 AM
Does your reset button actually work too!?! Doesn't seem as if either should work. Unless you're using an extremely old version of PHP where you don't have to explicitly name your variables they just take on the name of your form elements. (Even still your Reset button shouldn't work.)
That would work with register globals on(which is a bad idea), regardless of version.
http://www.php.net/manual/en/security.globals.php

You are correct about the reset button though, it would submit the form as well(should be <input type="reset").

Also Rappa, the for loops should look like this:

for ($i = 0; $i < $rows; $i++)
and

for ($i = 0; $i < $columns; $i++)

StupidRalph
08-12-2007, 03:22 AM
That would work with register globals on(which is a bad idea), regardless of version.
http://www.php.net/manual/en/security.globals.php

Oh okay, good catch. Register globals were on by default in the older versions I gotcha. Thats around the time I started fumbling with PHP, Register Globals was off by default, but most of the tutorials didn't reflect that yet. Good way to confuse someone who was new. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum