dynamic table base on database field
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Help me, i got stack when i want to make a dynamic table,
so the idea of the program is we can be costume a select field of database and can show it.
so if we have this field on table in database :
- No
- Name
- age
Address
and we just want to show Name and Address, so i save the rule on the database (SELECT Name, Address)
but the problem is on the dynamic table when i show, how to make dynamic table when field always change (Example :Maybe the field just Age, or Name and age or we show all #its just from Configure that i make)
i have tried to make query result into an array and show it like this.
<table>
<?php for($x=0;$x<$length_array;$x++){ ?>
<tr>
<?php for($y=0;$y<$width_array;$y++){ ?>
<td><?php $result[$length_array][$width_array] ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
note : variable $result is from result query and i change it into an array, but the problem is i cann't count a length and width from array in this code.
php codeigniter
|
show 5 more comments
Help me, i got stack when i want to make a dynamic table,
so the idea of the program is we can be costume a select field of database and can show it.
so if we have this field on table in database :
- No
- Name
- age
Address
and we just want to show Name and Address, so i save the rule on the database (SELECT Name, Address)
but the problem is on the dynamic table when i show, how to make dynamic table when field always change (Example :Maybe the field just Age, or Name and age or we show all #its just from Configure that i make)
i have tried to make query result into an array and show it like this.
<table>
<?php for($x=0;$x<$length_array;$x++){ ?>
<tr>
<?php for($y=0;$y<$width_array;$y++){ ?>
<td><?php $result[$length_array][$width_array] ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
note : variable $result is from result query and i change it into an array, but the problem is i cann't count a length and width from array in this code.
php codeigniter
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02
|
show 5 more comments
Help me, i got stack when i want to make a dynamic table,
so the idea of the program is we can be costume a select field of database and can show it.
so if we have this field on table in database :
- No
- Name
- age
Address
and we just want to show Name and Address, so i save the rule on the database (SELECT Name, Address)
but the problem is on the dynamic table when i show, how to make dynamic table when field always change (Example :Maybe the field just Age, or Name and age or we show all #its just from Configure that i make)
i have tried to make query result into an array and show it like this.
<table>
<?php for($x=0;$x<$length_array;$x++){ ?>
<tr>
<?php for($y=0;$y<$width_array;$y++){ ?>
<td><?php $result[$length_array][$width_array] ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
note : variable $result is from result query and i change it into an array, but the problem is i cann't count a length and width from array in this code.
php codeigniter
Help me, i got stack when i want to make a dynamic table,
so the idea of the program is we can be costume a select field of database and can show it.
so if we have this field on table in database :
- No
- Name
- age
Address
and we just want to show Name and Address, so i save the rule on the database (SELECT Name, Address)
but the problem is on the dynamic table when i show, how to make dynamic table when field always change (Example :Maybe the field just Age, or Name and age or we show all #its just from Configure that i make)
i have tried to make query result into an array and show it like this.
<table>
<?php for($x=0;$x<$length_array;$x++){ ?>
<tr>
<?php for($y=0;$y<$width_array;$y++){ ?>
<td><?php $result[$length_array][$width_array] ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
note : variable $result is from result query and i change it into an array, but the problem is i cann't count a length and width from array in this code.
php codeigniter
php codeigniter
edited Nov 26 '18 at 20:33
coba coba
asked Nov 26 '18 at 20:29
coba cobacoba coba
93
93
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02
|
show 5 more comments
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02
|
show 5 more comments
1 Answer
1
active
oldest
votes
It depends a bit on how your result set is returned, but based on your code above I'll assume your result is an array of arrays.
First think about what you want: you want an html table with a header that gives the name for each column, and then has a row for each record in your result set.
I very much dislike mixing php and html, so I will use a different syntax style, but the steps are the same no matter what style you use. This way makes the logic a lot easer to read.
First, make the table.
<?php
$html = '<table>';
Now you want to add a header to the table and create a cell for each column in your result. Before you do that, you need to answer: where are you going to get the names for you columns? There are two possible answers; 1) directly from the results of your query, or 2) hard-code them.
Getting them directly from the query results is much more flexible, but the names you give your database columns may not always be human-friendly.
$columnNames = ['Name', 'Address'];
---- OR ----
$firstRow = $result[0]; // of course we have checked that the result set is not empty!
$columnNames = array_keys($firstRow); // adjust if rows are objects instead of arrays
Now output the header:
$html .= '<thead>';
foreach($columnNames as $columnName) {
$html .= '<th>' . $columnName . '</th>';
}
$html .= '</thead>';
Now we can move on to the body, as in the answer to your previous question, creating a row in the table for each item in your result set. For each row you have an inner loop that creates the markup for each cell.
$html .= '<tbody>';
foreach($result as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
print $html;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488570%2fdynamic-table-base-on-database-field%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It depends a bit on how your result set is returned, but based on your code above I'll assume your result is an array of arrays.
First think about what you want: you want an html table with a header that gives the name for each column, and then has a row for each record in your result set.
I very much dislike mixing php and html, so I will use a different syntax style, but the steps are the same no matter what style you use. This way makes the logic a lot easer to read.
First, make the table.
<?php
$html = '<table>';
Now you want to add a header to the table and create a cell for each column in your result. Before you do that, you need to answer: where are you going to get the names for you columns? There are two possible answers; 1) directly from the results of your query, or 2) hard-code them.
Getting them directly from the query results is much more flexible, but the names you give your database columns may not always be human-friendly.
$columnNames = ['Name', 'Address'];
---- OR ----
$firstRow = $result[0]; // of course we have checked that the result set is not empty!
$columnNames = array_keys($firstRow); // adjust if rows are objects instead of arrays
Now output the header:
$html .= '<thead>';
foreach($columnNames as $columnName) {
$html .= '<th>' . $columnName . '</th>';
}
$html .= '</thead>';
Now we can move on to the body, as in the answer to your previous question, creating a row in the table for each item in your result set. For each row you have an inner loop that creates the markup for each cell.
$html .= '<tbody>';
foreach($result as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
print $html;
add a comment |
It depends a bit on how your result set is returned, but based on your code above I'll assume your result is an array of arrays.
First think about what you want: you want an html table with a header that gives the name for each column, and then has a row for each record in your result set.
I very much dislike mixing php and html, so I will use a different syntax style, but the steps are the same no matter what style you use. This way makes the logic a lot easer to read.
First, make the table.
<?php
$html = '<table>';
Now you want to add a header to the table and create a cell for each column in your result. Before you do that, you need to answer: where are you going to get the names for you columns? There are two possible answers; 1) directly from the results of your query, or 2) hard-code them.
Getting them directly from the query results is much more flexible, but the names you give your database columns may not always be human-friendly.
$columnNames = ['Name', 'Address'];
---- OR ----
$firstRow = $result[0]; // of course we have checked that the result set is not empty!
$columnNames = array_keys($firstRow); // adjust if rows are objects instead of arrays
Now output the header:
$html .= '<thead>';
foreach($columnNames as $columnName) {
$html .= '<th>' . $columnName . '</th>';
}
$html .= '</thead>';
Now we can move on to the body, as in the answer to your previous question, creating a row in the table for each item in your result set. For each row you have an inner loop that creates the markup for each cell.
$html .= '<tbody>';
foreach($result as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
print $html;
add a comment |
It depends a bit on how your result set is returned, but based on your code above I'll assume your result is an array of arrays.
First think about what you want: you want an html table with a header that gives the name for each column, and then has a row for each record in your result set.
I very much dislike mixing php and html, so I will use a different syntax style, but the steps are the same no matter what style you use. This way makes the logic a lot easer to read.
First, make the table.
<?php
$html = '<table>';
Now you want to add a header to the table and create a cell for each column in your result. Before you do that, you need to answer: where are you going to get the names for you columns? There are two possible answers; 1) directly from the results of your query, or 2) hard-code them.
Getting them directly from the query results is much more flexible, but the names you give your database columns may not always be human-friendly.
$columnNames = ['Name', 'Address'];
---- OR ----
$firstRow = $result[0]; // of course we have checked that the result set is not empty!
$columnNames = array_keys($firstRow); // adjust if rows are objects instead of arrays
Now output the header:
$html .= '<thead>';
foreach($columnNames as $columnName) {
$html .= '<th>' . $columnName . '</th>';
}
$html .= '</thead>';
Now we can move on to the body, as in the answer to your previous question, creating a row in the table for each item in your result set. For each row you have an inner loop that creates the markup for each cell.
$html .= '<tbody>';
foreach($result as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
print $html;
It depends a bit on how your result set is returned, but based on your code above I'll assume your result is an array of arrays.
First think about what you want: you want an html table with a header that gives the name for each column, and then has a row for each record in your result set.
I very much dislike mixing php and html, so I will use a different syntax style, but the steps are the same no matter what style you use. This way makes the logic a lot easer to read.
First, make the table.
<?php
$html = '<table>';
Now you want to add a header to the table and create a cell for each column in your result. Before you do that, you need to answer: where are you going to get the names for you columns? There are two possible answers; 1) directly from the results of your query, or 2) hard-code them.
Getting them directly from the query results is much more flexible, but the names you give your database columns may not always be human-friendly.
$columnNames = ['Name', 'Address'];
---- OR ----
$firstRow = $result[0]; // of course we have checked that the result set is not empty!
$columnNames = array_keys($firstRow); // adjust if rows are objects instead of arrays
Now output the header:
$html .= '<thead>';
foreach($columnNames as $columnName) {
$html .= '<th>' . $columnName . '</th>';
}
$html .= '</thead>';
Now we can move on to the body, as in the answer to your previous question, creating a row in the table for each item in your result set. For each row you have an inner loop that creates the markup for each cell.
$html .= '<tbody>';
foreach($result as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
print $html;
answered Nov 27 '18 at 0:30
JerryJerry
2,34211324
2,34211324
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488570%2fdynamic-table-base-on-database-field%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please not use blockquotes? Edit: thank you.
– Funk Forty Niner
Nov 26 '18 at 20:31
Is this related to your previous question ?
– Funk Forty Niner
Nov 26 '18 at 20:33
@FunkFortyNiner done
– coba coba
Nov 26 '18 at 20:34
@FunkFortyNiner yes it is, but i still cann't find an answer.
– coba coba
Nov 26 '18 at 20:35
the answer given on your previous question is the way to solve your problem.
– Jhecht
Nov 26 '18 at 21:02