admin管理员组文章数量:1406333
I am trying to add a html form to my website that allows for users to select an option from data in my database multiple times, save it as a variable then display the options selected onto the screen.
In my case I am aiming to have the user select 11 players from my database where the position = ST through to where position = GK. And once selected, the form being submitted and then the selected players displayed onto the screen in the same position. I currently have a grid layout setup for the positioning of the player selection and display after. I have multiple players in each position in my database so when selecting a player it would need to allow for all players of that position to be an option. Im sure this is pretty basic but i am new to php and am unsure what the best way to do this would be and could do with some help
I have attempted to do this but recieved errors my code is as follows:
<?php
require_once("../loginPage/includes/db.inc.php");
function displayST(object $pdo, $lastname, $firstname)
{
$query = "SELECT lastname, firstname FROM icons WHERE position = 'ST';";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':firstname', $firstname);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
<html>
<form action="playerView.inc.php">
<select name="ST">
<option value="<?php echo $row['firstname'] . $row['lastname'];?>"><?php echo $row['firstname'] . $row['lastname'];?></option>
</select>
</form>
</html>
<?php }
}
On the main page:
<?php
displayST();
?>
The error was:
Fatal error
: Uncaught ArgumentCountError: Too few arguments to function displayST(), 0 passed in /Applications/MAMP/htdocs/assignment/mainPages/dreamTeam.php on line 97 and at least 1 expected in /Applications/MAMP/htdocs/assignment/mainPages/phpFunctions.inc.php:5
Stack trace:
#0 /Applications/MAMP/htdocs/assignment/mainPages/dreamTeam.php(97): displayST()
#1 {main} thrown in
I have attached mock wireframes to explain better what i am trying to achieve, any guidance is appreciated
I am trying to add a html form to my website that allows for users to select an option from data in my database multiple times, save it as a variable then display the options selected onto the screen.
In my case I am aiming to have the user select 11 players from my database where the position = ST through to where position = GK. And once selected, the form being submitted and then the selected players displayed onto the screen in the same position. I currently have a grid layout setup for the positioning of the player selection and display after. I have multiple players in each position in my database so when selecting a player it would need to allow for all players of that position to be an option. Im sure this is pretty basic but i am new to php and am unsure what the best way to do this would be and could do with some help
I have attempted to do this but recieved errors my code is as follows:
<?php
require_once("../loginPage/includes/db.inc.php");
function displayST(object $pdo, $lastname, $firstname)
{
$query = "SELECT lastname, firstname FROM icons WHERE position = 'ST';";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':firstname', $firstname);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
<html>
<form action="playerView.inc.php">
<select name="ST">
<option value="<?php echo $row['firstname'] . $row['lastname'];?>"><?php echo $row['firstname'] . $row['lastname'];?></option>
</select>
</form>
</html>
<?php }
}
On the main page:
<?php
displayST();
?>
The error was:
Fatal error
: Uncaught ArgumentCountError: Too few arguments to function displayST(), 0 passed in /Applications/MAMP/htdocs/assignment/mainPages/dreamTeam.php on line 97 and at least 1 expected in /Applications/MAMP/htdocs/assignment/mainPages/phpFunctions.inc.php:5
Stack trace:
#0 /Applications/MAMP/htdocs/assignment/mainPages/dreamTeam.php(97): displayST()
#1 {main} thrown in
I have attached mock wireframes to explain better what i am trying to achieve, any guidance is appreciated
Share Improve this question edited Mar 7 at 14:41 ADyson 62.2k16 gold badges79 silver badges92 bronze badges Recognized by PHP Collective asked Mar 5 at 22:56 mclarkmclark 114 bronze badges 19 | Show 14 more comments2 Answers
Reset to default 1To achieve your goal of allowing users to select 11 players from your database and display them on the screen, you can use PHP Form Builder. PHP Form Builder is a powerful tool that simplifies the process of creating forms in PHP. It uses PowerLite PDO (https://github/migliori/power-lite-pdo) as its Database Abstraction Layer (DBAL), which makes it easy to interact with your MySQL database using PDO.
Here's a simplified example inspired by the provided code:
<?php
use phpformbuilder\Form;
use Migliori\PowerLitePdo\Db;
session_start();
include_once 'path/to/phpformbuilder/autoload.php';
$container = require_once 'path/to/power-lite-pdo/bootstrap.php';
$db = $container->get(Db::class);
// create the players html variable
$playersOutput = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('select-players-form') === true) {
// Process form submission
$selectedPlayers = [];
foreach ($_POST as $position => $playerId) {
$selectedPlayers[$position] = $playerId;
}
// Display selected players
$playersOutput = '<h2>Selected Players</h2><ul>';
foreach ($selectedPlayers as $position => $playerId) {
$playerName = $db->selectRow('players', 'name', ['id' => $playerId])->name;
$playersOutput .= '<li>' . $position . ': ' . $playerName . '</li>';
}
$playersOutput .= '</ul>';
}
// Create the form
$form = new Form('select-players-form', 'horizontal', 'novalidate', 'bs5');
$positions = ['ST', 'GK']; // Add other positions as needed
foreach ($positions as $position) {
$players = $db->select('players', 'id, name', ['position' => $position]);
$options = [];
while ($row = $db->fetch()) {
$options[$row->id] = $row->name;
}
$form->addSelect($position, 'Select ' . $position, $options, 'required');
}
$form->addSubmit('submit', 'Submit');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select Players</title>
<link rel="stylesheet" href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css">
<?php $form->printIncludes('css'); ?>
</head>
<body>
<div class="container">
<?php echo $playersOutput; ?>
<h1>Select Players</h1>
<?php $form->render(); ?>
</div>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<?php $form->printIncludes('js'); $form->printJsCode(); ?>
</body>
</html>
This example demonstrates how to create a form that allows users to select players by position and then display the selected players after form submission. You can expand this example to include all positions and customize it further as needed.
Disclaimer: I am the author of PHP Form Builder and PowerLite PDO.
Thanks to help from comments i fixed this error by passing in $pdo into my call statement and including a while loop for my options instead of select and it has come out exactly has hoped for, my code now is:
<?php displayST($pdo)?>
and:
function displayST(object $pdo)
{
$query = "SELECT * FROM icons ;";
$stmt = $pdo->prepare($query);
$stmt->execute();
echo "<select name='ST' class='player' id='ST1'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ){
echo "<option value=echo $row[lastName]' $row[firstName]'>$row[lastName] $row[firstName]</option>";
}
}
本文标签:
版权声明:本文标题:html - What would be the best way to display database data into a select form type and then display it after input using php - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745003810a2637133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$pdo, $lastname, $firstname
arguments when calling the function. – Barmar Commented Mar 6 at 0:22:lastname
or:firstname
parameters. It returns the names of everyone in the table, there's no need for those variables. – Barmar Commented Mar 6 at 0:24