By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

dynamically displaying all the contents of table

P: 32
I had made a table in a pgsql database which has 4 columns and 4 rows. now I want to display the data of the table in textfields . My table is like this

userid username fname lname
123 anuragpj anurag johari
223 anu anu verma
254 qewdh dqkjh jdnaj
444 hari dfjhj jhdasnb

I m using this php script to store the data in variable.

[PHP]<?
// make our connection
$connection = pg_connect("host=localhost dbname=mydb user=postgres");

// let me know if the connection fails
if (!$connection) {
print("Connection Failed.");
exit;
}

// declare my query and execute
$myresult = pg_exec($connection, "SELECT * FROM users where id>100");

// process results
for ($lt = 0; $lt < pg_numrows($myresult); $lt++) {
$id = pg_result($myresult, $lt, 0);
$username = pg_result($myresult, $lt, 1);
$fname = pg_result($myresult, $lt, 2);
$lname = pg_result($myresult, $lt, 3);
}
?>[/PHP]

can I generate the number of textfields needed automatically? If yes then how can i do this?
Jan 25 '07 #1
Share this Question
Share on Google+
7 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Although I have no PostgreSQL to test, I will show you a reworked version of my MySQL snippet. It calculates the no of fields and starts an output table with each field name in the heading. It then processes all result rows. For each row it displays the values of each field in an output table row.

As I said, I have no pg available, so there could be a typo or error in there. Good luck.[php]// get no of fields
$numflds = pg_num_fields($res);
// start the output table
echo "<table><tr>";
// generate table headings
for ($j = 0; $j < $numflds; $j++) {
$fieldname = pg_field_name($res, $j);
echo '<th>'.$fieldname.'</th>';
}
echo "</tr>";
// process each result row
for ($lt = 0; $lt < pg_numrows($myresult); $lt++) {
// process each result field within a row
echo '<tr>';
for ($i=0; $i<$numflds; $i++)
echo '<td>'.pg_result($myresult, $lt, $i).'</td>';
echo '</tr>';
}
echo "</table>";[/php]
Ronald :cool:
Jan 25 '07 #2

P: 32
Although I have no PostgreSQL to test, I will show you a reworked version of my MySQL snippet. It calculates the no of fields and starts an output table with each field name in the heading. It then processes all result rows. For each row it displays the values of each field in an output table row.

As I said, I have no pg available, so there could be a typo or error in there. Good luck.[php]// get no of fields
$numflds = pg_num_fields($res);
// start the output table
echo "<table><tr>";
// generate table headings
for ($j = 0; $j < $numflds; $j++) {
$fieldname = pg_field_name($res, $j);
echo '<th>'.$fieldname.'</th>';
}
echo "</tr>";
// process each result row
for ($lt = 0; $lt < pg_numrows($myresult); $lt++) {
// process each result field within a row
echo '<tr>';
for ($i=0; $i<$numflds; $i++)
echo '<td>'.pg_result($myresult, $lt, $i).'</td>';
echo '</tr>';
}
echo "</table>";[/php]
Ronald :cool:
if it will work it will display the information in a table but I want that information to be displayed in a textfield so that I can edit and update it.

If u can help it then please reply soon.
Jan 26 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
if it will work it will display the information in a table but I want that information to be displayed in a textfield so that I can edit and update it.

If u can help it then please reply soon.
Then make each table cell an input field by using the <input name=..... /> in that cell.

Ronald :cool:
Jan 26 '07 #4

P: 32
Then make each table cell an input field by using the <input name=..... /> in that cell.

Ronald :cool:
can u explain it with an example. Please
Jan 27 '07 #5

ronverdonk
Expert 2.5K+
P: 4,258
Now this question goes far beyond your original post. That was about dynamically displaying columns of a result set for a PostGreSQL table.

What you are asking now is something completely different, i.e. now you want to turn this display into a full fledged grid with edit and save options. From the ground up!
That goes beyond the scope of this thread and even this forum. Not mentioning the huge amount of new lines of code developed.

I will show you the basic functions you have to think of.
- enclose the shown table in a form
- make each cell in the table an input field with its own unique name (or ID if you are using JS)
- let the user submit the form when completed
- write the code to capture all changed fields in the form after submit
- update each row in the table with the changed fields

But I must warn you that this is not a simple task, updateable grids are relatively difficult.

A better solution for you would be to look into the use of an administration tool for PostGreSQL. Those tools usually have grid capabilities.

Ronald :cool:
Jan 28 '07 #6

cassbiz
100+
P: 202
This almost sounds like a case where you would want to use AJAX. If you do display it in a text fields, then when the field is changed it will update automatically. Otherwise you may be attempting to re-invent the wheel.

Good Luck
Jan 29 '07 #7

P: 32
Now this question goes far beyond your original post. That was about dynamically displaying columns of a result set for a PostGreSQL table.

What you are asking now is something completely different, i.e. now you want to turn this display into a full fledged grid with edit and save options. From the ground up!
That goes beyond the scope of this thread and even this forum. Not mentioning the huge amount of new lines of code developed.

I will show you the basic functions you have to think of.
- enclose the shown table in a form
- make each cell in the table an input field with its own unique name (or ID if you are using JS)
- let the user submit the form when completed
- write the code to capture all changed fields in the form after submit
- update each row in the table with the changed fields

But I must warn you that this is not a simple task, updateable grids are relatively difficult.

A better solution for you would be to look into the use of an administration tool for PostGreSQL. Those tools usually have grid capabilities.

Ronald :cool:
I m asking that how to make each table cell an input field. please give a small example related this.
Jan 29 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.