ollyno1uk Posted February 23, 2008 Share Posted February 23, 2008 Hi I am very new to php. I am trying to do something that should be simple - adding an if statement. case 'PRODUCT_LIST_BUY_NOW': $lc_align = 'center'; if ($products_info['supplier']=='1') { $lc_text = '<a href="some url"</a>' ; }else{ $lc_text = '<a href="some url" </a>' ; } break; The if is the part I added. Now this didn't work so I checked through the rest of the script and I could not see that $product_info had been defined on this page so perhaps this was why. Now here is where I am very new to this, but based on what I had been told by someone I added this to the top of the page in the hope I would create the variable: $result = mysql_query("select supplier from products"); $products_info = mysql_fetch_assoc($result); This has not worked either. I'm looking for some advice on how I can achieve this simple if else command. Please remember that I am very new to this so any other technical references to functions etc I will not understand well. Thanks in advance Quote Link to comment Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 just a tip... if you are new to if...else statements, i suggest creating 'very' simple scripts testing 'only' the if statements. this would rule out any other possible interference by other workings of the script. once you are certain that works, than implement it to your main script. just a thought *edit- are you getting any errors? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 just to be sure are you doing them in order? <?php $re = mysql_query("select * from products") or die (mysql_error()); if (mysql_num_rows($re)) { while ($products_info = mysql_fetch_array($re)) { if ($products_info['supplier']=='1') { $lc_text = '<a href="some url"</a>' ; }else{ $lc_text = '<a href="some url" </a>' ; } } } ?> Because if your doing mysql_fetch_array at the end of the page the start of it will not have the vars created Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 Thanks I know this if does work as I have used it on other pages. What I do not know though is what to do when on this page the variable has not been defined or if there is possibly something else on the page that could conflict with this particular if statement drisate - i'm sorry but I do not really understadn what you mean? Thanks a lot Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 just use the code i posted lol try it. i am positive that will work Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 And to optimize it you can also do this: <?php $supplier = @current(@mysql_fetch_assoc(@mysql_query("select supplier from products"))); if ($supplier=='1') { $lc_text = '<a  href="some url"</a>' ; }else{ $lc_text = '<a href="some url" </a>' ; } ?> Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 thanks but unfortuantely this has not worked. It shows the url as though supplier == 1 even though some do not and the site is very slow to load the page. Could it be that the issues comes from just dropping this if.else in the middle of what looks like a switch case? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 the $supplier var takes the value from the table you should look in phpmyadmin if the var is 1 or 0 abbout the slowing i don't think it's the if ... would probably come from my extreamly lazy string lol $supplier = @current(@mysql_fetch_assoc(@mysql_query("select supplier from products"))); Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 yes I have checked in the DB via phpmyadmin and there are som that have 0 and some that have 1 Do you think the string could cause a speed issue as well ? Any other clues? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 Yeah well if you have more then one row to check you need to use a loop so i guess this would be best <?php $re = mysql_query("select * from products") or die (mysql_error()); if (mysql_num_rows($re)) { while ($products_info = mysql_fetch_array($re)) { if ($products_info['supplier']=='1') { $lc_text = '<a  href="some url"</a>' ; }else{ $lc_text = '<a href="some url" </a>' ; } } } ?> Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 it's still not working. if I change it to != '1' then all the urls change, so I know the if is working It's as if the supplier field is not being looked at correctly. The strange this is that it is showing all records as though the have the 1 in supplier, as if there were no 0s but I have checked and there are. Its baffling. Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 thats because you missing a where to the query select * from products where id='$id' or something like that to point the row you wana show Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 sorry you will have to forgive my stupidity but you have lost me Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 23, 2008 Share Posted February 23, 2008 You are not formatting the "<a>" tags correctly in: <?php $lc_text = '<a href="some url"</a>' ; }else{ $lc_text = '<a href="some url" </a>' ; } ?> So the browser just chucks them. You probably want something like <?php $lc_text = '<a href="some url">Some text</a>' ; }else{ $lc_text = '<a href="someother url">Some other text</a>' ; } ?> This can be simplified by using the ternary operators "? :" <?php $lc_text = ($products_info['supplier']=='1'):'<a href="some url">Some text</a>'? '<a href="someother url">Some other text</a>' ; ?> Ken Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 Hi Thanks. Yes I apprecaite they are not right but I removed the urls for the pupose of this thread. The urls are correct on the script but its not picking up the variable from the if statement - as in, it is not doing one url for if the supplier value is 1 and another for if it is anything else. All products carry the same url with the above script but in the databse I have checked that there are both 1s and 0s in the supplier field. Hope this makes sense. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 23, 2008 Share Posted February 23, 2008 Can you post more of your script so we can see the whole switch statement? Ken Quote Link to comment Share on other sites More sharing options...
ollyno1uk Posted February 23, 2008 Author Share Posted February 23, 2008 This is a chunk. I have made a mess of my code so started again towards teh bottom is where the if lies for ($col=0, $n=sizeof($column_list); $col<$n; $col++) { $lc_align = ''; switch ($column_list[$col]) { case 'PRODUCT_LIST_MODEL': $lc_align = ''; $lc_text = ' ' . $listing['products_model'] . ' '; break; case 'PRODUCT_LIST_NAME': $lc_align = ''; if (isset($HTTP_GET_VARS['manufacturers_id'])) { $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>'; } else { $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> '; } break; case 'PRODUCT_LIST_MANUFACTURER': $lc_align = ''; $lc_text = ' <a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing['manufacturers_id']) . '">' . $listing['manufacturers_name'] . '</a> '; break; case 'PRODUCT_LIST_PRICE': $lc_align = 'right'; if (tep_not_null($listing['specials_new_products_price'])) { $lc_text = ' <s>' . $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</s> <span class="productSpecialPrice">' . $currencies->display_price($listing['specials_new_products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</span> '; } else { $lc_text = ' ' . $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . ' '; } break; case 'PRODUCT_LIST_QUANTITY': $lc_align = 'right'; $lc_text = ' ' . $listing['products_quantity'] . ' '; break; case 'PRODUCT_LIST_WEIGHT': $lc_align = 'right'; $lc_text = ' ' . $listing['products_weight'] . ' '; break; case 'PRODUCT_LIST_IMAGE': $lc_align = 'center'; if (isset($HTTP_GET_VARS['manufacturers_id'])) { $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>'; } else { $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a> '; } break; case 'PRODUCT_LIST_BUY_NOW': $lc_align = 'center'; //$lc_text = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $listing['products_id']) . '">' . tep_image_button('button_buy_now.gif', IMAGE_BUTTON_BUY_NOW) . '</a> '; $re = mysql_query("select * from products") or die (mysql_error()); if (mysql_num_rows($re)) { while ($products_info = mysql_fetch_array($re)) { if ($products_info['supplier']=='1') { $lc_text = '<a href="http://url here/url here" . <img style="border:0" src=includes/languages/english/images/buttons/button_buy_now.gif> </a>' ; break; } }else{ $lc_text = '<a href=url2>'; } } } $list_box_contents[$cur_row][] = array('align' => $lc_align, 'params' => 'class="productListing-data"', 'text' => $lc_text); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.