Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Array WOES!

  1. #1
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts

    Array WOES!

    Ok so first off here is my function:

    PHP Code:
    function showCart() {
        global 
    $db$denom;
        
    $cart $_SESSION['cart'];
        
    $cart_price $_SESSION['cart_price'];
        if (
    $cart) {
            
    $items explode(',',$cart);
            
    $items2 explode(',',$cart_price);
            
    $contents = array();
            foreach (
    $items as $item) {
                
    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1;
            }
            
    $output '<form action="cart.php?action=update" method="post" id="cart">';
            
    $output .= '<table>';
            foreach (
    $contents as $id=>$qty) {
                
    $qry_fc "SELECT * FROM functions AS fc, eventos AS ev, prices AS pc WHERE fc.fc_id=$id AND ev.ev_id=fc.fc_ev_id AND pc.pr_ev_id=fc.fc_ev_id LIMIT 1";
                
    $result_fc mysql_query ($qry_fc);;
                
    $row_fc mysql_fetch_array ($result_fc);
                
    extract($row_fc);
                
    $output .= '<tr>';
                
    $output .= '<td><a href="cart.php?action=delete&id='.$id.'"><img src="images/remove.png" border="0" width="16"></a></td>';
                
    $output .= '<td><a href="event.php?id='.$ev_id.'" class="cartLINK">'.$ev_name.' - '.$fc_dtime.'</a></td>';
                
    $output .= '<td>'.$denom.' '.$pr_price.'</td>';
                
    $output .= '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
                
    $output .= '<td>'.$denom.' '.($pr_price $qty).'</td>';
                
    $total += $pr_price $qty;
                
    $output .= '</tr>';
            }
            
    $output .= '</table>';
            
    $output .= '<p>Grand total: <strong>'.$denom.' '.$total.'</strong></p>';
            
    $output .= '<div><button type="submit">Update cart</button></div>';
            
    $output .= '</form>';
            
    $output .= '<a href="#" onclick="get(\'alert\', \'emptyCart\', \'\', \'get(\\\'shoppingcart\\\', \\\'showCart\\\', \\\'\\\')\')"><img src="images/empty.png" border="0"></a>';
        } else {
            
    $output '<p>La canasta esta vacia. Busque Eventos <a href="index.php" class="cartLINK">Aqui</a></p>';
        }
        return 
    $output;
        

    Works just fine. Now the problem. You will noticed two things first off that are never used. cart_price and items2. These are new additions. What I need to do is this. Both those are now arrays as well. For each item in array one there is another number item in array two, that co-insides to array1. In other words let's spell it out:

    Array1 = 1,2,1,5,1
    Array2 = 1,1,2,1,4
    So array one means that there are 3 quantities of item number 1. This works fine. Normally this would be this way, three of item number one. But the second array is a price list IDs. That connect to each one. There's not really 3 of item # 1, there is 3 different items # 1, same product, but different prices, hence it is considered a new item in the cart. So some how I need to merge these two arrays into one and make it work that way the way it currently does but just with this addition. I just made myself dizzie

    Any ideas?
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I don't know if it's possible, as I didn't look closely at your code, but you should look into a design where your array can be uniquely keyed.

  • #3
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Well I did it totally different than up there finally. I got messing with it and decided to do it this way.

    What I did was have an array inside an array now. Now I pass all this info to the add cart function in this format:

    id1;id2;qty
    Then when the add cart get's it, it adds it to the end of the session cart variable with a ",". Then when I want to read it out into the cart for showing I double explode. First I explode the main session variable "," into separate items for the cart. Then I foreach it and for each product, or line in the cart, I explode again, this time the ";" and this time I do the query of the DB using the two fields, id1 and id2 to find the correct row in each of the three tables that I am querying.

    Then the final array item is the qty which I read back to make my price.

    Yes it might not be the right way to handle this I know. BUT it works and works well. So for right now I'm happy.

    Thanks for the advice.
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •