How to associate table rows with hyperlink from that row?

Discussion in 'PHP' started by username178452, Jun 26, 2008.

  1. #1
    Hi. I have a table listing products' data that is retrieved from the database. Each row corresponds to a product, and at the end of each row, there is a "buy" hyperlink. Each row is generated through a "for" loop like this:

    Code:

    $connection=mysql_connect('localhost','root',');
    if(!$connection){die(' ' . mysql_error());}
    
    $bd=mysql_select_db('PAT',$connection);
    if(!$bd){
    die(' ' . mysql_error());
    }
    
    $query=mysql_query("SELECT * FROM products",$connection);
    $regists=mysql_num_rows($query);
    
    for($i=1;$i<=$regists;$i++){
    $data = mysql_fetch_assoc($query);
    $query_image=mysql_query("SELECT * FROM images",$connection);
    $image = mysql_fetch_assoc($query_image);
    if($data['reserved']!=1){
    echo "<tr align=center> <td>".$data['name']."</td>";
    echo "<td>".$data['label']."</td>";
    echo "<td>".$data['price']." €</td>";
    echo "<td>".$data['stock_quantity']."</td>";
    echo "<td><img scr='".$image['path']."'></td>";
    echo "<td align=right><a href='product_reservation.php' name='link'>Buy</a></td>";
    echo "</tr>";}
    
    PHP:

    What I wanted it to do when the user clicked on the buy hyperlink was to generate a prompt so that the user could input the desired quantity of the product in the same row as the hyperlink. Then, the product_id, the quantity, the price*quantity and the user_id would all be stored in a relational table called "Orders" with foreign keys from the products' table and the users' table. All of that would be in the file "product_reservation.php" that is referenced by the hyperlink.

    I thought of something like setting the name of each hyperlink to the current value of the $i variable (see in the for statement) and then relate it to the number of the product in its table, as this is listed in order. Such as this:

    1st product in table - "Garments" -> $i=1
    hyperlink name="1"

    And then relate them in some way. Is something like this possible?


    Please, please give suggestions and help.
     
    username178452, Jun 26, 2008 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,930
    Likes Received:
    4,563
    Best Answers:
    124
    Trophy Points:
    665
    #2
    Create a form around the table

    Then the link will actually submit the form.

    Each product is actually part of an array so you might have
    <input type='text' name='qty[123]' value='0'>
    Code (markup):
    and that would tell me the client wants 0 of product #123

    however if they update that to 3 that will be the value in the $qty array for that product
     
    sarahk, Jun 26, 2008 IP
  3. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. But what code should I input specifically to act after the form is submitted? I've tried something like this to associate the product_id with the respective submit button:

     
    <form action="product_reservation.php" method="post">
    <table align="center" width="800">
    <tr border="2" bgcolor="grey">
    <th>Name</th>
    <th>Label</th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Quantity to buy</th>
    <th>Image</th>
    
    
    </tr>$query=mysql_query("SELECT * FROM products",$connection);
    $regists=mysql_num_rows($query);
    
    for($i=0;$i<$regists;$i++){
    $data = mysql_fetch_assoc($query);
    $query_image=mysql_query("SELECT * FROM images WHERE product=".$data['id_product]."",$connection);
    $image = mysql_fetch_assoc($query_image);
    echo "<tr align=center> <td>".$data['name']."</td>";
    echo "<td>".$data['label']."</td>";
    echo "<td>".$data['price']." €</td>";
    echo "<td>".$data['quantity_stock']."</td>";
    echo "<td background ='".$imagem['path']."'></td>";
    
    
    
    
    
    echo "<td><input type='text' size='1' name='text_".$data['id_product']."'></td>";
    echo "<td align=right><input type='submit' name='button_".$data['id_product']."' value='Make reservation'>
    
    
    
    
    
    echo "</tr>";}  
    PHP:
    What i did basically was to associate the button name with the number of the row the product is in. I was aiming for something like this:
    id_product - 2
    submit name - button_2

    Is this correct? If it is, what can I do from here? I'm kind of a newbie to this, so could you help with simple, specific code? Thanks
     
    username178452, Jun 30, 2008 IP
  4. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Anyone please? It's really urgent.
     
    username178452, Jun 30, 2008 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    First I need to know following to help you quickly:

    1. What exactly you need in script when it is submitted ? I mean what pieces of information ?

    regards
     
    Vooler, Jun 30, 2008 IP
  6. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I need to save the product_id, the quantity that was typed by the user in the text box and the price*quantity into the database. Also, I need to substract the quantity that was typed in the quantity_available and sum it in the quantity_reserved fields in the product's table in database. Please say if you need more info to help, and thanks for the reply.
     
    username178452, Jun 30, 2008 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    sorry I misunderstood, is each product has separate form, and when button in front of product is hit, it submits only one product ? if yes it is too easy. Tell me so I provide you code.

    regards
     
    Vooler, Jun 30, 2008 IP
  8. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well yes to everything, expect for one thing - The whole table is part of one form. The output will be different depending on what button is pressed, and that's where my difficulty lays. But do you think I should generate a form for each row?
     
    username178452, Jun 30, 2008 IP
  9. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #9
    Both are possible. Which one you like? tell me quick I need to hit bed soon, believe me am here to reply you becuase I comitted :).

    regards
     
    Vooler, Jun 30, 2008 IP
  10. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Tell me the one that i stated first - one form for the whole table. Anyway I take it you're from Europe if you're going to bed now? Are you here tomorrow morning? I need to hand this project over tomorrow and i just could really use some help.

    Thanks a lot Vooler.
     
    username178452, Jun 30, 2008 IP
  11. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #11
    Try this
    
    <tr align=center> 
    	<td>$data[name]</td>
    	<td>$data[label]</td>
    	<td>$data[price] €</td>
    	<td>$data[quantity_stock]</td>
    	<td>
    		<form>
    			<input type='text' size='1' name='product_qty'>
    			<input type=hidden name=product_id value='$data[id_product]'>
    			<input type=hidden name=product_price value='$data[price]'>
    			<input type='submit' value='Make reservation'> 
    		</form>
    	</td>
    	<td background ='$imagem[path]'></td>
    </tr>
    
    HTML:
    What you receive is the fields for each form:
    You might have to rearrange the <th> tags.


    I hope it helps
    regards
     
    Vooler, Jun 30, 2008 IP
  12. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I see where you're getting at. I think I can make something out of it. Thanks a lot man.
     
    username178452, Jun 30, 2008 IP
  13. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #13
    I wish you best of luck boy.

    BTW, am somewhere in asia :), I work in night, that is why I need to head to bed.

    regards
     
    Vooler, Jun 30, 2008 IP
  14. username178452

    username178452 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ok Vooler, stay nice. I'll try to reach you if i need more help, i ask you to reply when available. But thanks a lot really, it is really important.
     
    username178452, Jun 30, 2008 IP