Building an edit/update database form - adding column
I am working on an update database record form. The data is fetched from db table and populated into an html table on the webpage. I am stuck now how to add another column (5th) in the table that will contains 'edit' href link for updating database script.
Like:
|Col1|Col2|Col3|Col4|Edit|
| -- | -- | -- | -- |href|
I know this is very basic question, but so far I am not able to solve it.
It will be very helpful if someone can help me to work it.
Here is my code:
<div id="update" class="col mb-4">
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid grey;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "n";
}
}
echo "<table class='table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>Col1</th><th>Col2<th>Col3</th><th>xCol4</th></tr>";
echo "</thead>";
echo "<tbody>";
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// $conn = null;
echo "</tbody>";
echo "</table>";
?>
</div>
php html
add a comment |
I am working on an update database record form. The data is fetched from db table and populated into an html table on the webpage. I am stuck now how to add another column (5th) in the table that will contains 'edit' href link for updating database script.
Like:
|Col1|Col2|Col3|Col4|Edit|
| -- | -- | -- | -- |href|
I know this is very basic question, but so far I am not able to solve it.
It will be very helpful if someone can help me to work it.
Here is my code:
<div id="update" class="col mb-4">
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid grey;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "n";
}
}
echo "<table class='table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>Col1</th><th>Col2<th>Col3</th><th>xCol4</th></tr>";
echo "</thead>";
echo "<tbody>";
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// $conn = null;
echo "</tbody>";
echo "</table>";
?>
</div>
php html
What'snew TableRows
? And what's up with the usage ofRecursiveArrayIterator
?
– Magnus Eriksson
Nov 21 '18 at 13:17
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new<td>
. Looks like a very complicated way of doing something very simple.
– Magnus Eriksson
Nov 21 '18 at 14:52
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
1
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).
– Magnus Eriksson
Nov 21 '18 at 15:22
add a comment |
I am working on an update database record form. The data is fetched from db table and populated into an html table on the webpage. I am stuck now how to add another column (5th) in the table that will contains 'edit' href link for updating database script.
Like:
|Col1|Col2|Col3|Col4|Edit|
| -- | -- | -- | -- |href|
I know this is very basic question, but so far I am not able to solve it.
It will be very helpful if someone can help me to work it.
Here is my code:
<div id="update" class="col mb-4">
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid grey;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "n";
}
}
echo "<table class='table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>Col1</th><th>Col2<th>Col3</th><th>xCol4</th></tr>";
echo "</thead>";
echo "<tbody>";
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// $conn = null;
echo "</tbody>";
echo "</table>";
?>
</div>
php html
I am working on an update database record form. The data is fetched from db table and populated into an html table on the webpage. I am stuck now how to add another column (5th) in the table that will contains 'edit' href link for updating database script.
Like:
|Col1|Col2|Col3|Col4|Edit|
| -- | -- | -- | -- |href|
I know this is very basic question, but so far I am not able to solve it.
It will be very helpful if someone can help me to work it.
Here is my code:
<div id="update" class="col mb-4">
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid grey;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "n";
}
}
echo "<table class='table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>Col1</th><th>Col2<th>Col3</th><th>xCol4</th></tr>";
echo "</thead>";
echo "<tbody>";
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// $conn = null;
echo "</tbody>";
echo "</table>";
?>
</div>
php html
php html
edited Nov 21 '18 at 14:48
Magnus Eriksson
7,21641327
7,21641327
asked Nov 21 '18 at 13:11
XCeptableXCeptable
59141434
59141434
What'snew TableRows
? And what's up with the usage ofRecursiveArrayIterator
?
– Magnus Eriksson
Nov 21 '18 at 13:17
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new<td>
. Looks like a very complicated way of doing something very simple.
– Magnus Eriksson
Nov 21 '18 at 14:52
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
1
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).
– Magnus Eriksson
Nov 21 '18 at 15:22
add a comment |
What'snew TableRows
? And what's up with the usage ofRecursiveArrayIterator
?
– Magnus Eriksson
Nov 21 '18 at 13:17
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new<td>
. Looks like a very complicated way of doing something very simple.
– Magnus Eriksson
Nov 21 '18 at 14:52
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
1
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).
– Magnus Eriksson
Nov 21 '18 at 15:22
What's
new TableRows
? And what's up with the usage of RecursiveArrayIterator
?– Magnus Eriksson
Nov 21 '18 at 13:17
What's
new TableRows
? And what's up with the usage of RecursiveArrayIterator
?– Magnus Eriksson
Nov 21 '18 at 13:17
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new
<td>
. Looks like a very complicated way of doing something very simple.– Magnus Eriksson
Nov 21 '18 at 14:52
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new
<td>
. Looks like a very complicated way of doing something very simple.– Magnus Eriksson
Nov 21 '18 at 14:52
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
1
1
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use
<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).– Magnus Eriksson
Nov 21 '18 at 15:22
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use
<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).– Magnus Eriksson
Nov 21 '18 at 15:22
add a comment |
1 Answer
1
active
oldest
votes
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
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%2f53412814%2fbuilding-an-edit-update-database-form-adding-column%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
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
add a comment |
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
add a comment |
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'><a href="some-path-somewhere/">Edit</a></td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.
answered Nov 21 '18 at 15:38
Magnus ErikssonMagnus Eriksson
7,21641327
7,21641327
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
add a comment |
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
Can you please put a link to some example/guide of how to split code into probably MVC format, as you mentioned moving database query to other file.
– XCeptable
Nov 21 '18 at 17:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
There isn't one single way of doing that. There are many so you need to do some research and see what way fits you the best. I would recommend looking into some framework. No reason to reinvent the wheel. There are plenty to choose from. Do some research, test some and use the one that fits you the best.
– Magnus Eriksson
Nov 21 '18 at 20:58
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%2f53412814%2fbuilding-an-edit-update-database-form-adding-column%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
What's
new TableRows
? And what's up with the usage ofRecursiveArrayIterator
?– Magnus Eriksson
Nov 21 '18 at 13:17
new TableRows is the instance of class defined above that extends RecursiveIteratorIterator. How to use RecursiveIteratorIterator to add another column ? I have been using it to implement the results fetched from the db only.
– XCeptable
Nov 21 '18 at 13:54
The indentation was off so I missed that. I'm actually having a hard time understanding the point of that class at all? I'm probably missing something, but why don't you simply do a foreach or while loop on the result and output the HTML in the loop? Then it would have been super simple to add a new
<td>
. Looks like a very complicated way of doing something very simple.– Magnus Eriksson
Nov 21 '18 at 14:52
I agree. I started using this as a starter template from w3schools and keep using it because I did not find a need to re-consider earlier.
– XCeptable
Nov 21 '18 at 15:12
1
Try and avoid outputting HTML using PHP if you can. It's usually better to make a loop, end the PHP block, write your HTML and use
<?= $somevariable ?>
when you need to output something. It makes the code cleaner and IDE's can deal with syntax highlighting much better (which makes it easier to spot issues).– Magnus Eriksson
Nov 21 '18 at 15:22