Jump to content
Larry Ullman's Book Forums

Dropdown Menu Function Pulling From Sql Db


Recommended Posts

I have been struggling for several days with this and I know its gonna be a simple solution or somehow I am not seeing the problem.

 

I have a drop Down Menu (Column AP) at http://aguy.us/tsw/admin/AbilityInsert.php which I am trying to call a Function dropdownmenu.

 

In trying to condense the code on the AbilityInsert.php page I realized I could take the repeated code for each of the Drop downs and use a function.. Currently all the menus work on the page with the exception of the AP Column, which is my Function Dropdownmenu experiment. The others are hard coded into the page until I get the function working properly.

 

The AP drop down is utilizing the Function dropdownmenu below with 5 variables. The first 3 variables seem to work as intended. The drop box title is AP fine and the name of the select is appropriately "ID_AP"

 

Inspect Element return

<td>AP<br> <select size="1" name="ID_AP">
<option selected="selected"></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select></td>

 

but if I use $option1 and $option2 in the following line of the function

 

print "<OPTION VALUE={$row['".$option1."']}>{$row['".$option2."']}</option>";

 

the Drop down returns empty but with the appropriate amount of iterations just blank.

 

If I change that line and hard code the line with the following

 

 

print "<OPTION VALUE={$row['ID_AP']}>{$row['ActPass']}</option>";

 

 

the drop box returns all the AP table contents for the appropriate rows.

 

If I use the hard coded line It defeats the purpose of the function.. To reduced repeated code. Since i have 9 of these drop downs on this page alone all pulling from separate SQl tables it would be awesome if I could just call a function with differing variables for each drop-down.

 

 

Function being called in the page with its variables:

dropdownmenu(AP, ActPass, ID_AP, 'ID_AP','ActPass');

 

 

Function Code:

//function that returns drop down menus from variious sql tables 
function dropdownmenu($table, $ordby, $n, $option1, $option2 ) {
//.$table. DROP DOWN
$sql2 = 'SELECT * FROM `'.$table.'` ORDER BY `'.$table.'`.`'.$ordby.'` ASC LIMIT 0, 30 ';
$result=mysql_query($sql2) or die(mysql_error()); 
if($r =mysql_Query($sql2)) {//run Query
$i=0;
//Retrieve Records and Print .$table. Drop down box
print "<td>".$table."</br> <SELECT SIZE='1' NAME= '".$n."'>";
print "<option selected=\"selected\"></option>";
while (($row =mysql_fetch_array($result)) !==False){
$i++;
print "<OPTION VALUE={$row['".$option1."']}>{$row['".$option2."']}</option>";
}
print "</SELECT></td>";
} 
}

 

So where has my function theory broken down. I am really stumped and have tried many variations in syntax, I have googled but apparently my google foo is not, I have pounded my head on my table and well I am apparently just now seeing my incorrect coding....

 

 

 

Help!

Link to post
Share on other sites

<?php

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'mypass');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'forum2');


$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );


mysqli_set_charset($dbc, 'utf8');



$q = "SELECT * FROM categories";
$r = mysqli_query($dbc,$q);

if($r)
{
echo "<select name=\"category\">\n";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
	echo "<option value=\"{$row['cat_id']}\">{$row['name']}</option>\n";
}
}

?>

 

Page Source

 

<select name="category">
<option value="1">Larry's Books</option>
<option value="2">Larry's Coffee</option>
<option value="3">Larry's Pets</option>
<option value="4">Larry's Cars</option>
<option value="5">Larry's Houses</option>

 

I like the background on your website, is that from Medal of Honor or Call of Duty?

 

Okay i just made the code above for populating a drop down menu, if you check my syntax against yours that should help you with your function. I am also using the mysqli improved connection, but whichever you use it doesn't matter for this example.

Link to post
Share on other sites

Okay i done a function for the drop down similar to yours hope this helps. I had to include the mysql connection inside the function otherwise $dbc was an undefined variable.

 

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'mypass');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'forum2');

function dropdownmenu($table, $name, $column1, $column2 )
{


$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' .	mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');

$q = "SELECT * FROM " . $table;
$r = mysqli_query($dbc,$q);

if($r)
{
	echo "<select name=\"" . $name . ">\n";
	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
	{
		echo "<option value=\"{$row[$column1]}\">{$row[$column2]}</option>\n";
	}
    echo "</select>";
}
   mysqli_close($dbc);
}

dropdownmenu('categories', 'category' , 'cat_id', 'name');


Link to post
Share on other sites

The Background is from The Polaris Site in Funcom's Soon to be released MMORPG (July 3) The Secret World..The NDA was released on the closed Beta yesterday and I can now say I LOVE THE GAME.. Any hooo...

 

Okay Just did this without concatenating and with out the " ' " inside the thing and its working awesome.

 

Thanks you Edward I am not sure how I missed this but you have helped a great deal today. I was thinking it was a logic problem not a syntax issue and here it is a syntax issue.

 

Thanks again

Robert

 

 

P.S You so rawk They are all working as intended.. :D Thanks for taking the time. fianl code on this function yours is more elegant but I stuck with this for now.

 

function dropdownmenu($table, $ordby, $n, $option1, $option2 ) {
//.$table. DROP DOWN
$sql2 = 'SELECT * FROM `'.$table.'` ORDER BY `'.$table.'`.`'.$ordby.'` ASC LIMIT 0, 30 ';
$result=mysql_query($sql2) or die(mysql_error());
if($r =mysql_Query($sql2)) {//run Query
$i=0;
//Retrieve Records and Print .$table. Drop down box
print "<td>".$table."</br> <SELECT SIZE='1' NAME= '".$n."'>";
print "<option selected=\"selected\"></option>";
while (($row =mysql_fetch_array($result)) !==False){
$i++;
print "<OPTION VALUE={$row[$option1]}>{$row[$option2]}</option>";
}
print "</SELECT></td>";
}
}

Link to post
Share on other sites

I used to play World of Warcraft regularly, i think i will give 'The Secret World' a go when it comes out. I am happy your code is now working, but if you can its worth looking into learning some kind of framework like Yii as it can automatically populate dropdown menu's for you and save you the extra coding.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...