Select and print with PHP & MySQL

I am really a beginner with PHP. I have thus far only used it to display a date and for including common components on my sites.

Now my daughter has asked that I let visitors to her site (still being setup) click on boxes (buttons) and when the visitor is finished selecting items, they click print and get a print of what they want in a list format (a shopping list) and maybe also a print of images or PDF files that match their shopping list.

I suspect I will need a temporary way to hold the selections and use that as the print file.

I want to learn more about how to program PHP and use databases, so pointing me to a good book and or example will be a help for me. Since this is something my daughter wants done sooner rather than later, pointing to a pre-made solution will make her happy (I still want to learn.)

Well the printing side of it is rather simple - simply output a page which looks just like what you want the printout to look like, and use JavaScript to prompt the user with print options. PHP has no such ability, as it runs on the server not on the client.

Where are the items coming from? A database?

Jake,

Looks like I have more research for printing. :slight_smile:

As I said, I am a beginner. My thoughts were to have the site visitor click on an item (or a box under the image or text), save the click to a temporary spot (PHP or MySQL). When the list is built, click on a print button and print the list.

The idea is to allow the visitor to click on items to build a printable shopping list.

So is the shopping list going to have to “remember” what has been picked from :

1 a single (long) page

2 several different pages

or

3 several different visits to your site?

If it is 2 or 3 you are asking about “maintaining state”, which can be done in several ways.

There are lots of ways of doing this, you could spend a little time looking at cookies, if they do not do what you want then you could look at using PHP Sessions.

If after you have collected all the items you need to store them on your server in order to retrieve them for 3 above or so that you, as an adminner can access them then there are several ways of doing this too.

Before anyone can help you though, perhaps you should tell us a bit more, like are these choices valuable? Are you collecting money? or is the time and effort your target audience put in somewhat trivial, a bit like a reminder list?

The idea is for the visitor to visit the site, find a listing for a store that they like, click on what they want to buy, and print a shopping list.

This will be a single long page that will have a print button at the bottom of the page. I haven’t figured out how to generate the initial list on the page, but some of the items will have an associated pdf to print if the visitor wants the pdf.

In that case it sounds as if you you can do it all in Javascript, one page, print at foot of page.

Is there any need to “remember” each user so that when they come back to your site they see their old information?

There should be no need to remember the old information.

If I can do all this with JavaScript, is there a module/widget or something that will do this, or will I need to get a JavaScript book to learn how to write the script?

Why not ask on the JS forum, maybe someone will help you knock something up quickly if you show you are willing to do a bit of learning.

I checked in the JavaScript forum, and they suggested I ask this question here:

I want people to select groceries from a list stored in our database, so that we can find coupons for them. The people will then be able to print out their grocery list and the matching coupons.

Is this something I can do with PHP?

You will need PHP (or another server-side language) to query your database, yes - its called server-side because your database is on the server.

If you use PHP to pick everything out of your database in one go and display it in one single page and the user has to select items from that page and then whilst still on that page print off a list of the items they chose, then all of that interaction will be happening on the client.

If all of the last paragraph is true, then you can do that bit using Javascript, because that is a client side technology (OK, nowadays you can do JS on the server - but lets leave that aside).

Now if that paragraph is not true, then tell us how you see the interaction going between the server and the client.

I want to learn more about how to program PHP and use databases, so pointing me to a good book and or example will be a help for me. Since this is something my daughter wants done sooner rather than later, pointing to a pre-made solution will make her happy (I still want to learn.)

Going back to your OP, OK lets start there, you want to collect some stuff from your database.

Have you already got a database installed, and do you have some test data in it?
Do you have either mysql or mysqli extensions installed on your server?

ps to find out check the output of


phpinfo();

This would be a good place to start [google]php mysql tutorial[/google]

Thanks for the link. I will get started, and I am now off to get a book or two from SitePoint.

I do have MySQL version 5 on the server (I have WordPress installed so there is that database).

So to get started, I will have:
to install and configure a database.
build something for my daughter to use while adding items to the database.
write some PHP code to read the database and put the items on the web page.

Is it at this point that I would need to have JavaScript track the selections and do the print?

I’d forget the JS for now, I suspect you may find you prefer to collect ‘pages’ of products from your database - if not the JS needed can be very simple to start off with.

When you say on the server, do you mean your development machine sat in front of you, or do you mean the server in the sky somewhere?

I do not have a server setup at my home office, but the site I am building is password protected. A development server is something I hope to get done this month. I am in the process of building computers. My wife’s computer finally developed more problems than I was willing to address (3 minute hold during POST before finding the 1st drive), so I build a machine for me and pass along my current computer to her. I also am building a media server for the grandchildren videos.

For a development server, I am thinking about installing one of the many varieties of Linux. I may just get one of the cast-off computers from the office and reload it.

I just re-read your message, and have a question.

What do you mean by “collect” pages?

I understand that I can have PHP read the database and put up the requested information (I just don’t know how, but will learn.)

Can PHP be used to build a page based on the selections of the visitor? If They click on store01 and a page is built, can they click on several items and submit to build a new page? (I need a confused smilie).

re: Your own dev setup. These days most people use an installer such as WAMP or XAMP (I think) which installs the whole Apache Mysql PHP stack for you.

You then fiddle around and learn on that, trying to learn working remotely on a live server is fraught with problems and makes it feel just one step too far away from you.

re your second post:

Essentially you may decide that everything in your database, can actually be described in categories.

So your start page will just list categories.

A user then clicks on one of those (so “drilling down”) and from that click you then go back and get everything in your database which belongs to that chosen category.

It’ll be easier to describe and explain when we know what you have in your database, but the interaction of PHP (outputting HTML in forms etc) and your backend database is how most sites are made, WP included.

But of course you cannot do that easily till you get a database up and running locally, and are ready to put information into it.

Depending on your skills this may not be as daunting as you might think, everyone on here will be urging you to give it a go.

This is what I have put together. First, I made a form.

<form action="insert.php" method="post">
Store name: <input type="text" name="store"><br>
Sale Dates: <input type="text" name="saledate"><br>
Sale Item: <input type="text" name="saleitem"><br>
Sale Price: <input type="text" name="saleprice"><br>
<input type="Submit">
</form>

This seemed to be workable, so I added some PHP.

<?
error_reporting(-1);
ini_set('display_errors', true);

$server= "**********";   /* Address of 1&1 database server */ 
$username="*********";        /* Database username */ 
$password="*********";           /* Database Password */
$database="*********";         /* name of database */

mysql_connect($server,$username,$password) or die ( "<h3>Server unreachable</h3>");
@mysql_select_db($database) or die( "<h3>Unable to select database</h3>");

$store=$_POST['store'];
$saledate=$_POST['saledate'];
$saleitem=$_POST['saleitem'];
$saleprice=$_POST['saleprice'];

$query = "INSERT INTO weeksales VALUES ('','$store','$saledate','$saleitem','$saleprice')";
mysql_query($query);

mysql_close();
?>

Then I tested it. This is the error messages:
Notice: Undefined index: store in /homepages/4/…test.php on line 32
Notice: Undefined index: saledate in /homepages/4/…test.php on line 33
Notice: Undefined index: saleitem in /homepages/4/…test.php on line 34
Notice: Undefined index: saleprice in /homepages/4/…test.php on line 35

Those are the four lines :
$server= ""; / Address of 1&1 database server /
$username="
"; /
Database username /
$password="
"; / Database Password /
$database="
"; / name of database */

So did I mess up the form, the PHP, or both? :slight_smile:

You seem to be testing and developing on a live server, are you saying you are not going to be installing a server on a local machine?

Those are the four lines :
$server= "*********"; / Address of 1&1 database server */

Use


var_dump($_POST);

for simple debugging early on in your script to make sure it is getting the expected post values.

If you are using mysql5.0 +, and when adding a blank to an auto-increment field, whilst this worked in Mysql4


INSERT INTO weeksales VALUES ('','$store','$saledate','$saleitem','$saleprice')";

It was illegal behaviour which has been fixed for Mysql5 so you should either leave it out, or do it so- using a zero


INSERT INTO weeksales VALUES ( 0 ,'$store','$saledate','$saleitem','$saleprice')";

Thinking about my reply, just try this first if you like:

insert.php


<form action="insert.php" method="post">
Store name: <input type="text" name="store" id="store"><br>
Sale Dates: <input type="text" name="saledate" id="saledate"><br>
Sale Item: <input type="text" name="saleitem"><br>
Sale Price: <input type="text" name="saleprice"><br>
<input type="Submit">
</form>
<?php  // note, use full php tags

if( $_POST ){
var_dump($_POST) ;
}
?>

Save it and run it, fill in the form, get any output?

Cups,

I get a 404 on the insert.php.

Is that a file I need to create?

Cups,

I get shifted to the insert.php page and thus no output from your code, but it does enter information now.

I have another page I have that will display the contents of the database, and when I pulled the php code out of the form page and made that the insert.php, I actually put data in the database.

I will add more information to the database, and work on making the form look better. The saleitem entry is 100 characters, but the form is only 20 characters. I need to make it larger and multiple lines.