NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Making a table...with multiple fields

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 05-10-2007, 03:49 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes

Making a table...with multiple fields


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?
unknowngiver is offline  
Old 05-11-2007, 12:38 PM   #2 (permalink)
NamePros Member
Join Date: Aug 2006
Location: amsterdam
Posts: 93
Ms Grace is a jewel in the roughMs Grace is a jewel in the roughMs Grace is a jewel in the rough
 



I'll might be able to answer your question if you provide the form code...
__________________
Directory Bids | ArcadeCharts.com | Play Arcade Games
CasinoPremiums.com | DrmRemover.com | InsuranceBasic.com | Pedicure.cc | 32-bit.com | BearDolls.com
Ms Grace is offline  
Old 05-11-2007, 03:51 PM   #3 (permalink)
JFS
NamePros Regular
 
JFS's Avatar
Join Date: Oct 2005
Location: Portugal
Posts: 800
JFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to all
 



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.
????: NamePros.com http://www.namepros.com/programming/326447-making-a-table-with-multiple-fields.html

hope it helps.
__________________
Joćo Fernandes Silva
JFS is offline  
Old 05-11-2007, 06:11 PM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
hm no u guys are getting the wrong idea...I am talking about a MYSQL database table
here is my code:

PHP Code:
                        <?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>
????: NamePros.com http://www.namepros.com/showthread.php?t=326447
                                    <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>
????: NamePros.com http://www.namepros.com/showthread.php?t=326447
                                    <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]
unknowngiver is offline  
Old 05-12-2007, 01:16 AM   #5 (permalink)
NamePros Member
Join Date: Aug 2006
Location: amsterdam
Posts: 93
Ms Grace is a jewel in the roughMs Grace is a jewel in the roughMs Grace is a jewel in the rough
 



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
__________________
Directory Bids | ArcadeCharts.com | Play Arcade Games
CasinoPremiums.com | DrmRemover.com | InsuranceBasic.com | Pedicure.cc | 32-bit.com | BearDolls.com
Ms Grace is offline  
Old 05-17-2007, 05:09 PM   #6 (permalink)
DNOA Certified Seller
 
RegFee's Avatar
Join Date: Apr 2005
Location: Arizona
Posts: 3,680
RegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond reputeRegFee has a reputation beyond repute
 


Third World Education Third World Education Save The Children Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health Baby Health
Originally Posted by Ms Grace
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 Code:

<?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.
????: NamePros.com http://www.namepros.com/showthread.php?t=326447
        
"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 (
$countcount($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];    
????: NamePros.com http://www.namepros.com/showthread.php?t=326447
        }
        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
__________________
Nombrex.com
RegFee is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:34 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger