sql server - Multiple drop menus with one PHP script -
website has several pages forms. many of have drop menus. write 1 php script populate multiple drop menus. i'm including code have far, don't think i'm on right track here.
order.php
<?php include 'functionsfile.php'; ?> <form method="post" action="order.php"> <select name="order_status" id="order_status"> <?php foreach ($data $row): ?> <option><?=$row["order_status"]?></option> <?php endforeach ?> </select> <!-- 3 more drop menus --> </form> <?php $table = array('order_status', 'customer', 'warehouse_id', 'order_description'); fillform($conn, $table); ?>
functionsfile.php
<?php require 'databaseconnection.php'; function fillform($conn, $table) { foreach ($table $menu) { $query = 'select * $table'; $smt = $conn->prepare($query); $smt->execute(); $data = $smt->fetchall(); } }
additional information
i wondering if have add parameter fillform() function knows field names well. alternatively, querying tables have 1 pertinent data field, bad practice name (one important) data field same table?
i'm not sure how control output of going on in code. if should add second array parameters, i'll totally lost.
i see few errors in code, these fixes out. first of all, functionsfile.php
file called after try use data generates. needs happen before try use $data
variable. secondly, fillform()
function doesn't return value. instead, creating scoped variable not accessible outside of function. try rewriting file functionsfile.php
this:
<?php require_once ('databaseconnection.php'); // no need pass in connection information if file 1 loading , it's not scoped. function fillform($table) { $query = "select * $table"; $smt = $conn->prepare($query); $smt->execute(); $rows = $smt->fetchall(); return $rows; }
now, in order.php
file, can load file in , use function's returned data after executes so:
<?php require_once ('functionsfile.php'); ?> <form method="post" action=""><!-- can leave action blank if posting same file. --> <select name="order_status" id="order_status"> <?php $rows = fillform('order_status'); foreach ($rows $row) { echo '<option value="' . $row['order_status'] . '">' . $row['order_status'] . '</option>'; } ?> </select> <select name="customer" id="customer"> <?php $rows = fillform('customer'); foreach ($rows $row) { echo '<option value="' . $row['customer'] . '">' . $row['customer'] . '</option>'; } ?> </select> <!-- next groups, etc. --> </form>
this allow reuse fillform()
function as need passing in table name , give more control on how want data displayed on per table basis.
for other questions,
you don't need pass in field names if selecting everything. if don't mind grabbing columns, allow access need outside of
fillform()
function. naming conventions, wouldn't name column identically table. instead, give table broad, plural name, , name column column storing. e.g. table -> customers | column1 -> id | column2 -> namei'm not sure how answer one. make sure understand writing before try use it. mean output of function, or output of html php?
Comments
Post a Comment