Please please, make me a loop

Discussion in 'PHP' started by jacka, May 31, 2007.

  1. #1
    Hi

    I have managed to do a change an input number when a user types in, but I don't know how I can change the number when there is a list of many items?
    I suppose the question I am asking is, how do I know which item the user has chamged, when there are many items listes.
    I know it may require a counter of some sort but I do not know how to do it.
    Any pointers to the right direction is most welcome.
    here is part of the code.
    .....
    <?php


    if ($row = mysql_fetch_array($result))
    {
    // Increment the total cost of all items
    $t = $t + 1;

    $totalCost += ($row["qty"] * $row["price_each1"]);
    ?>
    <tr>
    <td width="7%" height="25">
    <font face="verdana" color="black" align="center">

    <form >

    <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" >




    <td>





    </form>
    etc. etc.

    and this is the javascript fucntion...

    .......
    function UpdateQty2(s)
    {
    var itemId=s;
    //newQty=b;


    var newQty= document.getElementById("t").value;
    document.location.href = 'cartnew4.php?action=update_item&id='+itemId+'&qty='+newQty;


    When I change the "if" statement above to "while" to list all the items, I can not distingusih which item teh user has changed.
    thx
    :confused:
     
    jacka, May 31, 2007 IP
  2. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    One solution I thought was , what if I make id="t",to id="<?php echo $row["itemId"];?>", that way the id of every item is same as the item_number.

    Now in javascript all I have to do is call the id array whose qty number has been changed.


    say:
    item one, id=3000, item_number=3000 qty=2
    item two, id=5678, item_number=5678 qty=6

    Now if a person changes a quantity from line two, all I want to do is send the new quantity together with the item_number to the javascript. as simpel as that

    But I do not know how to write the javascript formula:

    Thanks for taking the time to look at my problem.
     
    jacka, May 31, 2007 IP
  3. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #3
    i would use a while loop for something like this. see my example.

    
    <form action....>
    <?php
        /// database connection and query stuff here.
        $result = mysql_query($query) or die("could not complete query. error: ".mysql_error());
        
        while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
        {
            echo '<input type="text" name="'.$row["itemId"].'" value="'.$row["qty"].'" id="t" onchange="UpdateQty2(\'name\')" /><br />';
        }
    ?>
    </form>
    
    Code (markup):
    this is just how i would do it. i hope it clears some things up for you. good luck and happy coding.

    ansi
     
    ansi, May 31, 2007 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi ansi
    Thanks for your comments.
    As I h"ave mentioned in my second posting, I can put it in a loop by replacing the "if statement with a "while", but it only works on the first item in the list.
    When there are more than one entry, the program does not know which item the user has chnaged , I guess because it is not put in an array, so it is only capable of replacing the first item in the list.
    thanks.
    jacka
     
    jacka, Jun 1, 2007 IP
  5. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #5
    not quite sure that i understand what you are trying to do exactly
     
    ansi, Jun 1, 2007 IP
  6. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi ansi

    Thanks for replying.

    The code that I have written will output a list of items from a database, and place this in a table a row at a time.

    To make matters simple, lets say it will only dispaly the qty from a databse:

    Qty
    12
    2
    15
    16 etc.

    Each of these lines are in an input box, individually, so the user can change any of the quatities he has previously ordered. so far so good.

    The problem I am having is when the user has changed the row 3 say,from 15 to 20, I have no way of telling the program to send just that particular number to the javascript onchange event.

    I think I will have to put the id's for each line in an array and then recall that particular id, but I am only just guessing.
    Thx
    jacka
     
    jacka, Jun 1, 2007 IP
  7. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #7
    right, but when they continue to the next step or whatever, arent the values there read again? i mean, you have cart.php (example) and when it is viewed it has the item numbers in input boxes, then they "continue on" to the next step where the actual values are read. unless you're calculating cart totals on the fly or something with javascript then onchange='update_total()'; then loop through and get the prices and multiply it by the quantity. but either way, they should be able to change it and then when it goes to the "continue" part, insert the data or whatever. just how i would go about it.
     
    ansi, Jun 1, 2007 IP