Jump to content

Drop Down Menu with SQL?


lordphate

Recommended Posts

okay So i'm trying to make a script where if a user clicks a link on the drop-down menu it would show all the files in that specified category(listed in MYSQL) ... how would i go about doing that i have the menu set up as:
[code]<FORM NAME="form">
<SELECT NAME="categories" size=1 onChange="javascript:jumpBox()">
<OPTION VALUE="">Go to....
<OPTION VALUE="movie">Movies
<OPTION VALUE="Audio">Audio
<OPTION VALUE="White Papers">White Papers
<OPTION VALUE="hacking">Hacking
<OPTION VALUE="Windows">Windows
<OPTION VALUE="AllinOnes">All in Ones
<OPTION VALUE="Applications">Applications
</SELECT>
</FORM>[/code] 

would i do an IF ($_GET('Audio')) { Show files here } elseif {$_GET('Movies') { Show Files } etc?
Link to comment
https://forums.phpfreaks.com/topic/13452-drop-down-menu-with-sql/
Share on other sites

On this page i will give example using javascript and php to generate
dynamic options in select box

we assume we have
table city
and table country

example structure:
CREATE TABLE `country` (
`id` bigint(20) NOT NULL auto_increment,
`kodenegara` varchar(20) default NULL,
`namanegara` varchar(20) default NULL,
KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;


INSERT INTO `city` VALUES (1, 1, 'jkt', 'jakarta');
INSERT INTO `city` VALUES (2, 1, 'sby', 'surabaya');
INSERT INTO `city` VALUES (3, 2, 'ny', 'newyork');
INSERT INTO `city` VALUES (4, 2, 'la', 'los angeles');

after we have the table
we have to create the script ofcourse :)

below this you have to put between <head></head>
<script language="javascript" type="text/javascript">
<!--
var store = new Array();
<?php
// Connecting, selecting database
$link = mysql_connect('host', 'user', 'pass')
or die('Could not connect: ' . mysql_error());
mysql_select_db('database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM city';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$iC=0;
$varsemen="";
$temp=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$negaraid=$line["negaraid"];
$kodekota=$line["kodekota"];
$namakota=$line["namakota"];

if ($temp!=$negaraid) {
if ($iC!=0) {

$varsemen=substr($varsemen,1,-1);
$varsemen.=");";

}

$temp=$negaraid;
$varsemen.= " store[$negaraid] = new Array(
'$namakota','$kodekota',";

} else {

//echo "

//'$kodekota','$namakota',";
$varsemen.="
'$namakota','$kodekota',";

}

$iC++;
}
$varsemen=substr($varsemen,0,-1);
$varsemen.=");";
echo $varsemen;


?>




function init()
{
optionTest = true;
lgth = document.forms[0].second.options.length - 1;
document.forms[0].second.options[lgth] = null;
if (document.forms[0].second.options[lgth]) optionTest = false;
}


function populate()
{
if (!optionTest) return;
var box = document.forms[0].first;
var number = box.options[box.selectedIndex].value;
if (!number) return;
var list = store[number];
var box2 = document.forms[0].second;
box2.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box2.options[i/2] = new Option(list[i],list[i+1]);
}
}

function go()
{
if (!optionTest) return;
box = document.forms[0].second;
destination = box.options[box.selectedIndex].value;
if (destination && confirm('Do you really want to go to this site?')) top.location.href = destination;
}
init();
// -->

</script>

and now we need something in the body

<form action="kedua.php" method="POST">
<select name="first" onchange="populate()">
echo "<option value=0>---</option>";

<?php


// Performing SQL query
$query = 'SELECT * FROM n_negara';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$iC=0;
$varsemen="";
$temp=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$idnegara=$line["id"];
$kodenegara=$line["kodenegara"];
$namanegara=$line["namanegara"];

echo "<option value=$idnegara>$namanegara</option>";
}
?>

</select>
<br />

<select name="second" >
<option>No second level yet</option>
<option>Your browser can't handle this script</option>
</select>

<input type=submit name=submit value=submit>
</form>
</center>

my category entry is in a already table upload ... This is what i have..but its saying I have error...
[code]<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{  include './library/db.php';

$id = $_GET['id'];
$query = "SELECT name, type, size, path, uploader, category FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($filename, $type, $size, $filePath, $uploader, $category) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-length: $size");
header("Content-type: $type");

readfile($filePath);

exit;
}



include 'header.php';
include 'sidebar.php'; include './library/sessions.php';
include './library/db.php';
?>
<div id="main">
<html>
<head>
<title>Download File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head> <body> <div id="main">
<!-- Copy and Paste this code into the HEAD of your HTML document -->

<SCRIPT LANGUAGE="JavaScript">
<!-- This Drop Down Menu was made at www.CodeSpy.com -->

function jumpBox(form){
var URL = document.form.site.options[document.form.site.selectedIndex].value;
window.location.href = URL;
}

</SCRIPT>
  </head><body> <div id="main">
  <!-- Copy and Paste this code into the BODY of your HTML document -->

<FORM NAME="categories">
<SELECT NAME="categories" size=1 onChange="javascript:jumpBox()">
<OPTION VALUE="">Go to....
<OPTION VALUE="movie">Movies
<OPTION VALUE="Audio">Audio
<OPTION VALUE="WhitePapers">White Papers
<OPTION VALUE="hacking">Hacking
<OPTION VALUE="Windows">Windows
<OPTION VALUE="AllinOnes">All in Ones
<OPTION VALUE="Applications">Applications
</SELECT>
<input type=button value="Go!" onClick="javascript:jumpBox(this)">
</FORM>
<?php
$category = $_POST('categories');
$query  = "SELECT id, name, size, uploader FROM upload where category = '$category'";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "No Files to Download...Be the first Uploader! <br>";
}
else
{
    while(list($fileid, $filename, $size, $uploader) = mysql_fetch_array($result))
    {
?>
    <a href="search.php?id=<?=$fileid;?>"><?=$filename;?></a> - <?=$uploader;?> <?=$category;?> <?=$size;?> Bytes  <br>
<?php
    }

  include 'footer.php';  }
?>
</body>
</html>  </div>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.