Jump to content

Recommended Posts

Hi everyone!

 

Please help me as im totally lost!

 

I need to make a table where the user can select the ammount of rows and columns so what Ive done so far is make a table with drop down boxes where the user can select the rows and columns but I don't know how to ransfer this?  Can anyone please help?

 

<table colour = " red " border = " 1 ">
<caption> <strong>Please Select...</strong></caption>
<thead> <tr> <th> No. of Rows </th> <th> No. of Columns </th>
</tr> </thead> <tfoot> <tr>
<th> Total </th> <th> Rows+Columns </th>
</tr>
</tfoot>
<tbody> <tr>
<td> <select>
  <option value"1R">1 Row</option>
  <option value"2R">2 Rows</option>
  <option value"3R">3 Rows</option>
</select>

</td> <td> <select> 
<option value"1C">1 Column</option>
  <option value"2C">2 Columns</option>
  <option value"3C">3 Columns</option> 
</select></td>
</tr>
</tbody>
</table>

Link to comment
https://forums.phpfreaks.com/topic/131960-php-tables/
Share on other sites

Thanks for the link.  The HTML isnt really the problem its how to code this in PHP I am having difficulty in. 

 

You don't show any <form> tags

Your <select>s don't have name attributes

 

You use PHP to output HTML code, but if you don't know the correct HTML, how do hope to get it right with PHP

 

 

Link to comment
https://forums.phpfreaks.com/topic/131960-php-tables/#findComment-685701
Share on other sites

No the PHP is the problem.  It is my entention to write a more complex table as that only has 3 rows and columns but I merely wanted to gain an understanding in how to go about coding the PHP based on an easy table so I can apply the same principles to my own table

Link to comment
https://forums.phpfreaks.com/topic/131960-php-tables/#findComment-685744
Share on other sites

No the PHP is the problem.  It is my entention to write a more complex table as that only has 3 rows and columns but I merely wanted to gain an understanding in how to go about coding the PHP based on an easy table so I can apply the same principles to my own table

 

There is no way you can do anything in PHP from the HTML in your original post. I gave my reasons in my last post.

Link to comment
https://forums.phpfreaks.com/topic/131960-php-tables/#findComment-685755
Share on other sites

I use a form that takes the

# columns

# of rows

Border Width

Cellpadding

Cellspacing

and Table width

 

The php part then takes those inputs and creates the table using this,

 

print "<table border=\"$border\" cellpadding=\"$cp\" cellspacing=\"$cs\" width=\"$width$ms\">\n";
for ($r=0; $r<$row; $r++) {
     print "<tr>\n";
       if($r != $row) {
         for ($i=0; $i<$col; $i++) {
          $cell = $i+1;
	  print "<td>$cell</td>\n";	
     }	
     if($i == $col) {
          print "</tr>\n";
         }
       }
}
print "</table>\n";

 

Here is the whole shabang that you can taylor to your needs.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Dyna Table</title>
<style type="text/css">
html,body{margin:0;padding:0}
body{font: 76% arial,sans-serif;text-align:center}
p{margin:0 10px 10px}
a{display:block;color: #981793;padding:10px}
div#header h1{height:80px;line-height:50px;margin:0;
  padding-left:10px;background: #cccccc;color: #202020}
div#container{text-align:left; border: 3px solid #202020;}
div#content p{line-height:1.4}
div#navigation{background:#404040; color: #cccccc;}
div#extra{background:#cccccc; color: #404040; border-bottom: 1px solid #202020;}
div#footer{background: #cccccc;color: #404040}
div#footer p{margin:0;padding:5px 10px}

div#container{width:700px;margin:0 auto}
div#content{float:left;width:500px}
div#navigation{float:right;width:200px}
div#extra{clear:both;width:100%}
</style>
</head>
<body>
<div id="container">
<div id="header"><h1>Dynamic Table Creation</h1></div>
<div id="wrapper">
<div id="content">
<p>
<?php
if (isset($_POST['submitted'])) { // Handle the form.
// Check for Columns.
	if (!empty($_POST['columns'])) {
		$col = $_POST['columns'];
	} else {
		$col = FALSE;
	    echo '<p><font color="red" size="+1">Please enter the amount of columns you need!</font></p>';
    }

// Check for Rows.
	if (!empty($_POST['rows'])) {
		$row = $_POST['rows'];
	} else {
		$row = FALSE;
	    echo '<p><font color="red" size="+1">Please enter the amount of rows you need!</font></p>';
    }

	// Check for border width.
	if (!empty($_POST['border'])) {
		$border = $_POST['border'];
	} else {
		$border = 0;
	}

	// Check for table width.
	if (!empty($_POST['width'])) {
		$width = $_POST['width'];
	} else {
		$width = 100;
	}

	// Check for cell Padding.
	if (!empty($_POST['cp'])) {
		$cp = $_POST['cp'];
	} else {
		$cp = 0;
	}

	// Check for cell Spacing.
	if (!empty($_POST['cs'])) {
		$cs = $_POST['cs'];
	} else {
		$cs = 0;
	}

	// Check for cell Spacing.
	if ($_POST['meas'] == "perc") {
		$ms = "%";
	} else {
		$ms = "";
	}





	if ($col && $row) {


print "<table border=\"$border\" cellpadding=\"$cp\" cellspacing=\"$cs\" width=\"$width$ms\">\n";
for ($r=0; $r<$row; $r++) {
     print "<tr>\n";
       if($r != $row) {
         for ($i=0; $i<$col; $i++) {
          $cell = $i+1;
	  print "<td>$cell</td>\n";	
     }	
     if($i == $col) {
          print "</tr>\n";
         }
       }
}
print "</table>\n";

echo"<textarea name=\"code\" rows=\"15\" cols=\"50\" >";
print "<table border=\"$border\" cellpadding=\"$cp\" cellspacing=\"$cs\" width=\"$width$ms\">\n";
for ($r=0; $r<$row; $r++) {
     print "<tr>\n";
       if($r != $row) {
         for ($i=0; $i<$col; $i++) {
          print "<td></td>\n";	
     }	
     if($i == $col) {
          print "</tr>\n";
         }
       }
}
print "</table>\n";
echo"</textarea>";
}
}
?>
</p>

</div>
</div>
<div id="navigation">
<p><strong>Set your table settings in the form inputs</strong> listed below.
</p>
</div>
<div id="extra">
<p><strong>Table Settings</strong> <br />
<form action="index.php" method="post">
<p><b># of Columns:</b><br /> <input type="text" name="columns" size="15" maxlength="15" value="<?php if (isset($_POST['columns'])) echo $_POST['columns']; ?>" /></p>
<p><b># of Rows:</b><br /> <input type="text" name="rows" size="15" maxlength="15" value="<?php if (isset($_POST['rows'])) echo $_POST['rows']; ?>" /> </p>
<p><b>Border Width:</b><br /> <input type="text" name="border" size="15" maxlength="15" value="<?php if (isset($_POST['border'])) echo $_POST['border']; ?>" /> </p>
<p><b>Cell Padding:</b><br /> <input type="text" name="cp" size="15" maxlength="15" value="<?php if (isset($_POST['cp'])) echo $_POST['cp']; ?>" /></p>
<p><b>Cell Spacing:</b><br /> <input type="text" name="cs" size="15" maxlength="15" value="<?php if (isset($_POST['cs'])) echo $_POST['cs']; ?>" /> </p>
<p><b>Table Width:</b><br /> <input type="text" name="width" size="15" maxlength="15" value="<?php if (isset($_POST['width'])) echo $_POST['width']; ?>" />  % <input name="meas" type="radio" value="perc"> or PX <input name="meas" type="radio" value="pix"></p>

<div align="left"><input type="submit" name="submit" value="Create" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

</p>
</div>
<div id="footer"><p>Sample Code Only</p></div>
</div>
</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/131960-php-tables/#findComment-686279
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.