Adding tables able to calculate prices

Good day,

Helped a lot by people from this forum, here is our website:
[URL="http://www.libromania.cl
"]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!!!

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.

I’m really sorry for that.
I used PHP for making a function to send e-mails.

Moved to the PHP forum where it is more likely to get lots of answers.

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 …


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(
1 => 'textbook one',
2 => 'textbook two',
3 => 'textbook three',
4 => '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?

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!!!

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

// set up your array here

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

books,php


<?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

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.

Great!! Thanks a lot!

I will start working on it and keeping you posted.

:slight_smile: 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!!

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?)

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!!

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!!!

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!!

You can mix PHP and the text you want on the page:


<?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:


$books_grade_6 = array() // etc

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 ....
    &lt;?php
    
    foreach($books as $bk){
    
    // this is on two lines so you can read it without scrolling on SP
    echo '&lt;p&gt;&lt;input type=checkbox name=book_id['. $bk['id'] . '] id=book_id['. $bk['id'] . '] &gt;' .  $bk['price'] . ' : ' . $bk['title'] . '&lt;/p&gt;';
    
    }
    ?&gt;
    // end your form


    // the rest of your html

    &lt;!--#include virtual="../spanish/lower_section_spanish.html" --&gt;
     
    &lt;!--#include virtual="../spanish/footer_spanish.html" --&gt;

&lt;/div&gt;

</body>
</html>

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

Thanks again!!!

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!!!

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.

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!

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:

I’d appreciate if you could take a look.

Thanks a lot!!!