Jump to content


Photo

Retreive first characters from a field


  • Please log in to reply
8 replies to this topic

#1 nicuz

nicuz
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 June 2006 - 06:01 AM

I have a field in my database called "description" (type:longtext) and I would like to retreive the first let's say 13 characters. What wold the select query be?

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 June 2006 - 06:11 AM

If you mean the first 13 entrys use limit 13
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 nicuz

nicuz
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 June 2006 - 06:37 AM

No. The first 13 characters.

Eg: in the field I have stored: I love php and I love programming.

What I want is to select the first 13 char. which are: I love php an

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 June 2006 - 06:50 AM

$text = "$description"; echo ShortenString($text);
function ShortenString($text)
{
// Number of characters you want to display
$num_chars = 100;
$text = $text." ";
$text = substr($text,0,$num_chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 never_rain

never_rain
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 30 June 2006 - 06:54 AM

Ok, here is the solution.

$description = 'I love PHP and I love Programming';

$description1 = substr($description,0,13);

echo $description1; \\ the result will be first 13 characters of your string


Faisal Niaz[br]Digitalspot Solutions

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 June 2006 - 06:57 AM

$text = "$description";
echo ShortenString($text);
function ShortenString($text)
{
// Number of characters you want to display
$num_chars = 100;
$text = $text." ";
$text = substr($text,0,$num_chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
?> 
$num_chars = 100; // change to number of charecters.
$text = "$description"; text = database varable.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 June 2006 - 06:59 AM


please look up substr ok

http://uk.php.net/substr
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 never_rain

never_rain
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 30 June 2006 - 07:31 AM

I have another problem using substr() my description field is stored in database and is non english language. I used html editor to store the values. When I type any Character in the editor and dont use formatings from html editor, the values displayed corectly. But when I do Formating and apply these settings, nothing is shown.  this means, when I use substr function, it has some confliction with the html stored in the database. here is an example. field name is description and the value from database is as follows

This value I copied Paste and used HTML Editor to increase / decrease size etc


$description = <P class=MsoNormal dir=rtl style="MARGIN: 0cm 0cm 0pt; DIRECTION: rtl; unicode-bidi: embed; TEXT-ALIGN: justify"><SPAN lang=AR-AE style="FONT-SIZE: 14pt; FONT-FAMILY: 'Simplified Arabic'; mso-bidi-language: AR-AE">ÊÎÊÕ ÅÏÇÑÉ ÇäÈÍèË èÇäÇÓÊÔÇÑÇʬ ÇäÊê ÊÙÏ åæ ÇäÅÏÇÑÇÊ ÇäÑÆêÓêÉ ÈÇäåÙçϬ ÈÇäÅÔÑÇá èÇäÊèÌêç èÇäÊæÓêâ Èêæ ÇäÃæÔ×É ÇäåÎÊäáÉ ÇäåÑÊÈ×É ÈåÌÇäÇÊ ÇäÈÍèË èÇäÇÓÊÔÇÑÇÊ èÇäåãÊÈÇÊ èÇäèËÇÆâ èÇäæÔÑ</SPAN><SPAN lang=AR-SA style="FONT-SIZE: 14pt; FONT-FAMILY: 'Simplified Arabic'">.èÊäÙÈ ÇäÅÏÇÑÉ ÏèÑÇ åÍèÑêÇ áê ÊÍâêâ ÇäåÙçÏ äÃçÏÇáç èÊæáêÐ ÇäåçÇå ÇäÊê Ãæê×Ê Èç áê åÌÇä ÇäÇÑÊâÇÁ ÈåÓÊèé ÇäÅÏÇÑÉ ÇäÍãèåêÉ èÊÈÓê× ÅÌÑÇÁÇÊ ÇäÙåä ¬ è ÇäåÓÇçåÉ ÇäáÙÇäÉ áê ÇäÊæØêå ÇäÅÏÇÑê&nbsp; åæ ÎäÇä ÅÙÏÇÏ ÇäçêÇãä ÇäÊæØêåêÉ èÊâÏêå ÇäÇÓÊÔÇÑÇÊ ÇäÅÏÇÑêÉ èÅÌÑÇÁ ÇäÈÍèË èèÖÙ ÇäÍäèä ÇäåäÇÆåÉ ääåÔãäÇÊ ÇäÊê ÊÙèâ ÇäÙåä áê ÇäÃÌçÒÉ ÇäÍãèåêÉ ÇäÇÊÍÇÏêÉ èÇäåÍäêÉ .</SPAN><SPAN dir=ltr style="FONT-SIZE: 14pt; mso-bidi-font-family: 'Simplified Arabic'"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>

These Values dont show any thing in the Page.

Here is another record in whcih I didn't use any HTML editor functionalities. Just type the values and clicked submit. Here is how it looks.

$description = <P>æåÊåæÊæå&nbsp; ÇÊæÇ æÊÇæ ÊÇæ ÇçÙ Ç ÇÙç ÇÚÙç ÚçÙ Ú Ù ÈÑäÇ ÑÊÇ äÇÊæ äÇçÊ Çä ÙÚä ÙÚ ÊÇääÇÙçä Ùç äÙç ÇÙç ÇÊ äÇÑ ÇÊääÇ&nbsp; äÊæÇ Ùç ÇÙç ÇÙç ÇÙç ä Úä ÙáÚÈÑ áÚÈÄ ÚÑ&nbsp; ÑÇÊäÇ ÊäÇ ÊÇäÇ&nbsp; äÑÚÙ äÙÚ äÙÚ È áÚÙ Êæ æÊ æ ÊÇ ÊÇÑ äÇÑ Ù ÑÙ ÑÙÚ ÑÙÚ Ñ ÙÚ ÙÚäÇ ÙäÇ ÙÚ äÇÙ äÇ ÙÚäÇ ÙÚäÇÑ ÙÚ ÈÑÚ ÄáâÄ áâ Äá ä Ñ ÊÇ ÊÇ ÊÇ äÇ ÇäÇ ÙÚ äÚÙ äÙÚ äÙÚ äÙÚ ä ÚÙä ÙÚä È âáÈê â êâêâê â êâ êËâ&nbsp; È ÄÈ ÄÈ Ä ä ÑÄä ÑäÇ Ñ äÇÑ äÄ&nbsp; áÄ âê â â&nbsp; È äÑ Ç Ñ ÇÑ Ç Ç äÇä Ç äÇ äÇ ä Çä Çä Ç ÚÈ&nbsp; â êÄâ ê âê Äâ ÄÈâ Ä âÄ âÄ â Äâ Äâ Äâ Ä âÄ â Äâ Äâ Äâ Ä âÄ&nbsp;&nbsp;&nbsp;&nbsp; åÊ æåÊ æåÊ æå Êæå Êæå ÊçÎÇ ÎçÇ Ùç ÇÙç äç äÙçÚä ÙÚçä ÙçÚä çÙä çÙä çÙ ä çÚä&nbsp; äÇ éÉäÇ ÇäÇ ÙÊÇäÇÑ ÙÚ ÙÚ </P>

This value is shown perfectly. What I have figured out is that there is some conflict of HTML codes used above with substr and thats why it didn't show any thing. Do you have any idea what happened wit this.
Faisal Niaz[br]Digitalspot Solutions

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 June 2006 - 07:49 AM

lets get this stright you added html to the database? why?

does the code work as a normal insert without html inserted.


when you add data to the database you need to addslahes ok

addslases($text);


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users