Log in

View Full Version : Dynamic data in dropdown and preselected value?



Johan Beijar
03-08-2008, 10:44 AM
Hi,

I'm having a page that collects the productid from a previous page and should display data for a specific product. I do not get the dropdownlist Brand to work. I would like the brandname to be selected and also to display the other brands.

I use the $selectbrand to store the brandname and it should show up as the selected brand in the dropdownlist. I then use the $row_brandlist to display the remaning brandnames.

I only get resource id #20 in the dropdownlist...any input is appriciated.

Thank you,

//Johan Beijar




<?
$produpd = trim($_GET['productid']);
$produpdarray = mysql_query("SELECT brandid, username, productname, productnumber, producttype, gender, currency, price, recpriceretail, prodpic, mancountry, productdescription FROM products WHERE productid = ".$produpd." ");

$prup = mysql_fetch_array( $produpdarray );

?>
<div id="content">s
<h1>Update Product & Attributes</h1>
<table>
<TR>
<TD>
<form action="add_products_process.php" method="post">
<table align="center" border="0" cellspacing="3" cellpadding="5">
<tr>
<?
// Get records from database (table "My_Customer").
$selectbrand=mysql_query("SELECT brandid FROM products WHERE productid = ".$produpd." ");
?>
<td>Brand</td>
<td><select name="Brand">
<option selected><? echo $selectbrand; ?></option>
<?
// Show records by while loop.
while($row_brandlist=mysql_fetch_assoc($prup)){
?>
<option><? echo $row_brandlist['brandid']; ?></option>
<?
// End while loop.
}
?>

city_coder
03-08-2008, 11:13 AM
So let me get this right?
You've sent some data from a form including the productid and this is the unique key in your database?

You lost me when you said you store the brandname in the $selectbrand var.

If you want to do the compare and make it selected when you find the right (whatever it is, brand or product) then


while($row_brandlist=mysql_fetch_assoc($prup)){

if($productid == $rowbrandlist['produpd']) {
echo '<option>'.$rowbrandlist['produpd'].' selected>';
echo "</option>";
} else {
echo '<option>'.$rowbrandlist['produpd'].'</option>';
}
}


Sorry its not split up into html and PHP like you have been doing, but im tired lol and a bit busy.
I duno if that is what you wanted for the selected part or not. Hope it is.
If you can explain a bit better to us what it is that you wanted then might be able be able to help more.

Johan Beijar
03-08-2008, 12:22 PM
Hi,

Thanks!

Yes, I send productid from a previous page with GET-method.

I would like to have the brandname of the specific product as the selected choice in the dropdownlist. I would also like to have other brandnames posted in the dropdownlist. This an "update-page" for a specific product so they user needs to be able to choose from the brandname that he/she has stores previously.

I store the brandname for the product to be updated in the $selectbrand and then I have the remaing brandnames in the $prup-array.

Does this clarify my question and problem?

//Johan

Johan Beijar
03-08-2008, 12:53 PM
I use the following code but get none output in in the dropdownlist...

Any suggestions...

/Johan Beijar



<td>Brand</td>
<td><select name="Brand">
<?
$produpd = trim($_GET['productid']);
$produpdarray = mysql_query("SELECT productid, brandid, username, productname, productnumber, producttype, gender, currency, price, recpriceretail, prodpic, mancountry, productdescription FROM products WHERE productid = ".$produpd." ");

$prup = mysql_fetch_array( $produpdarray );

while($row_brandlist=mysql_fetch_assoc($prup)){

if($produpd == $rowbrandlist['productid']) {
?>
<option> <? echo $rowbrandlist['brandid'];?> </option>
<? echo "</option>";
} else { ?>
<option> <? echo $rowbrandlist['brandid'];?> </option>'
<? }
}
// End while loop.
?>
</select></td>

Johan Beijar
03-08-2008, 01:02 PM
one additional thing...the expected output in the dropdownlist should be something like;

Brand C
Brand A
Brand B
Brand C

And Brand C is the selected Brand by default dependent on which product the user has choosen to update.. Brand C is the brand of the product with productid taken from the previous page.
Brand A, Brand B and Brand C are all brands that the user can choose from and connected to his/her username.

Thank you for any help,

//Johan Beijar

city_coder
03-08-2008, 08:25 PM
Well that bit of code that i posted before should solve your problem of displaying which brand is to be displayed first(the user selected 1) but were not upto that point yet.

Have you ran the query in your mysql database just to check that it brings back results? just as a raw query and putting in a brand "brand A"?

Does the page you display something or is it just that you get a nice informative blank page?

One think i have noticed is that when your doing the query and i dont know whether this will make a difference is that you do:


$produpdarray = mysql_query("SELECT productid, brandid, username, productname, productnumber, producttype, gender, currency, price, recpriceretail, prodpic, mancountry, productdescription FROM products WHERE productid = ".$produpd." ");

Whereas in my experience it should be


$produpdarray = mysql_query("SELECT productid, brandid, username, productname, productnumber, producttype, gender, currency, price, recpriceretail, prodpic, mancountry, productdescription FROM products WHERE productid = "$produpd");

basically its just removing the concat parts at the end where you reference the variable with the brand in it.

Try it, see what you get in mysql, see if you get some results and if you dont then you know thats its the mysql syntax, if it does bring back results then take out the concat part(use my suggested query)

Also just as a note, you wont get Brand C appear twice(at the top and some where else in the option list), not unless you do 2 queries and stick on the end, but its not necessary anyway.

Hope this helps you to figure something out to an extent, let us know what happens so we can help if its not sorted

Leafy
03-09-2008, 04:51 PM
<?php
$produpd = trim($_GET['productid']);
$produpdarray = mysql_query("SELECT * FROM products");
$products = array();
while($row = mysql_fetch_array($proupdarray,MYSQL_ASSOC)) {
$products[count($products)] = $row;
}
?>
<div id="content">
<h1>Update Product & Attributes</h1>
<table>
<TR>
<TD>
<form action="add_products_process.php" method="post">
<table align="center" border="0" cellspacing="3" cellpadding="5">
<tr>
<td>
Brand
</td>
<td>
<select name="Brand">
<?php
foreach($products as $row) {
printf(
"<option%s>%s</option>",
(
($row["brandid"] == $_GET["productid"])?
(" selected=\"selected\""):
("")
),
"Brand {$row["brandid"]}"
);
}
?>
</select>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</div>


A few things I changed:


Don't use PHP short tags, they won't work in future versions of PHP.
Escape your inputs to a MySQL query using mysql_real_escape_string($str);
Try to use minimal amounts of queries. I shortened this from 2 to 1.