Page 1 of 2

Extract Variable from Database query

Posted: Sun Nov 06, 2005 8:50 pm
by facets
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

Posted: Sun Nov 06, 2005 9:35 pm
by feyd
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..

Posted: Sun Nov 06, 2005 9:53 pm
by josh

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'];

Posted: Sun Nov 06, 2005 11:36 pm
by facets
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";

Posted: Sun Nov 06, 2005 11:46 pm
by feyd
my post still applies.. :?

Posted: Mon Nov 07, 2005 3:42 am
by facets
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.

Posted: Mon Nov 07, 2005 4:00 am
by n00b Saibot
Do you want it like this :arrow: 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...

Posted: Mon Nov 07, 2005 4:13 am
by facets
that sounds about right.
know of an example I could follow?

Posted: Mon Nov 07, 2005 4:35 am
by n00b Saibot
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>";

Posted: Mon Nov 07, 2005 5:08 am
by facets
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];
}

Posted: Mon Nov 07, 2005 5:12 am
by n00b Saibot
i guess that to be innerText, change that to innerHTML. see if that works... maybe you should turn on report error if using IE...

Posted: Mon Nov 07, 2005 5:51 am
by facets
Many thanks! That hit the spot.

Posted: Mon Nov 07, 2005 9:17 pm
by facets
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.!

Posted: Mon Nov 07, 2005 9:47 pm
by feyd
why? Just reconstruct it from the submitted value of the combo box.. :)

Posted: Mon Nov 07, 2005 9:50 pm
by facets
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>');
	}