Jump to content

Newbie issue driving me mad


ollyno1uk

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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")));

Link to comment
Share on other sites

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 &#160;href="some url"</a>' ;
}else{
$lc_text = '<a href="some url" </a>' ;
}

}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.