Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Encountering my PHP nightmare

    So... I am making a website and its framework is almost complete. Now is the time for doing the scary part that makes me **** bricks and become depressed.

    I am wanting to make a simple product list for my website that is generated in real-time (not a static product list that I make in pure HTML), and I want to store the product information in a MySQL database.
    What I don't understand, and cant even imagine - hows its done?
    http://i56.tinypic.com/w1fd5.png

    I really have no idea where to start. I am guessing most of the code lays in PHP, as MySQL will be simply storing the data and providing information on request via a PHP MySQL query.

    Does anyone know where I am coming from?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,316
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    What you're asking is a very vague question.

    Do you want product categories? - If yes you will not only need a table for your products but also a table for your categories which have an auto incrementing ID. In your products table you then use a column called category and store that category number there. Otherwise you can just have a products table by itself.

    Also in your products table you'll need to list how many items are in stock, you'll need an enabled column (EG you may have defective stock so wish to disable it temporarily from the site) and you'll almost certainly need an orders table.

    As for the page layout you may want to look into a loop for how many items per row but I have a feeling that CSS may solve this for you - I'm no html/css expert so someone else will need to comment on that as I'm only a backend coder.

    Failing that.. it's hard to know what you're having a problem with really.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Whatever would have been in your static list is what goes into the database (as far as item ID#, pricing, name, description, etc.). Then you will be running a MySQL query to grab the exact same information - only now you will be grabbing it from the database.

    You will get that database information into an array in PHP and then process the results in the page to format them exactly how you used to format them. Really, the only thing changing is that you have to create an array from the database information and then loop through the array to create the HTML structure. Everything else pretty much stays the same if you have set things up logically for your "static list."


    So...

    • Step 1: Determine what fields need to be in your product list database.
    • Step 2: Create your database.
    • Step 3: Populate your database with some sample products (or the real thing if you like).
    • Step 3: Copy your HTML into a framework for structuring your product listing (in other words, grab the HTML structure from ONE product in your static version for HTML elements, class names, and the like - and then replace the specific data within with PHP variables that will fill in the boxes when you're done). Wrap all of that in a print or echo command in your PHP page.
    • Step 4: Run a MySQL query with whatever restrictions you like to get some results. Write a "while" loop in PHP to print/echo the framework you created in step 3 for each product returned by your MySQL query.
    • Step 5: Debug, refine, re-test.
    • Step 6: Profit!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #4
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rowsdower! View Post
    Whatever would have been in your static list is what goes into the database (as far as item ID#, pricing, name, description, etc.). Then you will be running a MySQL query to grab the exact same information - only now you will be grabbing it from the database.
    http://i55.tinypic.com/2uyrvcp.png
    Exactly dude! But I'm not quite sure about how I would go with writing up the PHP so it lists the products, but when it does list them, its lists each product with an image, and some text along with the image such as a brief description.
    I'm wondering, if I should go about it by creating a static HTML page with the image and the short text description, and having PHP include() those static HTML pages.


    You will get that database information into an array in PHP and then process the results in the page to format them exactly how you used to format them.[/QUOTE]

    Quote Originally Posted by tangoforce View Post
    Your spot on with what I was trying to get out... ARRAY! This is what I'm trying to say I think (assuming I am interpreting you correct).
    So, a PHP array is what I should be looking into..

    Quote Originally Posted by Rowsdower! View Post
    Really, the only thing changing is that you have to create an array from the database information and then loop through the array to create the HTML structure. Everything else pretty much stays the same if you have set things up logically for your "static list."
    Right!

    Quote Originally Posted by Rowsdower! View Post
    So...

    [LIST][*]Step 1: Determine what fields need to be in your product list database.[*]Step 2: Create your database.[*]Step 3: Populate your database with some sample products (or the real thing if you like).[*]Step 3: Copy your HTML into a framework for structuring your product listing (in other words, grab the HTML structure from ONE product in your static version for HTML elements, class names, and the like - and then replace the specific data within with PHP variables that will fill in the boxes when you're done). Wrap all of that in a print or echo command in your PHP page.
    OMG, YES! I had a feeling it would work like that, but wasn't quite sure!
    Before I discovered MySQL, I was using that with PHP, where it would include $files = /*/info.html
    and then (if $files = file) {include $file}

    Fantastic! Heh, yeah profit... one day... I need to get a job to buy all the stock first. For now its a lesson as I've had a recent obsession with PHP/MySQL.


    Thanks for your info fellers... This has just been a huge shock to my mind and I have no sense of concentration because of it! I've taken all of this in and now I'm gonna get off the computer now and let my sub-conscious 'connect' all of this together, my approach plan, etc.
    Last edited by Democrazy; 09-14-2011 at 02:02 PM.

  • #5
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by Democrazy View Post
    Exactly dude! But I'm not quite sure about how I would go with writing up the PHP so it lists the products, but when it does list them, its lists each product with an image, and some text along with the image such as a brief description.
    I'm wondering, if I should go about it by creating a static HTML page with the image and the short text description, and having PHP include() those static HTML pages.
    That is exactly what you DON'T want to do. The whole point of PHP is to automate tasks so coding a separate page by hand for each product would totally defeat the purpose. Consider an arrangement roughly like this as your basic approach:

    PHP Code:
    # assuming your table is named "my_table_of_products" and that this table has columns
    # named "product_name," "product_image_src," "product_price," and "product_description"

    $query="SELECT * FROM my_table_of_products";
    $result=mysql_query($query);
    while(
    $row=mysql_fetch_assoc($result)){
        print 
    "<div class=\"product_block\"><h1>".$row['product_name']."</h1><img src=\"".$row['product_image_src']."\" alt=\"".$row['product_name']."\" /><h2>".$row['product_price']."</h2><p>".$row['product_description']."</p></div>";

    The part inside the "while" loop is where you would engineer your actual product display block template and insert the correct data fields from your MySQL database in the right spots.

    Does that make sense?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Democrazy (09-15-2011)

  • #6
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Hey Rowsdower!,


    It makes sense, in the sense that I know what your saying. I can visualize in my head the process in which you have described, however, how I can go about implimenting it is another story.

    What I am going to do now is read upon on the PHP manual in regard the the array() function, and also play around with that example code you quoted so I can get a feel of whats going on.

    I'll report back how I am going.
    Last edited by Democrazy; 09-15-2011 at 05:34 AM.

  • #7
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    I am going to create a basic product listing page for the category "Belts". Once I understand this, I think I will be bootstrapped enough to know how to go on and create a full blown product page.
    The product category page will be static HTML that will simply move into the PHP-powered product list page (which I am now trying to create). There is only 17 categories, and they will never be changing.

    I tried this, but got an error:
    PHP Code:
    $query "select * from products";
    $result mysql_query($query);
    while(
    $row=mysql_fetch_assoc($result)) {
        print 
    ".$row['name'] where category = 'Belts'.$row['colour'] where category = 'Belts'";

    PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    http://i54.tinypic.com/2j1si8w.png

    I am image what I am trying to do will print "Somename Black, ProductName2 Brown"?


    I'm not really interested in styling at the moment, I just want to see the PHP-MySQL work. Styling my site layout is last on the list to do.
    Last edited by Democrazy; 09-15-2011 at 05:34 AM.

  • #8
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Its call cool. I solved it:

    PHP Code:
    $query "select * from products where category = 'Belts'";
    $result mysql_query($query);
    while(
    $row=mysql_fetch_assoc($result)) {
        echo 
    $row['name'] . $row['primarycolour'];

    Last edited by Democrazy; 09-15-2011 at 10:02 AM.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by Democrazy View Post
    Its call cool. I solved it:

    PHP Code:
    $query "select * from products where category = 'Belts'";
    $result mysql_query($query);
    while(
    $row=mysql_fetch_assoc($result)) {
        echo 
    $row['name'] . $row['primarycolour'];

    This will do this:
    NamePrimarycolor

    If you want a space between it:
    PHP Code:
        echo $row['name'] . " " $row['primarycolour']; 


  •  

    Posting Permissions

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