Unstoppable Domains

Making a table...with multiple fields

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
19
Hey
I am getting data from a form ..[allowing as many fields ] and then im using that data to make a table...but i cant figure out how to make that table..
should i make the table first...then go through each input and make fields [but then it would use 2 many queries] or is there another way of doing it?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
I'll might be able to answer your question if you provide the form code...
 
0
•••
what this code do?

simple, it gets the all the content of the table bug and display it.
Code:
<?php
// starts the table
<table class="style19" cellspacing="0" cellpadding="0">

//query the database
$query = "SELECT * FROM bug ORDER BY id_bug DESC";
$result = mysql_query($query) or die(mysql_error());

// starts a cycle and will only stop when there is no more results from the query
while($row = mysql_fetch_array($result)){
      $bug_list1 .="
	<tr>
		// $row['name of your field'] if you have 20 fields then add more lines
		<td class='style40'>".$row['id_bug']."</td>
		<td class='style40'>".$row['topico']."</td>
		<td class='style40'>".$row['username']."</td>
		<td class='style40'>".$row['assigned']."</td>
		<td class='style40'>".$row['status']."</td>
	</tr>
       ";
    }

echo $bug_list1;
?>
</table>

this is simple way.

if you want to change the colors from one line to the other you can do this:
Code:
<table class="style19" cellspacing="0" cellpadding="0">
<?php
$query = "SELECT * FROM bug ORDER BY id_bug DESC";
$result = mysql_query($query) or die(mysql_error());
$var = "Y";
while($row = mysql_fetch_array($result)){
    if ($var=="Y"){
      $bug_list1 .="
	<tr>
		<td class='style30'>".$row['id_bug']."</td>
		<td class='style30'>".$row['topico']."</td>
		<td class='style30'>".$row['username']."</td>
		<td class='style30'>".$row['assigned']."</td>
		<td class='style30'>".$row['status']."</td>
	</tr>
       ";
    }
    if ($var=="N"){
            $bug_list1 .="
	<tr>
		<td class='style20'>".$row['id_bug']."</td>
		<td class='style20'>".$row['topico']."</td>
		<td class='style20'>".$row['username']."</td>
		<td class='style20'>".$row['assigned']."</td>
		<td class='style20'>".$row['status']."</td>
	</tr>
       ";
    }
    if ($var=="N"){
      $var="Y";
    }else $var="N";

}
  echo $bug_list1;

?>

</table>

instead of this you can also check if the id ( in this case id_bug ) is pair or not, but i prefer this way.

hope it helps.
 
0
•••
hm no u guys are getting the wrong idea...I am talking about a MYSQL database table
here is my code:

PHP:
						<?php
							for($a=1;$a<=$num_fields;$a++)
							{
						?>
								<tr>
									<td width="5%"><p id="field">#<?php echo $a;?></p></td>
									<td width="20%"><input type="text" name="name<?php echo $a; ?>" id="name<?php echo $a; ?>" maxlength="10" class="field" value="Name <?php echo $a; ?>" /></td>
									<td width="30%">
										<select name="type<?php echo $a; ?>" id="type<?php echo $a; ?>" class="field">
											<option>Small-Medium Text</option>
											<option>Huge Text</option>
											<option>Small-Medium Integer</option>
											<option>Huge Integer</option>
											<option>Real Number</option>
										</select>
									</td>
									<td width="5%"><input type="text" maxlength="3" name="length<?php echo $a; ?>" id="length<?php echo $a; ?>" class="field"/></td>
									<td width="20%">
										<select name="null<?php echo $a; ?>" id="null<?php echo $a; ?>" class="field">
											<option>Not Null</option>
											<option>Null</option>
										</select>
									</td>
									<td width="15%"><input type="checkbox" name="unique<?php echo $a;?>" id="unique<?php echo $a;?>" class="field"/></td>
								</tr>
						<?php
							}
						?>
Where $num_fields is a user inputted value [int]
 
0
•••
I'm not sure if this helps but:

I think you could create a table first throuh phpadmin (or something similiar) with the fields: id [auto increment], name, type, lenght, null, unique.

Also make the input names arrays. Example: change name$a => name[$a], type$a => type[$a], etc...

So when a user submits the form, you just have to loop through the array and create the INSERT query. Ones the query is created, you just have to execute it. That way you only have to execute one query.

Ms Grace
 
0
•••
Ms Grace said:
I'm not sure if this helps but:

I think you could create a table first throuh phpadmin (or something similiar) with the fields: id [auto increment], name, type, lenght, null, unique.

Also make the input names arrays. Example: change name$a => name[$a], type$a => type[$a], etc...

So when a user submits the form, you just have to loop through the array and create the INSERT query. Ones the query is created, you just have to execute it. That way you only have to execute one query.

Ms Grace

I wanted something that did this for a script I'm making now, so I wrote the following:

PHP:
<?php
if(isset($_POST['submit']))
{

	//Define variables
	$keys = "";
	$values = "";
	$error = "";
	// Copy post data into another array
	$arr_post_data_copy = $_POST;
	
	//designate which form data need to be handled differently, if applicable
	$badkeys = 
		array(
		"submit", //neccesary, otherwise will try to input data into   
                            //nonexistant 'submit' column.
		"data1",
		"etc"
		);
		
	//remove these keys from arr_post_data_copy
	
	foreach ($badkeys as $b)
	{
		unset($arr_post_data_copy[$b]);
	}
	//remove blank values from the new array
	$arr_post_data_copy = array_filter($arr_post_data_copy);
	
	//build an array with all of the keynames
	$count = "0";
	while ($count< count($arr_post_data_copy))
	{
	
		$goodkeys[] = key($arr_post_data_copy);
		next($arr_post_data_copy);
		$count++;
	}
	
	//build key and value strings with post data
	$count = "0";
	foreach ($arr_post_data_copy as $data)
	{
		echo $data."<br>";
		if ($keys != "")
		{
			$keys .= ",".$goodkeys[$count];	
		}
		else 
		{
			$keys .= $goodkeys[$count];		
		}
			
		if ($values != "")
		{
			$values.= ",'".addslashes($data)."'";	
		}
		else 
		{
			$values.= "'".addslashes($data)."'";			
		}
	$count++;
	}
	
	//check if data has made it this far
	if ($keys == "" OR $values == "")
	{
		$error .= "<li>No values were passed to the script.";
	}
	
	
	if ($error == "")
	{
		// Connect to Database 
		include("connect.php");
		// Execute the query
		mysql_query("INSERT INTO tablename ($keys) VALUES ($values)")or die(mysql_error());
		echo "Entry # ".mysql_insert_id()." was successfully added to the database";
	}
	
	else 
	{
		echo $error;
	}

}

else 
{

?>
Test Form:<br />
<form method="POST" name="form">
<input type="text" size="25" name="textbox1" />
<input type="text" size="25" name="textbox2" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php 
}
?>

I have it check if the string is empty and if not, use a comma, if so, don't use a comma. There are quicker ways to go about this, ie setting a counter for the first run through and incrementing the counter, then checking if counter is >0, then adding a comma before adding the rest of the string. Also, I could've just built the entire string and then trim any commas off the beginning. I'm completely new to this though, so I can get away with making fat scripts ;)

Hope this helps,
Jorge
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back