SunQuest
           MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 6th, 2000, 01:09 PM
jmholm jmholm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Chicago IL, USA
Posts: 16 jmholm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 54 sec
Reputation Power: 0

I am trying to build a form based upon a query to MySQL. The form is a list of products, each with a quantity listbox (HTML <select> field). Each product may have a different set of allowable qty's (i.e., product 1 can be ordered in these qty's: 100, 200, 300 while product 2 can be ordered in qty's of 250, 500, or 1000).

My solution is to have a products table and a qty table. Qty table has two columns, prod_id and qty.

Can I do something like this:
select p.prod_name, q.qty from products p, qty q where p.id = q.id

and then loop through to populate the qty listbox for the first product, then get the next product and all qty's, and so forth? I'm confused about how to get the qty values for each product, then get the $row pointer to the next product and get all qty's again.

I suppose I could do this by executing a query to select all products, then begin a loop executing another query for each product to get the qty values for that product, then move to the next product and do the same. This seems to be terribly inefficient as it could cause a large number of query's to be executed, even with a relatively small product list.

Any ideas?

thanks,

michael

Reply With Quote
  #2  
Old March 6th, 2000, 01:57 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Your query isn't bad, might need some tweaking. Are the various quantities available for each product going to be an even multiple? If not, how do you plan on using one field for quantities? (Can be done, just curious).

As for the result set, yes, you can loop thru with something along these lines:

while ($prod_data=mysql_fetch_row($result))
{
// display product info and quantity box
}

This will loop thru the products one at a time.

Reply With Quote
  #3  
Old March 6th, 2000, 02:09 PM
jmholm jmholm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Chicago IL, USA
Posts: 16 jmholm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 54 sec
Reputation Power: 0
Rod,
Thanks for the quick reply. I'm still unsure about how to achieve the result.

When you state "Are the various quantities available for each product going to be an even multiple?", do you mean will each product have the same number of qty options? If so, no. Some may have 2 options, while others may have 5 or 6 options.

I'm sorry if I am missing something simple, but I am confused about your example:
while ($prod_data=mysql_fetch_row($result))
{
// display product info and
quantity box
}

If my query returns 5 rows that contain the following:

product 1,100
product 1,200
product 2,250
product 2,500
product 2,750

How do I get it to loop though, giving me this:
<tr>
<td>Product 1</td>
<td>
<select name='qty_1'>
<option value='0'>Quantity</option>
<option value='100'>100</option>
<option value='200'>200</option>
</select>
</td>
</tr>
<td>Product 2</td>
<td>
<select name='qty_2'>
<option value='0'>Quantity</option>
<option value='250'>100</option>
<option value='500'>200</option>
<option value='750'>300</option>
</select>
</td>
</tr>

Reply With Quote
  #4  
Old March 6th, 2000, 07:40 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Ok, now I see how you are setting up the tables. I'm not sure that's the best way but let's see what we can do with it.

$result=mysql_query("select p.product_name as pn, p.id as id, q.qty as qty from products p, qty q where p.id=q.id order by p.product_name");

$pn="";
while($data=mysql_fetch_array($result))
{
if ($pn!=$data[pn]) // start new line
{
if ($pn!="") print "</select><p>";
$pn=$data[pn];
print "$data[pn]: ";
print "<select name='qty[".$data[$id]."]'>";
print "<option value=0 default>0</option>";
}
print "<option value=$data[qty]>$data[qty]</option>";
}

This will give you the quantity selected in the array $qty with the product id as the index of the array.

Reply With Quote
  #5  
Old March 6th, 2000, 11:00 PM
jmholm jmholm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Chicago IL, USA
Posts: 16 jmholm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 54 sec
Reputation Power: 0
Rod,

Thanks for the code. It works great.

As for it not being the best way to do it, I'm completely open to suggestions to make it more efficient. Care to share?

Thanks again for the help.

michael

Reply With Quote
  #6  
Old March 7th, 2000, 07:40 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Well, I said I wasn't sure it was the best way. Not sure if there is a better way with the limitations you describe, either.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > MySQL query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway