zero_ZX Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/ Share on other sites More sharing options...
The Letter E Posted January 31, 2011 Share Posted January 31, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/#findComment-1168038 Share on other sites More sharing options...
zero_ZX Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/#findComment-1168236 Share on other sites More sharing options...
denno020 Posted February 1, 2011 Share Posted February 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/#findComment-1168241 Share on other sites More sharing options...
zero_ZX Posted February 1, 2011 Author Share Posted February 1, 2011 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 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? Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/#findComment-1168245 Share on other sites More sharing options...
The Letter E Posted February 1, 2011 Share Posted February 1, 2011 You might want to check out the LEFT JOIN option. It's slightly more confusing than most SQL but I think it might be the cure for your problem. I'm still unclear as to exactly what you want to achieve but give it a shot. I found this tizag tutorial that may help: http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php Good luck, E Quote Link to comment https://forums.phpfreaks.com/topic/226275-custom-table-fields-rows/#findComment-1168411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.