Jump to content

Php Dropdown Form & Dynamic Site


pmhabo
 Share

Recommended Posts

I'm a complete newb when it comes to websites, so I need some help with this more complicated stuff.What I have so far: A database with about 60 rows of data with 10 columns each.For example something like this:A B C D E F1 2 3 4 5 67 8 9 10 11 12What I am trying to have: A page on my website that has a dropdown form to select one of the rows of data by just using column "A" data (1,7, etc...). After the row is selected a new page is generated based on the data in that selected row.EX: -Form dropdown box lists 1, 7, etc... (from column A)-User selects "7" from dropdown-website is generated using all data (7,8,9,10,11,12) from row 7I don't even know where to start. I'm guessing connecting to the database, but I don't even know how to do that.Can someone walk be through the steps?Thanks!

Link to comment
Share on other sites

Is your database MySQL? If so, are you able to use MySQLi commands? Which columns are unique/primary keys? These are all relevant factors. If you are on MySQL and can use MySQLi (that's what I've learned and I'm not sure how close regular MySQL commands are in PHP), and you want to then this is the sort of thing you would want:First a form with a drop down menu that sends the selection to a php script, called show_info.php:

<form method="get" action="show_info.php"><fieldset><label for="selection">Select record to view:</label><?php$mysqli = mysqli_connect('hostname','username','password','database');$rows_sql = "SELECT id, COUNT(id) FROM $table";$rows_res = mysqli_query($mysqli, $rows_sql) or die(mysqli_error($mysqli));$info = mysqli_fetch_array($rows_res);$rows = info["COUNT(id)"];if($rows > 0) {echo '<select name="selection">';while($row = mysqli_fetch_array($rows_res)) {echo '<option value=$info["id"]>$info["id"]</option>'}echo '</select>';} else if($rows == 0) {echo 'No records to show';}?><input type="submit" value="go" /></fieldset></form>

Then show_info.php might look like:

<?phpif(!$_POST) {//someone has entered the URL directly, send them awayheader("Location: index.html");exit();}$mysqli = "mysqli_connect('hostname','username','password','database');$selection = htmlspecialchars($_GET["selection"]);$sql = 'SELECT * FROM $table WHERE id="$selection"';$sql_res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));$display_block = "";while($info = mysqli_fetch_array($sql_res)) {$col_a = $info["col_a"];$col_b = $info["col_b"];...etc...$display_block .="Col a: $col_a \n";}echo $display_block;?>

That's the general idea...I hope that helps some.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...