PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sun Nov 06, 2005 8:50 pm
Hi Guys,
I'm using the following code to extract data from my DB and populate a drop down menu.
Question is I wish to display the Variable productService outside of the drop down.
How could I do this?
Code: Select all
function serviceProduct() {
$sql_query = mysql_query("SELECT complaintType, id, productService FROM complaintType ORDER BY complaintType");
$output = "<select name=\"id\">\n";
$output .= "<option value=\"\">-- Please Select --</option>\n";
while(list($complaintType, $id, $productService)=mysql_fetch_array($sql_query)) {
$complaintType = stripslashes($complaintType);
$output .= "<option value=\"$id\">$complaintType, $productService</option>\n";
}
$output .= "</select>";
mysql_free_result($sql_query);
return $output;
}
echo $productService;
TIA, Will
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Nov 06, 2005 9:35 pm
you're producing hidden notices behind the scenes with your code.. mostly due to using list() with mysql_fetch_array() ..
Store the result from mysql_fetch_array() into a single variable. When inside the loop use list() to pull the bits out.. You may want to use mysql_fetch_row() or the numeric toggle on fetch_array..
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Sun Nov 06, 2005 9:53 pm
Code: Select all
// Get a nice associative array
$array=mysql_fetch_array($result, MYSQL_ASSOC);
// Loop through
foreach($array as $key=>$value) {
}
// Get single variable
$array['key'];
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sun Nov 06, 2005 11:36 pm
Thanks for the replies so far.
I don;t think I explained what i'm trying to do exactly.
I wish to display $productService outside of the while loop.
So if the user selects item-id 3 (or anyone of the drop down contents) $productService gets displayed next to the drop menu.
here's my code now.. it's a bit leaner too
i'm hoping there is a dynamic way to do this??
Code: Select all
$result = mysql_query("SELECT complaintType, id, productService FROM complaintType ORDER BY complaintType");
echo "<select name=\"id\">\n";
echo "<option value=\"\">-- Please Select --</option>\n";
while($row = mysql_fetch_row($result))
{
print("<option value=\"$row[0]\">$row[0]</option>\n");
}
echo "</select>";
echo "$productService";
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Nov 06, 2005 11:46 pm
my post still applies..
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 3:42 am
Thanks for your help feyd.
I am unsure of what you mean?? Sorry..
I've removed the list() & mysql_fetch_array issue you mentioned.
I can see in the HTML output that echo'ing the $productService variable displays the the right field but how do I make it so when the user selects an item from the dropdown the $productService is displayed?
Code: Select all
$sql_query = mysql_query("SELECT complaintType, id, productService FROM complaintType ORDER BY complaintType");
echo "<select name=\"id\">\n";
echo "<option value=\"\">-- Please Select --</option>\n";
while(list($complaintType, $id, $productService)=mysql_fetch_row($sql_query)) {
echo "<option value=\"$id\">$complaintType</option>\n";
echo $productService;
}
echo "</select>";
thanks again for your help.
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Mon Nov 07, 2005 4:00 am
Do you want it like this
A user selects an item from dropdown and then you show him the productService next to it maybe in a textbox or a div
If yes, that will require some js or alternatively frequent posting of form...
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 4:13 am
that sounds about right.
know of an example I could follow?
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Mon Nov 07, 2005 4:35 am
untested:
Code: Select all
$productServiceList = array();
$sql_query = mysql_query("SELECT complaintType, id, productService FROM complaintType ORDER BY complaintType");
echo "<select name=\"id\" onchange=\"showProdServ(this.options[this.selectedIndex].value)\">
<option value=\"\">-- Please Select --</option>\n";
while(list($complaintType, $id, $productService)=mysql_fetch_row($sql_query))
{
echo "<option value=\"$id\">$complaintType</option>\n";
$productServiceList[$id] = $productService;
}
echo "</select>
<span id=\"prodServ\"></span>
<script>
Viewer = document.getElementById(\"prodServ\");
ProdServList = new Array();";
foreach ($productServiceList as $id=>$prodServ)
echo "ProdServList[{$id}] = \"{$prodServ}\"\n";
echo "
function showProdServ(val)
{
Viewer.innerText = ProdServList[val];
}
</script>";
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 5:08 am
thanks! that's damn close.
it's not displaying the variable to screen yet but the list is in the html code.
some js debugging now
thanks again!!
Code: Select all
<div id="prodServ">Stuff</div>
<script>
Viewer = document.getElementById("prodServ");
ProdServList = new Array();ProdServList[1] = "0"
ProdServList[2] = "1"
ProdServList[41] = "0"
ProdServList[38] = "0"
ProdServList[40] = "0"
ProdServList[39] = "0"
ProdServList[3] = "0"
ProdServList[4] = "0"
ProdServList[35] = "0"
ProdServList[5] = "1"
ProdServList[6] = "0"
ProdServList[7] = "1"
ProdServList[8] = "0"
ProdServList[9] = "0"
ProdServList[10] = "1"
ProdServList[11] = "1"
ProdServList[12] = "0"
ProdServList[13] = "0"
ProdServList[14] = "0"
ProdServList[15] = "0"
ProdServList[16] = "1"
ProdServList[17] = "0"
ProdServList[18] = "0"
ProdServList[34] = "0"
ProdServList[33] = "0"
ProdServList[19] = "0"
ProdServList[20] = "0"
ProdServList[32] = "0"
ProdServList[21] = "0"
ProdServList[23] = "1"
ProdServList[24] = "1"
ProdServList[25] = "0"
ProdServList[37] = "0"
ProdServList[26] = "0"
ProdServList[27] = "1"
ProdServList[28] = "0"
ProdServList[36] = "0"
ProdServList[29] = "0"
ProdServList[30] = "0"
ProdServList[31] = "0"
function showProdServ(val)
{
Viewer.innerText = ProdServList[val];
}
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Mon Nov 07, 2005 5:12 am
i guess that to be innerText, change that to innerHTML. see if that works... maybe you should turn on report error if using IE...
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 5:51 am
Many thanks! That hit the spot.
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 9:17 pm
now to push my luck
would there be anyway to assign the output of Viewer.innerText = ProdServList[val]; to a PHP variable?
I basically need to use an if statement to display another block of text depending on the output.
edit: I'm guessing this will have to be done with JS as it's all client side. Sorry.!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Nov 07, 2005 9:47 pm
why? Just reconstruct it from the submitted value of the combo box..
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Mon Nov 07, 2005 9:50 pm
I need to do this before submit though?
my suggested work flow ->
user selects from drop down
name 'product or service' is displayed
product div or service div is displayed.
Code: Select all
if (ProdServList[val] == 'Service') {
document.write('<a href=\"javascript:hidediv()\">Hide div</a>');
}
else {
var mytext = \"Product\";
document.write('<a href=\"javascript:showdiv()\">show div</a>');
}