PDA

View Full Version : IF statement not working



TheKG
11-16-2008, 08:38 AM
I simply cannot get my IF statement to work. I have tried so many different ways and nothing is working. If I read one more tutorial or thread in a forum I'm going to pop an eyeball! Please help. I have set up a db with 4 tables. I am now attempting to display products from the tables. These are clothing items that are available in different colors/prints and several sizes. I have been successful in displaying only the one color/print on the page, but all sizes in all styles are also displaying. The IF statement I have attempted is to display all the sizes in only one of the styles. My plan is to nest tables so that each style displays in separate locations on the page for ordering clarity. Hope this isn't too confusing. Here's my query:



$query = "SELECT T1.*, T2.vendor_name
FROM products AS T1, vendors AS T2
WHERE T1.vendor_code=T2.vendor_code
AND T1.color_code='BEEY'
AND T1.vendor_code='CHE'";


The style numbers are in T1 as "style_no". Just want to tell this program if the style number is (whatever the style number is) to echo the next statement. Here's the while command:



while($row = mysqli_fetch_assoc($result))

I'd appreciate any help anyone can give.

sjlplat
11-16-2008, 08:15 PM
Normally I create a query, then write a loop to build an array and an output. As long as you perform all actions within the loop, you should be able to extract only the data you need, build the output, then move on to the next record.


$query = mysql_query("SELECT * FROM table WHERE field1='this' AND field2='this'") or die(mysql_error());

while ($array = mysql_fetch_array($query)) {
$field1 = $array['field1'];
$field2 = $array['field2'];
echo $field1;
echo $field2;
}

TheKG
11-18-2008, 03:28 PM
Thanks for the reply. Don't think this will work for my needs. What I'm attempting to do is create the variable $style using the column style_no from my db. Then after the while loop use "if $style=4700" (4700 being the style number of the article of clothing). Is this possible?

RDM
11-18-2008, 04:24 PM
while($row = mysqli_fetch_assoc($result)) {
$style = $row['style_no'];
}

if ($style == 4700) {
//do whatever here
}

sjlplat
11-18-2008, 08:04 PM
So are you trying to retrieve just a single style type from the database?

TheKG
11-19-2008, 10:21 AM
Yes, exactly. The page I'm working on now is for an item with a print fabric. It's available in 2 styles, each style available in 9 sizes. Although it's still a "work in progress" as far as layout and font, here's the url to maybe give you a better idea of what I'm attempting http://www.testsite.uniqueuniforms.com/ordertest1.php. Keep in mind that the style number does not display on the page; the listing you see is all sizes, both styles.

Hope this makes it less confusing...I'm confused enough.

Look at the post from RDM. I already tried that in several formats. Depending upon which version I try, I either get nothing or everything.

TheKG
11-19-2008, 10:24 AM
Thanks...but tried this with/without quotes, brackets, parenthesis; with one or two "=" signs. The page displayed either no results or all results.

RDM
11-19-2008, 11:14 AM
Thanks...but tried this with/without quotes, brackets, parenthesis; with one or two "=" signs. The page displayed either no results or all results.

Can you post the entire code? Or at least the code from the sql query to the if statement, not just parts of it?

shadmego
11-19-2008, 11:44 AM
After looking at the sample page, I am still not sure what you are trying to accomplish.

Here is what I understand from the explaination so far:

1. You have a product with (at least) two style
2. Each of these styles have 9 sizes each
3. You want the product page to show each style with each of the nine sizes

If this is not correct, then forgive me. I am coming in the middle of this, but I did look over your site. I looked at the print link, where you offer two brand names for prints, you have several pages of styles your customers can choose from. When someone selects a style, they are taken to a page that allows them to select the sizes they desire.

Why can you not do something like this for the product in question? Make a styles page (only) and then based on which style is selected, make a page for each style with the available sizes (only).

Again, I could be way off base, but I think it would help get around the confusion of the while/if statements you are trying to get working by splitting them into two different pages.

~regards

TheKG
11-19-2008, 12:45 PM
After looking at the sample page, I am still not sure what you are trying to accomplish.

Here is what I understand from the explaination so far:

1. You have a product with (at least) two style
2. Each of these styles have 9 sizes each
3. You want the product page to show each style with each of the nine sizes

If this is not correct, then forgive me. I am coming in the middle of this, but I did look over your site. I looked at the print link, where you offer two brand names for prints, you have several pages of styles your customers can choose from. When someone selects a style, they are taken to a page that allows them to select the sizes they desire.

Why can you not do something like this for the product in question? Make a styles page (only) and then based on which style is selected, make a page for each style with the available sizes (only).

Again, I could be way off base, but I think it would help get around the confusion of the while/if statements you are trying to get working by splitting them into two different pages.

~regards

You have the basic concept. My active site uniqueuniforms.com was designed with Front Page :eek: . It has been active since 2003 and I have found that customers interested in a particilar color/print want to see all styles available on one page. Many times an entire office or department is placing an order and each employee wants a different style. This is much more convenient for the customer.

sjlplat
11-19-2008, 05:21 PM
If you're only retrieving one style then why do you need to build an associative array based on two tables?

TheKG
11-20-2008, 11:15 AM
Since I was very new to MySQL and PHP, I did quite a bit of reading. All information I found recommended categorizing the info into tables under 1 db. I have 4 tables in my db; vendors, colors, styles and products. The page design was intended to be as universal as possible so I could "save as" and change as little as possible to generate the ordering pages needed for each of 80 - 100 prints I sell. Have I misunderstood the benefits of using PHP and MySQL?

sjlplat
11-20-2008, 04:58 PM
Since I was very new to MySQL and PHP, I did quite a bit of reading. All information I found recommended categorizing the info into tables under 1 db. I have 4 tables in my db; vendors, colors, styles and products. The page design was intended to be as universal as possible so I could "save as" and change as little as possible to generate the ordering pages needed for each of 80 - 100 prints I sell. Have I misunderstood the benefits of using PHP and MySQL?

Your table organization sounds fine. I'm wondering why you need to query two tables if you're only retrieving data for one product. You want a list of sizes for one style, so why can't you query your size table by filtering out the style?


$query = mysql_query("SELECT * FROM styles WHERE style='$style'") or die(mysql_error());
$array = mysql_fetch_array($query);
foreach ($array as $size) {
echo $size;
}

GerritMesker
11-21-2008, 08:59 AM
Your table organization sounds fine. I'm wondering why you need to query two tables if you're only retrieving data for one product. You want a list of sizes for one style, so why can't you query your size table by filtering out the style?


$query = mysql_query("SELECT * FROM styles WHERE style='$style'") or die(mysql_error());
$array = mysql_fetch_array($query);
foreach ($array as $size) {
echo $size;
}

Maybe it doesn't make a difference but I'm used to define the where-clause like this:

$query = mysql_query("SELECT * FROM styles WHERE 'style'='$style'") or die(mysql_error());
}

So, with the fieldname also between ' - characters. But to be honest, why I do that I can't rember anymore but it works. :D

TheKG
11-21-2008, 09:59 AM
My query includes the other tables because some of the information needed in my "echo" is located in the other tables; i.e., vendor_name, color_img. Although some of the info doesn't display, it is in an "<input" that passes the info to my cart. Received another reply that didn't display here, but advised that I put all info in 1 table for simplicity and it will actually query faster with less cpu usage. I will be experimenting with all advice given and update this thread with the results.

sjlplat
11-22-2008, 07:58 AM
It's hard to know exactly how to approach the table organization without having all the development info available. It may be more efficient with a single table, but it may not. As a beginner, it is definitely beneficial to experiment. It helps you learn about different techniques and contributes to developing your own unique style. Individuality is a major asset when developing web content.

TheKG
11-22-2008, 09:17 AM
Thank you for all your suggestions. I revised my tables; tried only 1...what a disaster! Realized that many of the fields in other tables need to be repeated in the "products" table. As I stated in another post, so much reading, so many tutorials, conflicting information. The best info and advice I have received is right here! My appreciation is to everyone who contributed their experience. I feel better equipped to experiment and find the best solution for the results I want.