scrubbicus Posted May 4, 2009 Share Posted May 4, 2009 I believed I asked a question like this earlier but I'm still having a hard time figuring out how to do it. I have a table in MySQL like this: Name: Bob Age: 20 Sex: Male E-Mail: bob@gmail.com So I have those values and those fields. I want to be able to loop through this table and automatically generate the columns on the left and the fields on the right. <tr> <td> Name </td> <td> Bob </td> </tr> <tr> <td> Age </td> <td> 20 </td> <tr> </tr> <td> Sex </td> <td> Male </td> </tr> Something like that, each of those fields being generated by a loop and a query. So that I'm able, if I want to, add another field into the table, which automatically will get entered into my form because I'm only having to generate the column name along with an <input type="text"> but when I want them to display I want that to also dynamically generate other then me having to go into the code everytime I add a new column and type it in, like if I wanted to add Date as a column later on. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Can you post the code you have? Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-825340 Share on other sites More sharing options...
scrubbicus Posted May 4, 2009 Author Share Posted May 4, 2009 I really don't have much code right now because none of it is working. The script will be in a public function that's called from outside through a class. I just have a table called people The fields are: Name, Age, Date, Gender The values for it will be added through a form by the user. I want to bring up the table through PHP and through looping through the table so I can see all the details of the users. I don't want to have to include in my loop hardcoded headers Name, Age, Date, and Gender because I want to be able to add a field to the table and it automatically be added to this dynamically generated list. Show somehow I'll be looping through my SHOW COLUMNS FROM people and my SELECT * FROM people WHERE name='$name' in the same loop dynamically generating the HTML table that I can view. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-825357 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Can you work up a code to the extent that you can? Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-825359 Share on other sites More sharing options...
scrubbicus Posted May 4, 2009 Author Share Posted May 4, 2009 Well I have absolutely no clue how I would go about writing the code in the first place, I don't even know where to start other then creating the public function and querying the tables. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-825404 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Well I have absolutely no clue how I would go about writing the code in the first place, I don't even know where to start other then creating the public function and querying the tables. Well, there you go. Start with that. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-825417 Share on other sites More sharing options...
scrubbicus Posted May 4, 2009 Author Share Posted May 4, 2009 Well I did start with that, infact I have that on my script but I've tested out a bunch of ways after that to get my columns and rows dynamically looping. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-826010 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Okay, then can you post what you have and I can try to point out the error or hint you on how to solve this issue? Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-826014 Share on other sites More sharing options...
scrubbicus Posted May 4, 2009 Author Share Posted May 4, 2009 $results = mysql_query('SELECT * FROM '.$table); $num_rows = mysql_num_rows($results); $rows = array(); for($i=1;$i <= $num_rows; $i++) { $rows[$i] = mysql_fetch_array(mysql_query('SELECT * FROM clients WHERE id='.$i.'')); echo '<p/>'; foreach($rows[$i] as $key => $value) { if(!is_int($key)) { if($key == 'id') { echo '<p />'; } else { echo '<b>'.ucwords($key).'</b>: '.$value.'<br/>'; } } } } I think I got it! So if anyone wants something like this then this works for me. I'll mess around with it some more so I get exactly what I want. I just wasn't thinking as indepth as I needed to think. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-826306 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 I would just combine the 2 SQLs into 1. Quote Link to comment https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/#findComment-826307 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.