Forum Moderators: coopster

Message Too Old, No Replies

Need help with an IF statement

         

iggy99

9:38 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



hi -

i have a database table with a filed called product_addon which will on occasion have a 'Y' in the field

this code will display the "Y" if it exists

<?php
$db->p("product_addon");
?>

this is what i am trying to use to make 'Y' show different results

<?php

if ( $product_addon === "Y" )
{
echo "this and that";
}
else
{
echo "the other thing";
}
?>
so far all i can get this to do is show 'the other thing' whne the 'Y' is present

any ideas on how to get this to work?

many thanks

eelixduppy

9:49 pm on Jan 4, 2007 (gmt 0)



Try echoing the value of
$product_addon
before the if conditional. My guess is that you aren't even setting this variable. In above function call you said that it will display Y if it exists, not set the variable. Just make sure that the variable is being set first, is all ;)

Best of luck!

ahmedtheking

10:32 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try == instead of === because == means "equals this, but can be pretty much like it" (cos 11 to us is eleven but to a computer, this may just be a string rather than a integer cos it could be composed of "1+1") whereas === means that is has to be that exactly! Same applies for false/true and 0/1 (I think, please correct me if I'm wrong) where == can accept 0 and false but === can only accept either.

iggy99

11:27 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



ok changed a bit still no go --

i tried == no go then i addded

$product_addon = "Y";

for this

<?php
$product_addon = "Y";

if ( $product_addon === "Y" )
{
echo "this and that";
}
else
{
echo "the other thing";
}
?>

still no go

thoughts?

eelixduppy

11:33 pm on Jan 4, 2007 (gmt 0)



ahmedtheking,

Here's a good link: Comparison operators [us2.php.net].

iggy99,

If this doesn't work then there is a problem: ;)


$product_addon = "Y";

if($product_addon == "Y")
{
echo "this and that";
} else {
echo "the other thing";
}

Now, this is not what you are going to have when you are ready to impement the function you have that returns this value. Instead of having this static product_addon variable declared here you are going to return it.

iggy99

12:22 am on Jan 5, 2007 (gmt 0)

10+ Year Member



--->Instead of having this static product_addon variable declared here you are going to return it.

i think this is my problem ---

how do i 'return it'? i need to pull this from the table field called product_addon in table nemed 'product'

eelixduppy

4:21 am on Jan 5, 2007 (gmt 0)



Ok, let's say you have this function p:

[url=http://us3.php.net/manual/en/language.functions.php]function[/url] p($field) {

$link = [url=http://us3.php.net/manual/en/function.mysql-connect.php]mysql_connect[/url]("localhost","username","password");
[url=http://us3.php.net/manual/en/function.mysql-select-db.php]mysql_select_db[/url]("database");

$query = "SELECT ".$field." FROM product WHERE some_col = 'something'";
/*
In the previous line I'm assuming that you are only retrieving one row in which case you need to limit it somehow, possibly using unique id's
*/

$result = [url=http://us3.php.net/manual/en/function.mysql-query.php]mysql_query[/url]($query,$link) or [url=http://us3.php.net/manual/en/function.die.php]die[/url]([url=http://us3.php.net/manual/en/function.mysql-error.php]mysql_error[/url]());
if($result) {
$row = [url=http://us3.php.net/manual/en/function.mysql-fetch-array.php]mysql_fetch_array[/url]($result);
[url=http://us3.php.net/manual/en/functions.returning-values.php]return[/url] $row[$field];
} else {
[url=http://us3.php.net/manual/en/functions.returning-values.php]return[/url] 0;
}
}

Then you would be able to do something like this:


//Here you would initiate $db to the correct class
$product_addon = $db->p("product_addon");

if($product_addon == FALSE) {
echo "An error has occurred";
}
else if($product_addon == "Y") {
echo "Found Y";
} else {
echo 'This and that';
}

I hope this makes some sense. We'll see when you respond ;)

Although you may not need any of the resources I linked to, it just gives me something to do when there isn't a lot of action around WW ;)

iggy99

3:42 pm on Jan 5, 2007 (gmt 0)

10+ Year Member



ok i am making some progress...

seems we are already connection to the database -

this page is called product.php

this is on the top and i think this is how data from table 'products' is being pulled into the page

<?php
eval(load_class("product", "ps_product"));
$ps_product = new ps_product;
eval(load_class("product", "ps_product_category"));
$ps_product_category = new ps_product_category;
eval(load_class("product", "ps_product_attribute"));
$ps_product_attribute = new ps_product_attribute;
?>

i have noticed this before a tag that is pulling data...

<?php
if (!$ps_product->is_product($product_id)) {
$product_id = $ps_product->get_field($product_id, "product_parent_id");
}
$q = "SELECT * FROM product WHERE ";
$q .= "product_id='$product_id'";
$db->query($q);
$db->next_record();
?>

then this which displays the actual data
<?php
$db->p("product_name");
?>
<?php
$q = "SELECT * FROM product WHERE ";
$q .= "product_id='$product_id'";
$db->query($q);
$db->next_record();

$num=1;

$qt="quantity";
$qt .=$num;

$pa="product_id";

$or="order";
$or .=$xo_num;

?>

so i am of course confused :)

not sure how to get this variable out of the filed product_addon for any given product id

thoughts?

iggy99

12:03 am on Jan 6, 2007 (gmt 0)

10+ Year Member



works!

many tanks for your help:)

<?php

$q = "SELECT * FROM product WHERE ";
$q .= "product_id='$product_id'";
$db->query($q);
$db->next_record();
$product_addon = $db->f("product_addon")
?>

<?php

if ( $product_addon == "Y" )
{
echo "shop/cart";
}
else
{
echo "shop/addon";
}
?>

eelixduppy

12:05 am on Jan 6, 2007 (gmt 0)



Hey, great job! Glad it all got sorted :)