db in drop box

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

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

db in drop box

Post by nite4000 »

I need a lil help here I am close to having it but not close enough. I have a site where they log in it uses session now

I need to take a drop box and have it display the co_id from another table


I have a table coupons which lists all coupons available

coupons table
id,name,desc


Coupon_users table
id,co_id,mem_id,email


now in the drop box i know i have to select from the coupon_users table and user where something = something which would be from coupons table but not sure what

here is some code i have

Code: Select all

$r = mysql_query("select * from coupon_users") or die(mysql_error());
$cop = mysql_fetch_array($r, MYSQL_ASSOC);
@mysql_free_result($r);
 

Code: Select all

 
<select name="coupons_a">
  <?php
                     $r = mysql_query("SELECT * from coupons WHERE id ='".$cop['co_id']."'          ") or die(mysql_error());
            while($co = mysql_fetch_array($r, MYSQL_ASSOC)) {
          
           
            echo '<option value="'.$co['id'].'">'.$cop['co_id'].'</option>';
                 
             
                }
              
                  ?>  
  </select>
 


I hope someone can help I know I am close

also should be able to show if the users has more then one coupon which would be the co_id so if they have 3 then in drop box it should show 3 numbers each number being a coupon id


Thanks
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: db in drop box

Post by andyhoneycutt »

In code block A, you're only getting one row. Not sure if you're aware of that. Also, mysql_fetch_assoc() is an alias of mysql_fetch_array(<resource>,MYSQL_ASSOC), if you want to save a little typing :)

What appears to be going on to me is you're grabbing all of the coupons available to users, then matching them up to coupons in a different table based on ID. You can pull this off in one query using a JOIN. Also, if you could explain your code a bit better I'd love to help more.

Thanks much,
Andy
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: db in drop box

Post by nite4000 »

well here is what i have now and its listing the items for the logged in member only however i need to make it show more then one in drop box if the user has more then one

Code: Select all

 
 <?php $r = mysql_query("select * from coupon_users where email='" . $_SESSION['sess_name'] . "'") or die(mysql_error());
$cop = mysql_fetch_array($r, MYSQL_ASSOC);
@mysql_free_result($r);
?>
<select name="coupons_a">
  <?php
                     $r = mysql_query("SELECT * from coupons WHERE id ='".$cop['co_id']."'") or die(mysql_error());
            while($co = mysql_fetch_array($r, MYSQL_ASSOC)) {
          
           
            echo '<option value="'.$co['id'].'">'.$co['name'].'</option>';
                 
             
                }
              
                  ?>  
  </select>
 
 
 
 

Anyone?
Post Reply