Hi,

I am bulding my own website and I would like to have a drop down selected menu on there.
I would like to do it, creating a database on mySQL connected to a php file to show the results. However, I am not sure if what I wrote above is right because my acknowledges on this issue are not enough.

Thanks for reading my thread and hope hearing from you soon.


DT

To create a dynamic drop-down menu that extracts information from a database:

<?php 
   // In this example,
   // field1 will be the value you want your form to return.
   // field2 will be the text you want the drop-down menu to show.
   // You can change these accordingly.
   $query = "SELECT field1, field2 FROM MyTable";
   $mysqli = new mysqli('host', 'user', 'password', 'database');
   $result = $mysqli->query($query);
?>
<select>
<?php
   while($row = $result->fetch_assoc())
      echo '<option value="'.$row["field1"].'">'.$row["field2"].'</option>';
?>
</select>

Hi guys!

Thanks for answer so fast.

Firstly, let's answer to everyone with a better explanation.


I already did a search bar with help from pixeldigest.com.

What I would like to know is instance of a search bar, do a drop down menu like this one www.plus2net.com (even a simple one, it should be right for me).

Correct me if I am wrong, but, I consider that to do this, I will need to do a database conected to php file which is sending back from the server, and obviously another php or htm file where is my form and the rest of my website, I guess.

It is quite hard to do this for someone who doen't know too much, however, it will better after all.

Thanks guys


DT

Member Avatar for diafol

SO what exactly do you want?
1. Somebody to write the code for you
2. Somebody to check your code
3. Links to a few resources

No.2 - step right in, the door's open.
No.3 - your ability on Google is probably as good as anybody else.
No.1 - you've come to the wrong forum.

If you want to use a paid application for drop-down menus, i would recommend Likno AllWebMenus.

You can use their API and do what you want easily.

A general PHP code example is on the likno.com website:
http://www.likno.com/addins/dhtml-menu-serverside-example.html

Sorry if you are looking for a free alternative, i cannot suggest one because i went the paid route.

Hi guys,

I wrote below the html code of my drop down menu, ok.
Then, what is missing (it is what I don't know to do) is how to make it work.

So, ardav (thanks for your replies), what I exactly need is how I do work the submit button on my drop down menu. How is the php code in the results php file and how should be the database.

I hope this explanation can help to understand what I want.

<body>
<form method="get" action="">
<table width="250" class="form" id="tablemargin">
<tr><td class="textright"><label for="dropmenu1"><p>dropmenu1</p></label></td>
<td><select class="text" name="dropmenu1">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu2"><p>dropmenu2</p></label></td>
<td><select class="text" name="dropmenu2">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu3"><p>dropmenu3</p></label></td>
<td><select class="text" name="dropmenu3">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu4"><p>dropmenu4</p></label></td>
<td><select class="text" name="dropmenu4">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu5"><p>dropmenu5</p></label></td>
<td><select class="text" name="dropmenu5">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu6"><p>dropmenu6</p></label></td>
<td><select class="text" name="dropmenu6">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu7"><p>dropmenu7</p></label></td>
<td><select class="text" name="dropmenu7">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu8"><p>dropmenu8</p></label></td>
<td><select class="text" name="dropmenu8">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu9"><p>dropmenu9</p></label></td>
<td><select class="text" name="dropmenu9">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td class="textright"><label for="dropmenu10"><p>dropmenu10</p></label></td>
<td><select class="text" name="dropmenu10">
<option selected="selected">Choose one...</option>
<option>Yes</option>
<option>No</option>
<option>I don't care</option>
</select></td></tr>
<tr><td></td><td><p><input type="submit" value="Search" class="button floatright" id="psubmit"></p></td></tr></table>
</form>
</body>

Thank you guys


DT

Member Avatar for diafol

OK, DT, that's more like it.

Here's a sample implementation. The form is sent to page formhandler.php

<form method="post" action="formhandler.php">
<label for = "dd1">Dropdown 1:</label>
<select id="dd1">
 <option value="1">Yes</option>
 <option value="0">No</option>
</select>

<label for = "dd2">Dropdown 2:</label>
<select id="dd2">
 <option value="1">Yes</option>
 <option value="0">No</option>
</select>

<label for = "dd3">Dropdown 3:</label>
<select id="dd3">
 <option value="1">Yes</option>
 <option value="0">No</option>
</select>

<input id="sendDD" name="sendDD" value="Send Form" type="submit" />
</form>

In formhandler.php

<?php
 //include your DB connection details
 $dd1 = 0; $dd2 = 0; $dd3 = 0;

 if($_POST['dd1'] == '1')$dd1 = 1;
 if($_POST['dd2'] == '1')$dd2 = 1;
 if($_POST['dd3'] == '1')$dd3 = 1;

 $r = mysql_query("INSERT INTO table1 SET field1=$dd1,field1=$dd2,field1=$dd3");
 ..etc..
 header("Location:http://www.mysite.com/returnpage.php"); //give the correct url
?>

As you can see the php code above will only do so much. Equally it could run an update query, e.g. "UPDATE table1 SET field1=$dd1,field1=$dd2,field1=$dd3 WHERE id = $idvalue"

You can do this

I'll give you the base method, search w3shools.com for the CSS

Using css styling, to create a drop down menu.

<ul>
<li>Option One</li>
<li>Option Two</li>
</ul>

So, search for w3schools.com to create a css style so that the above option would display as a drop down menu.

Then, as you would like to create a repeated region, lets say, to make it simple, in dreamweaver - simply throw the repeated tags around the li part, something like

<ul>
//php code here
<?php do { ?>
<li><a href="http://link-to-file.html"><?php echo $row_recordset; ?></a></li>
<?php } while xxx = $recordset ?>
</ul>

This is very very very easy.

If you dont get it, let me know

APOLOGIES!!! THATS IS FOR A DHMTL MENU! LOL!!!

But the principle stayts the same, now its even easier.

something like

<select name="xxx" id="xxx">
//PHP CODE HERE
<?php do { ?>
<option value="xxx">xxx</option>
<?php } while xxx = $recordset //this would depend on your sql query!!! ?>
</select>

hey man, i feel bad for you.

here is a complete solution which I've quickly created in dreamweaver, I should not provide this, I guess, cause we try to learn you something, by compiling solutions for yourself. But here it is.

<?php require_once('Connections/conn_name.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_conn_name, $conn_name);
$query_data = "SELECT catID, Title FROM category ORDER BY Title ASC";
$data = mysql_query($query_data, $conn_name) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>Menu
    <select name="menuname" id="menuname">
      <?php
do {  
?>
      <option value="<?php echo $row_data['Title']?>"><?php echo $row_data['Title']?></option>
      <?php
} while ($row_data = mysql_fetch_assoc($data));
  $rows = mysql_num_rows($data);
  if($rows > 0) {
      mysql_data_seek($data, 0);
	  $row_data = mysql_fetch_assoc($data);
  }
?>
    </select>
  </label>
</form>
</body>
</html>
<?php
mysql_free_result($data);
?>

hi guys,

I think that I am getting the idea of this, however, I think that something is not working. I tried everything that everyone said, but, my database is not right.

I have SQL Manager for MySQL to create databases. So, imagine that on my drop down menu you select "yes" it will show after submit one image if you select "no" another different image. My database that I did is like you can see on pixeldigest.com

Thanks


DT

Member Avatar for diafol

The link goes to a webpage. How is it related to your DB structure?

>I think that I am getting the idea of this, however, I think that something is not working. I tried everything that everyone said, but, my database is not right.

So, show your DB structure.

>I have SQL Manager for MySQL to create databases. So, imagine that on my drop down menu you select "yes" it will show after submit one image if you select "no" another different image.

Correct me if I'm wrong - the value of your dropdown will determine the image shown in the next visible page. I can't see the problem:

Say the dropdown is called "dd1" (name and id). On your form page:

<select id="dd1" name="dd1">
 <option value="1">Yes</option>
 <option value="0">No</option>
</select>

In your next page:

<?php
//include your DB connection details
$dd1 = 0;
if($_POST['dd1'] == '1')$dd1 = 1;
//decide whether you need to get the image from the database or if it's hard-coded.
if($dd == 1){
 $imgsrc = "image1.png";
}else{
 $imgsrc = "image2.gif";
} 
?> 
...

<img src="<?php echo $imgsrc;?>" />

Sorry for repeating previous code.

I must admit I thought I knew what you needed, but am a bit perplexed.

I will show what I did below.

search.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search.php</title>
</head>

<body>
<form id="fmsearch" name="fmsearch" method="get" action="/webroot/phptesting/search_result.php">
<table width="200" border="1">
<tr><td><select name="title" id="title">
	<option selected="selected">Choose one...</option>
 	<option value="1">Yes</option>
 	<option value="0">No</option></select></td>
    <td><input type="button" name="Submit" id="Submit" value="Search" /></td></tr>
</table></form>
</body>
</html>

search_result.php

<?php require_once('Connections/search_db.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$title = 0;
$colname_rs83 = "-1";
if ($_POST['title'] == '1') title = 1 {$colname_rs83 = $_POST['title'];}
$colname_rs83 = "-1";
if ($== '1') {$imgsrc = "http://www.pixeldigest.com/images/phpsitesearch/lcd2.gif";}
else{$imgsrc = "http://www.pixeldigest.com/images/phpsitesearch/tv23.gif";} 

mysql_select_db($database_search_db, $search_db);
$query_rs83 = sprintf("SELECT * FROM table1 WHERE tb_name LIKE %s OR tb_desc LIKE %s ", GetSQLValueString("%" . $colname_rs83 . "%", "text"),GetSQLValueString("%" . $colname_rs83 . "%", "text"));
$rs83 = mysql_query($query_rs83, $search_db) or die(mysql_error());
$row_rs83 = mysql_fetch_assoc($rs83);
$totalRows_rs83 = mysql_num_rows($rs83); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search_result.php</title>
</head>

<body>
<img src="<?php echo $imgsrc;?>" />
</body>
</html>
<?php mysql_free_result($rs83); ?>

search_db.php

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_search_db = "localhost";
$database_search_db = "search_db";
$username_search_db = "myone";
$password_search_db = "myone";
$search_db = mysql_pconnect($hostname_search_db, $username_search_db, $password_search_db) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

The database that I am using is the same as pixeldigest.com

Another point to explain to everyone is that I don't want that someone write the code just for copy and paste and I leave. I want to understand the code and if it is necessary to read some resources, I will. To be honest I thought that this should be easy to do.

Thanks


DT

Member Avatar for diafol

Fair enough DT, but the goalposts seem to change with each post.

Originally I thought you wanted to build a dropdown (from the title). Then from reading your first and second posts, it was unclear whether you needed help with the form-structure side or form-handling side of things. Then you mention that your DB isn't right, but provide no info on it, just that pixelwhatever uses the same one. What the exact same one? You share a database? If not, how do you know that your DB has the same structure? This should be invisible to the browser.
You use the yes/no dropdown in your code, but then you have a 'search' dropdown.

Gotta be honest, I've been chasing my tail on this one. I hope some of the others will help you further. Good luck.

hi guys

I may say thanks to ardav for his work with this thread and sorry because I was not able to explain myself.

I am going to explain everything more simple. I wrote below some code where it will show a drop down menu with a submit button. Then, How can I make it work?

<html>
<body>

<select>
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
 <input type="submit" />
</body>
</html>

Thank you


DT

rediculous. just use ajax

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.