SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Adding tables able to calculate prices

    Good day,

    Helped a lot by people from this forum, here is our website:
    http://www.libromania.cl


    As you can see, we offer school textbooks.
    Now books season is starting here, and I need to publish books lists. It is, for every school working with us, we have to publish the corresponding books that every student has to have next season, and give to the parents the individual price per book and also the total amount of money to pay.
    The problem is, what happens if one person doesn't need one book, he will want to buy all books but one. Then, I need a table where the book titles and prices are, and also having a check for every item, and when one item is unchecked, the total amount of money to pay also changes accordingly.

    Thanks a lot!!!
    Last edited by Sir_Arcturua; Oct 13, 2011 at 17:54. Reason: Spelling mistake

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    This question is posted in the wrong place. Which server side language are you using to process the orders? If you tell us that then the post can be moved to the appropriate forum where people familiar with that language will be able to help.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm really sorry for that.
    I used PHP for making a function to send e-mails.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Moved to the PHP forum where it is more likely to get lots of answers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Big question.

    First off, you can accomplish what you want with Javascript, PHP or a combination of both.

    As a programmer you do not want to trust anything coming from the internet, that includes Javascript values - you use JS to make the shopping experience nicer and more responsive for those that have JS turned on on their browser.

    Start off by forgetting about clever Javascript solutions -- what you have to start with is the basic HTML form elements and PHP.

    To see one way of doing that, consider this simple script ...

    PHP Code:
    if(isset($_POST){
    var_dump($_POST);
    }

    echo 
    '<hr />';


    // this array is pretending to be your mysql select result
    // ie select book_id, book_name
    $rows = array(
    => 'textbook one',
    => 'textbook two',
    => 'textbook three',
    => 'textbook four',
    );

    echo 
    "<form action = '' method= 'POST'>";

    foreach(
    $rows as $key=>$value)
    echo 
    "<input type=checkbox id=book_id[$key] name=book_id[$key]>$value <br />" PHP_EOL ;

    echo 
    "<input type=submit></form>"
    Copy that simple script and run it, fill in different values and make sure you understand how checkboxes and PHP work.

    Starting from there, you then go on and make $rows an multi-array which also contains prices, say.

    But, the html only ever passes back to PHP a list of book ids (say 1 and 2 only), which when the form is posted, PHP queries the database for the prices of books 1 and 2, and passes back to the user the prices which PHP has totalled up.

    so even if the user sees:

    [checkbox] - Textbook One - $12.99
    [checkbox] - Textbook One - $10.99

    You do not send prices back from the interface to PHP.

    The next steps you take might be to show us how you store books, id, titles and prices - in a database?

  6. #6
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot for your help!!!

    About the page, it is very simple ... we have no database. It only shows stuff in HTML with CSS. The only PHP code is a small server side code for sending e-mails e-mail from the contact form page using mail() function in a separate .php file.

    I will be checking and testing the code you sent me in order to learn how to work. Is it possible to use it without database? I mean, make the calculations in the table itself and not just publish the values calculated externally?

    The final goal of all this is to have a final amount for payment in a variable for using it as an input in any online payment method, for instance Paypal.

    Thanks again!!!

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The nub of my point was this:

    That you can write in HTML all the prices and descriptions you want, but some unscrupulous person can change the total to 1 penny, and submit that to your email script, and to your paypal account, so you have to work out the full price on the backend (in PHP).

    If you are just dabbling with PHP and don't want/need a database you could just have a multi-array and maintain it by hand, put that in a file called books_array.php

    You'd then include() the file with that array in two scripts.

    Lets name them:

    books.php (the HTML form)
    order.php (the form "handler" or processor)

    books_array.php
    PHP Code:
    <?php

    // set up your array here

    $books = array(
    => array(
      
    'id' => 1,
      
    'title' => 'Textbook One',
      
    'price' => 12.99
    ),
    => array(
      
    'id'=>2,
      
    'title' => 'Textbook Two',
      
    'price' => 10.99,
    ),
    // and so on
    );

    books,php
    PHP Code:
    <?php
    include 'books_array.php'
    ?>

    // enter your page html here ...


    // start your form ....
    <?php

    foreach($books as $bk){

    // this is on two lines so you can read it without scrolling on SP
    echo '<p><input type=checkbox name=book_id['$bk['id'] . '] id=book_id['$bk['id'
                   . 
    '] >' .  $bk['price'] . ' : ' $bk['title'] . '</p>';

    }
    ?>
    // end your form


    // the rest of your html
    order.php
    PHP Code:
    <?php

    if(isset($_POST)){
    var_dump($_POST);
    }

    include 
    'books_array.php';

    // check which books were picked

    // grab the price

    // add the prices up

    // send the email

    // sent to paypal

    // send notification to the user onscreen **
    ** you might want to do this first, kind of "are you sure you want to order all these books for $xx.xx?"

    I have just pseudocoded roughly what order.php should do, but you should get the gist, that you maintain a single array of books, but that it serves 2 purposes - to generate the html list, and to provide something to generate the prices from.

    We can fill out those lines of pseudocode when you have got had a play with that and got some more realistic data loaded, filled out your html page and so on.

  8. #8
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great!! Thanks a lot!

    I will start working on it and keeping you posted.

  9. #9
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Done by now!

    I copied the code n two files and uploaded them. It is working fine, as you can see in:

    http://www.libromania.cl/books.php

    The point is I was not clear enough, so there is something else I need to ask you.
    The idea of our website is not to offer a list of many books to every person to pick some of them ... but, every school defines the books for every grade for next school season, so parents have to buy texts according to grades of their children.

    So what indeed we have is many small predefined lists, one per grade per school.
    We are implementing one webpage per school, it is, when parents select a school, a new webpage is displayed with a list of all grades(here we have 12 grades) from 1 to 12, so when they select one of them, a new webpage is opened, with the small list of textbooks indicated by the school for this specific grade.

    In other words:
    step 1: parents access website
    step 2: go to customers -> school where their children study
    step 3: select the grade where their first child will attend next school season
    step 4: a list of texts defined by the school for this level is displayed, all checked and ready to checkout
    step 5: it is possible to uncheck one or more textbooks, if desired
    step 6: checkout


    Sorry for my lack of explanation. What I try to say is according to the grade and school, a different textbook list will be displayed.

    Thanks a lot!!

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The idea of our website is not to offer a list of many books to every person to pick some of them ... but, every school defines the books for every grade for next school season, so parents have to buy texts according to grades of their children.
    Well you are miles away from nailing that problem if you need me to explain how a html form and a simple PHP array work.

    Not trying to put you off -- just to give you a reality check -- and to ask what is your first goal?

    (plus, how do you do this task at the moment then?)

  11. #11
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    About your last question, we are starting selling to parents, so we need to implement this now.

    The first goal is to be able to show for every person, textbooks according to his/her child (selecting school and grade), and offering an online payment method.

    Thanks a lot!!

  12. #12
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is it possible, for instance, try this?:
    - create different "books_array_nn.php" files, one per grade
    - have a webpage per school with all grades, each one linking to the corresponding table, in order to show it properly
    - allow people to unclick textbooks
    - proceed with checkout

    Thanks a lot!!!

  13. #13
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    When you say:

    // enter your page html here ...


    I added my HTML code like this:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
    <html lang="es">
    <head>
    <!--#include virtual="../spanish/head_spanish.html" -->
    </head>

    But I guess as the file is PHP, it is not in the proper format, and include sentences are considered comments.

    Could you please let me know how should I add HTML code within a PHP file?

    Thanks!!

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You can mix PHP and the text you want on the page:

    PHP Code:
    <?php

    // include something here

    ?>
    <html>
    <head>
    </head>
    <body>
    <p>Blah...</p>
    <form method=post action=order.php>

    <?php

    // echo your form elements ...

    ?>
    <!-- carry on with the form ...-->
    <input type-submit>
    </form>
    </body>
    </html>
    re different arrays:

    Are you saying that Grade 6, say, for all schools is the same list of books?

    If so then yes, that would be a good way to start - have different named arrays, just give them an easy to remember name:

    PHP Code:
    $books_grade_6 = array() // etc 

  15. #15
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi!!

    Unfortunately, texts lists for same grade are not the same for different schools, but at this moment we have only one school where parents will purchase us directly from website (others schools purchase from us directly and provide books to the parents).
    I agree that this way is not a long-term solution if more schools use it, but I guess it is easier than do it using a database, and allow me to know more about PHP/HTML, and maybe better prepared to implement a database solution as a next step.

    About integrating HTML in a .php file, please considerate I have defined sections and include them in other .shtm files using #include. I guess this format is wrong for .php files, as they recognize those includes as comments, as in this case:

    <?php
    include 'dsstgo_list_1B.php';
    ?>

    // enter your page html here ...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
    <html lang="es">
    <head>
    <!--#include virtual="../spanish/head_spanish.html" -->

    </head>
    <body>
    <div id="container">
    <!--#include virtual="../spanish/header_spanish.html" -->
    <!--#include virtual="../spanish/menu_spanish.html" -->


    // start your form ....
    <?php

    foreach($books as $bk){

    // this is on two lines so you can read it without scrolling on SP
    echo '<p><input type=checkbox name=book_id['. $bk['id'] . '] id=book_id['. $bk['id'] . '] >' . $bk['price'] . ' : ' . $bk['title'] . '</p>';

    }
    ?>
    // end your form


    // the rest of your html

    <!--#include virtual="../spanish/lower_section_spanish.html" -->

    <!--#include virtual="../spanish/footer_spanish.html" -->

    </div>
    </body>
    </html>

    Could you please let me know the right format? Should I avoid using #include sentences?

    Thanks again!!!

  16. #16
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi again!!

    I was wandering about this situation, and I guess one possible solution is:
    to have one .php file with all text lists for all levels of one school
    to have a webpage with radiobuttons, in order to select the desired grade
    depending the selected grade there is a variable with a certain value, and a based on this variable, a new page with the corresponding table is displayed

    Sounds it good?
    Please take a look to the code I sent you in my last e-mail. There is something I am making wrong including html sentences in a .php file.

    Thanks a lot!!!

  17. #17
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I'm not familiar with #include - I have not used anything like that for a long time.

    Your idea with radio buttons to choose a single grade is a sound one, though you could use a select box too.

    Yes, you do need to look into using a database this may sound daunting, but it is quite easy to grasp as long as you give yourself long enough time to digest and understand the rules that apply and the specialist terms used -- then you will be in a good position to ask pertinent questions(the Mysql forum on Sitepoint is very very good).

    Your PHP/HTML will not care whether it is iterating through a PHP array from a csv file, database or a hand-maintained array - so you still have to keep improving that and thinking about what you want to get from your application.

    e.g.

    how are you going to store orders?
    how will you maintain each school/grade/books list?
    will you eventually permit the schools to login and alter their own grade/books lists?

    You need to think big, but start small as you have done.

  18. #18
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I guess you are right. I will start dealing with databases. Could you please suggest a website/online course for noobies?

    Thanks a lot!

  19. #19
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    I was reading about Mysql during the weekend. I am at this moment working in define identities and attributes. I have some doubts and I created a new post:
    http://www.sitepoint.com/forums/show...and-attributes

    I'd appreciate if you could take a look.

    Thanks a lot!!!


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
  •