Jump to content

Dynamically generating columns and rows


scrubbicus

Recommended Posts

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: [email protected]

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/156734-dynamically-generating-columns-and-rows/
Share on other sites

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.

 

 

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.