SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sessions and arrays

    Hello all - got a query for you.

    It's a photography site. It has client galleries where the client can click on a photo and goes to a page with that photo and the various sizing options (pulled from a db) with an input box next to each so that the client can enter quantities as required for each size for that particular photo.

    What I need to be able to then do is to (I guess) on submit, pass the photo id, input's id (the photo size ) plus the quantity entered in the input boxes to a session as an array e.g. photo_34, 6x4, 2.

    I've confused myself somewhat between two issues:

    Firstly - how to actually build the array to include the photo id, plus info in the input ID (which is the size) and the actual returned value of the input. (e.g. photo_34, 6x4, 2 ) in each case.

    Secondly how to add further sizes and quantities for other images - would it end up being in effect with one huge array like "photo_34, 6x4, 2, photo_52, 7x8, 1" etc etc which I then explode later on?

    Any suggestions welcomed!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi TS

    Right then, you would be passing the values through a form and so could pass the size and id via hidden fields and the quantity through a text input.

    You then have them as POST values which you could just add ti a session array called basket.

    Give me a few minutes and I will whip something up!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    OK, here is an absolutely basic, barebones example.
    It isnt secure nor is it clever!
    PHP Code:
    <?php

    session_start
    ();

    if(isset(
    $_POST['form_id'])) {
        
        
    /* simplified
            ** */
        
    $str '';    

        foreach(
    $_POST as $key=>$data) {
            if((
    $key != 'submit') && ($key != 'form_id')) {
                
    $str .= $data',';
            }
        }

        
    $_SESSION['basketo'][] = rtrim($str',');
        
        
    print_r($_SESSION['basketo']);
    }

    ?>


    <form name="testForm" id="testForm" action="" method="post">
        <div class="imageHolder">
            [Your Image]
        </div>
        Quantity: <input type="text" name="quantity" />
        <input type="hidden" name="photo_id" value="1" />
        <input type="hidden" name="dims" value="16x4" />
        <button type="submit" name="submit" value="submit">Submit</button>
        <input type="hidden" name="form_id" value="addPhoto" />
    </form>
    But it should give you the principle.
    Each time the form is submitted it adds the info in a comma delimited list - Quantity, ID and Dimensions.

    If you want me to put it into something more functional - just ask
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok I see.

    One thing though - in this case you've got a single photo but say, 6 input boxes for quantities associated with different sizes so I'm not sure the hidden field would work for the photo size?
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  5. #5
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually - I suppose if I dumped the idea of the page with the single photo and all the different sizes in favour of a single quantity input box and a select box with a Buy Button for each image just in the customer gallery then what you've posted would work fine...

    Hmmm... goes away to think again....
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    lol, that would work
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Spike - I'll have a go over the weekend.

    I may come back to you for some more help on this as I'm not good at all with arrays (read: managed to avoid up to now!!).

    BTW - I'm supposing that there's nothing wrong with storing an order as an array like that in the db?
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Personally, I'd try to stay away from storing the array in your database. You instantly lose most of the benefits having this information in a database affords, easy access, reporting etc...

    I also think you know it's wrong, and it's making you feel dirty, but it's so ruddy tempting isn't it? :P
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Personally, I'd try to stay away from storing the array in your database. You instantly lose most of the benefits having this information in a database affords, easy access, reporting etc...

    I also think you know it's wrong, and it's making you feel dirty, but it's so ruddy tempting isn't it? :P

    I'm still learning all this stuff so I'm not great at figuring out the best way to structure these things yet. I was assuming a table for orders with an order number, the client id number and a column which would have the ordered items and quantities in an array.

    What do you reckon would be a better way of doing this?
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    To be fair, I'm no database guy myself, and I'm sure any schema I come up will be shot down by r937 but I'll give it a whirl...

    I'll be right back, with a poor solution just for you!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You have to think about what you want to offer your customers.
    Storing the order in a session as they go through the shop and then processing the order through a checkout is a single shot solution, if they close the browser - they lose the order.
    Storing in a database would mean they could come back to an order in the future if they had an id/ login.

    Just a simple table with ID, order_id, order_contents, order_date would be a start.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    OK, here is what I have so far...


    • product
      • id
      • name
      • description
    • sizes
      • id
      • name
      • price
    • product_sizes
      • product_id
      • size_id
    • users
      • id
      • name
      • email
    • orders
      • id
      • user_id
    • order_lines
      • order_id
      • product_id
      • size_id
      • quantity
      • total

    This should do it I think, obviously it would need to built upon, but here's a quick overview...

    • Allows multiple orders per user
    • Allows multiple items per order.
    • Allows you to change the price of the product without affecting past orders
    • Provides a more granular option for reporting.
    • Allows you to specify whether or not each product/image is available in a specific size.

    Comments are most welcome, I'm all for improving this.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see nothing wrong with what you've done SilverB.. Stick a few constraints in and it'll tie all nicely together.

    You could also create a category structure for separating the products.

    • category
      • id
      • parent_id
      • name
      • description


  14. #14
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah Yes - that makes total sense SilverBullet - thanks! I've basically got all those tables but splitting the order into 2 tables seems to be a sensible solution.

    There's no actual order processing as such (apart from an email notification to my client) - no payment, just an indication of an order as the client is adamant about actually doing a face-to-face to confirm it all with their customer.

    Thanks everyone!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That's great news, at least now you know what data you're going to need to pass to the database, and how it needs to be separated.

    This should also aid you in determining a structure for your session.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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