Jump to content

Custom table fields & rows


zero_ZX

Recommended Posts

Hi all,

 

So I'm not completely sure on how to do this, but basically this i what i want.

 

I want the users to be able to create "fields" in my table, so let's say we have a structure like this:

 

Field 1  Field 2  Field 3

Data 1  Data 2  Data 3

 

Problem is, that i want my users to update both the <td> and <tr> information via mysql, without altering the database.

And I'm not sure on how I can make sure, that everything gets out in the right order.

 

I have this so far:

 

<table id="sort-table"> 
					<thead> 
					<tr>
<?PHP
echo '	<td> Identifier </td> ';

$result = mysql_query("SELECT * FROM fields ORDER BY `order`");
while($row = mysql_fetch_array($result))


{

echo '	<td> ' . $row["name"] . ' </td> ';
}
?>
						<th style="width:128px">Options</th>

					</tr> 
					</thead> 

This will display the field 1, 2, 3 etc..

 

My problem is.. How do i get the content displayed in the right order to the right fields.. :/

 

I hope you understand me, if not, please tell me so I can elaborate ^_^

I know this might seems like a noob question, and I'm sorry that I couldn't search, but I didn't know what to search for.

Link to comment
Share on other sites

Please elaborate. You already have an ORDER BY in your query which will order the data by whichever column you prefer.

 

If you want to affect what order the columns are shown in you have to create a template in your script.

 

ex DB:

|ID|  |NAME|  |AGE|

1        Joe        25

2        Jan        18

3        Yolanda  35

 

ex PHP:

<?php
    $sql = "SELECT * FROM db ORDER BY ID ASC";
    $result = mysql_query($sql);
    echo '<table>';
    echo '<tr><td>ID</td><td>NAME</td><td>AGE</td></tr>';
    while($rows = mysql_fetch_array($result)){
     echo '<tr><td>'.$rows['ID'].'</td><td>'.$rows['NAME'].'</td><td>'.$rows['AGE'].'</td></tr>';
    }
    echo '</table>';
?>

 

something like that should work. I just banged it out in the reply textarea, so forgive me if there's an error in there.

 

E

Link to comment
Share on other sites

Thanks for your reply:

 

|ID|  |NAME|  |AGE|

1        Joe        25

2        Jan        18

3        Yolanda  35

 

The ID is one table in my database called identify.

The NAME & AGE are stored in another table called fields.

Joe, 25, Jan, 18, Yolanda, 35 is stored in a third table called content.

Within the content table each content is linked to a field and to the identify.

 

My question is how i would print the different the data, as in, what query should i execute?

As in first row, I want to select ID number one and the next field by order, then get the info from the content table matching those. Then keep on looping this process until everything is printed.

 

Hope that makes more sense :/

As you see, nothing is defined in the database structure, it's all user made.

Link to comment
Share on other sites

Is there a reason you have put this information across multiple tables?

The best way would be to have all of it in one table, and then have different columns for each of the data you want entered.

 

Denno

Yea, the table would be very confusing if it stored multiple items. If you stored everything within a single table, there would be no need of tables, just a database :P

 

Any way, so I got this:

<?PHP
echo '	<td> Identifier </td> ';

$result = mysql_query("SELECT * FROM fields ORDER BY `order`");
while($row = mysql_fetch_array($result))


{

echo '	<td> ' . $row["name"] . ' </td> ';
}
?>
						<th style="width:128px">Options</th>

					</tr> 
					</thead> 
					<tbody> 
<?PHP						

$result = mysql_query("SELECT * FROM identify ORDER BY `id`");
while($inforow = mysql_fetch_array($result))
{

echo '	<td> ' . $inforow["value"] . ' </td> ';

$resultfields = mysql_query("SELECT * FROM fields ORDER BY `order`");
$rowfields = mysql_fetch_array($resultfields);	
$resultcontent = mysql_query("SELECT * FROM content where identify = '".$inforow['id']."' AND field = '".$rowfields['id']."'");
while($rowcontent = mysql_fetch_array($resultcontent))
{

 

Now my problem is the last selection:

$resultcontent = mysql_query("SELECT * FROM content where identify = '".$inforow['id']."' AND field = '".$rowfields['id']."'");

 

I can get the identify ID, but I have no idea on how I would get the field ID.

Maybe i should execute everything in a while loop when creating the table headers, then also create the rows at the same time, so it finish a header, then the rows below it, then move on to the next header and next rows etc.. Dunno if it's even possible to do it like that?

Link to comment
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.