SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 42
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.)
    Each day is a learning experience.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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 Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jake,

    Looks like I have more research for printing.

    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.
    Each day is a learning experience.

  4. #4
    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)
    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?

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Each day is a learning experience.

  6. #6
    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)
    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?

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Each day is a learning experience.

  8. #8
    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)
    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.

  9. #9
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Each day is a learning experience.

  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)
    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
    PHP Code:
    phpinfo(); 
    This would be a good place to start php mysql tutorial

  11. #11
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Each day is a learning experience.

  12. #12
    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'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?

  13. #13
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Each day is a learning experience.

  14. #14
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    I suspect you may find you prefer to collect 'pages' of products from your database
    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).
    Each day is a learning experience.

  15. #15
    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)
    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.

  16. #16
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I have put together. First, I made a form.
    Code:
    <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.
    Code:
    <?
    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?
    Each day is a learning experience.

  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)
    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
    PHP Code:
    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
    Code sql:
    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
    Code sql:
    INSERT INTO weeksales VALUES ( 0 ,'$store','$saledate','$saleitem','$saleprice')";

  18. #18
    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)
    Thinking about my reply, just try this first if you like:

    insert.php

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

  19. #19
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups,

    I get a 404 on the insert.php.

    Is that a file I need to create?
    Each day is a learning experience.

  20. #20
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Each day is a learning experience.

  21. #21
    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 sample code I posted was for insert.php, it mixed html with php and posted back to itself.

    I just wanted to illustrate one way of debugging and "peering in" to see what is going on.

    The fundamental first question is this: "Is my html correctly formed, and is PHP picking up the values?"

    If so, prove it, then move on to putting it into your database.

    When you finally have everything working then you remove these "sanity checks", but you should not be doing that on a live server because you leave yourself open to hacks unless you password protect the folder.

    In order to develop safely and at easily you ideally need to install a server on a local machine, in my eyes this is a fundamental part of the process of learning PHP.

  22. #22
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups,

    So all I need to do is copy your code (form and php) and name it insert.php, and then test it?

    I will be getting a development server setup soon. The entire site I am working on is password protected, and when I am ready for the site to go live, I will have a password protected folder with things such as thins safely away from prying eyes.
    Each day is a learning experience.

  23. #23
    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)
    So all I need to do is copy your code (form and php) and name it insert.php, and then test it?
    Exactly.

    I will be getting a development server setup soon.
    Good news, that'll get you into a much quicker and far more comfortable try, test, fail, try-again loop.

  24. #24
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups,

    It worked. I just entered a string of characters and clicked submit. The result was:

    array(4) { ["store"]=> string(10) "aaaaaaaaaa" ["saledate"]=> string(11) "bbbbbbbbbbb" ["saleitem"]=> string(12) "cccccccccccc" ["saleprice"]=> string(15) "ddddddddddddddd" }

    I noticed that two of the inputs had both name and id and two had only name. Is there a reason for that?
    Each day is a learning experience.

  25. #25
    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)
    Ah, CSU-Bill, I am ashamed to say I can never remember which of the two (id or name) works when dealing with which language, JS/jQuery and the DOM and the world of strict xHTML and PHP and the backend (I could look it up, but it is one of those rare occasions I have no memorable Google search string to hand).

    So in my example I tried them both out and clearly "name" is the HTML/PHP transfer attribute.

    So, now you have got a working example of

    a) a well formed html form (for your particular browser) and
    b) PHP working and receiving the vars loud and clear

    .... so the next thing is to insert those vals into your database.

    Next step is to look closely at the typical data you will have in your database, for this you will need to come up with some simple test data - which is easy enough to insert but also be wide-ranging enough for us to be able to model your data, imagining the various scenarios of how to slice and dice it.

    This is something that is very rarely done right at the first attempt (in fact almost never).


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
  •