[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 05-10-2007, 02:49 PM   #1 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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, 11:38 AM   #2 (permalink)
NamePros Member
 
Join Date: Aug 2006
Location: amsterdam
Posts: 93
494.50 NP$ (Donate)

Ms Grace will become famous soon enoughMs Grace will become famous soon enough


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, 02:51 PM   #3 (permalink)
JFS
NamePros Regular
 
JFS's Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 760
56.85 NP$ (Donate)

JFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really nice


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.
__________________
Joćo Fernandes Silva
Selling :
19P.ORG - ARCADEHITS.ORG - AZIAN.NET - COREFANS.COM - CTUTORIALS.NET - DEDISEEK.COM - HITCHECK.COM - HOST15.COM - HOSTCUSTOMER.COM - LARGETIPS.COM - SCRIPTCANDY.COM - VISUALBOOK.NET - VOXVPS.COM / .NET - WALLPAPERSARENA.COM
JFS is offline  
Old 05-11-2007, 05:11 PM   #4 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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>
                                    <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]
unknowngiver is offline  
Old 05-12-2007, 12:16 AM   #5 (permalink)
NamePros Member
 
Join Date: Aug 2006
Location: amsterdam
Posts: 93
494.50 NP$ (Donate)

Ms Grace will become famous soon enoughMs Grace will become famous soon enough


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, 04:09 PM   #6 (permalink)
DNOA Certified Seller
 
RegFee's Avatar
 
Join Date: Apr 2005
Location: Arizona
Posts: 3,644
285.00 NP$ (Donate)

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
Quote:
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.
        
"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
__________________

RegFee.info - Available Domain Names - NEW DESIGN!
DNWholesaler.com - Domains for Sale

<--- baby Rogan says: sorry for spelling/punctuation mistakes, daddy's feeding me
RegFee is offline  
Closed Thread


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 09:25 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85