Jump to content



Latest News: (loading..)

- - - - -

SQL help -- selecting default product description


  • Please log in to reply
6 replies to this topic

#1   rudolfl

rudolfl
  • Members
  • 126 posts
  • Real Name:Rudolf Ladyzhenskii
  • Gender:Male
  • Location:Melbourne, Australia

Posted 11 July 2012 - 02:34 AM

Hi, all
Consider situation -- site offers multiple languages, but product description only exists in "English" for some products. Let's say I am viewing shop, selecting "German". If product description is available in German, it will show. If this field is blank, nothing will be shown.

What I want is to check whether description is available in language selected. If yes, use it, if not -- use one from default language.
I wan to implement this as a SQL query. This way, PHP code will not have to change.

I am struggling to figure out how to do it. Can anyone help?

Thanks,
Rudolf

#2   DunWeb

DunWeb

    The Censored One

  • Members
  • 12,720 posts
  • Real Name:Chris
  • Gender:Male
  • Location:Ontario, Canada

Posted 11 July 2012 - 03:23 AM

@rudolfl

I don't see how an SQL query would check to see if a description is available in the selected language PHP code edits as well to output the results.



Chris

Edited by DunWeb, 11 July 2012 - 03:25 AM.

:|: Was this post helpful ? Click the LIKE THIS button :|:

See my Profile (click here)

#3   rudolfl

rudolfl
  • Members
  • 126 posts
  • Real Name:Rudolf Ladyzhenskii
  • Gender:Male
  • Location:Melbourne, Australia

Posted 11 July 2012 - 05:04 AM

There are conditional statements in SQL one can use and I got something working, but not fully.
SQL can extract one or another based on condition (I used IF inside WHERE), but then for descriptions that do exist in both languages, I am getting both in the result.

I did not include my query on purpose, as i want to see what other people think first.

Rudolf

#4   FWR Media

FWR Media
  • Community Sponsor
  • 6,836 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 11 July 2012 - 08:15 AM

You really should populate the database correctly, perhaps a script to fill the missing descriptions.

Failing that you can try the following untested code: -

Find product_info.php ( about line 135 ) ..

<?php echo stripslashes($product_info['products_description']); ?>

Replace with ...

<?php
  if ( !tep_not_null( $product_info['products_description'] ) ) {
	// Where 1 is the default language id
	$query = "SELECT products_description FROM `products_description` WHERE language_id = 1 AND products_id = " . (int)$HTTP_GET_VARS['products_id'];
	$result = tep_db_query( $query );
	$row = tep_db_fetch_array( $result );
	$product_info['products_description'] = ( array_key_exists ( 'products_description', $row ) && tep_not_null( $row['products_description'] ) ) ?
	$row['products_description'] : '';
	tep_db_free_result( $result );
  }
  echo stripslashes ( $product_info['products_description'] );
?>

Edited by FWR Media, 11 July 2012 - 08:19 AM.


#5   bruyndoncx

bruyndoncx

    Problem Thinker

  • Members
  • 2,656 posts
  • Real Name:Carine Bruyndoncx
  • Gender:Female
  • Location:Belgium/ Antwerp/ Turnhout/ Arendonk

Posted 11 July 2012 - 08:15 PM

my solution to this problem where I have some descriptions in english but not in dutch and since almost everyone understands english, I implemented this
	//CB multi-language
	if ( ($product_info['products_description'] == '') or (empty($product_info['products_description'])) ){
		if ($languages_id == '4') { $other_language = '1'; } else {$other_language = '4'; }
		$other_lang_query = tep_db_query("select pd.products_description from " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.products_id = '" . (int)$_GET['products_id'] . "' and pd.language_id = '" . $other_language . "'");
		$other_lang_result = tep_db_fetch_array($other_lang_query);
		if ( ($other_lang_result['products_description'] != '') and (!empty($other_lang_result['products_description']))){
			$product_info['products_description'] = '<br /><hr />'. TEXT_NO_DESCRIPTION_IN_THIS_LANGUAGE . '<hr /><br />' . $other_lang_result['products_description'];
		}
	}


Hava a nice day !
Carine Bruyndoncx


PS frustraded by the lack of feedback
<!--
Did you know 99% of all people benefiting from my posts, won't bother to repay the favor.
Wouldn't it be great if you are part of that exclusive 1% ?
post your findings in my responsive liive shop review thread ?
-->

#6   rudolfl

rudolfl
  • Members
  • 126 posts
  • Real Name:Rudolf Ladyzhenskii
  • Gender:Male
  • Location:Melbourne, Australia

Posted 11 July 2012 - 11:09 PM

Thanks to all.

I have implemented something similar, but was wondering if it is possible to do by using SQL only. I guess, the answer is no. Anyway, now I know. (Oracle actually have functionality to do those sort of things -- friend who is working with Oracle told me so).

Rudolf

#7   bruyndoncx

bruyndoncx

    Problem Thinker

  • Members
  • 2,656 posts
  • Real Name:Carine Bruyndoncx
  • Gender:Female
  • Location:Belgium/ Antwerp/ Turnhout/ Arendonk

Posted 12 July 2012 - 12:04 AM

SQL inspiration - in tep_db_query format for php, notice the if construction , the group by products_id to retrieve just 1 line, and the having, though in your case this is optional, this report for me lists missing descriptions and shows any existing description to help translation efforts.
I think you just need the  if, and group by in your product_info page

HTH

[code]
tep_db_query("select p.products_status, p.products_id, p.products_model, pd.products_name,
    if(pd.language_id = 1, pd.products_description, '') as EN,
    if(pd.language_id = 4, pd.products_description, '') as NL, p2c.categories_id
    from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, products_to_categories p2c
    where p2c.products_id = p.products_id and p.products_id = pd.products_id
                                and p.products_status = 1
    group by products_id
having (EN is null or EN = '' or NL is null or NL = '' )  
");
Hava a nice day !
Carine Bruyndoncx


PS frustraded by the lack of feedback
<!--
Did you know 99% of all people benefiting from my posts, won't bother to repay the favor.
Wouldn't it be great if you are part of that exclusive 1% ?
post your findings in my responsive liive shop review thread ?
-->