...

View Full Version : Encountering my PHP nightmare



Democrazy
09-14-2011, 02:08 PM
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?

tangoforce
09-14-2011, 02:35 PM
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.

Rowsdower!
09-14-2011, 02:37 PM
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!

Democrazy
09-14-2011, 02:58 PM
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]


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



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!


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.

Rowsdower!
09-14-2011, 03:19 PM
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:


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

Democrazy
09-15-2011, 04:47 AM
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.

Democrazy
09-15-2011, 05:50 AM
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:

$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. :)

Democrazy
09-15-2011, 11:00 AM
Its call cool. I solved it:


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

:)

Wanna
09-15-2011, 11:06 AM
Its call cool. I solved it:


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


echo $row['name'] . " " . $row['primarycolour'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum