Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-26-2010, 01:29 AM
Kyle55155 Kyle55155 is offline
 
Join Date: Mar 2010
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Need a little code

I'm using this code to display forum thread titles on a custom vb page:
<script type="text/javascript" src="external.php?forumids=111,112,113,114,115,133 ,148,154&type=js"></script>
<script language="" type="text/javascript">
<!--
for (x = 0; x < 3; x++)
{
document.writeln("<a href=\"showthread.php?t="+threads[x].threadid+"\">");

document.writeln("<b>"+threads[x].title+"</b><br />");

document.writeln("</a>");
document.writeln("Posted "+threads[x].threaddate+" at "+threads[x].threadtime+" by "+threads[x].poster+"<br />");
}
//-->
</script>

I'm trying to figure out how to limit the number of characters that are displayed in the title.

document.writeln("<b>"+threads[x].title+"</b><br />");

I tried inserting this code inside the javascript but I can't seem to get it to work
<?php
$len = 30;

if (strlen($thread['title']) > $len) {
$thread['title'] = substr($thread['title'],0,$len);
$thread['title'] .= "...";
} else {
$thread['title'] = $thread['title'];
}
?>

Ideally I would like to limit the character length of threads.title and append a ... when it gets cut off.

Any coders out there have a solution for me?

Thanks
Reply With Quote
  #2  
Old 07-26-2010, 02:29 AM
Guest190829
Guest
 
Posts: n/a
Default

The problem is you are trying to use PHP within javascript; luckily, javascript provides the same string manipulation functions that you need. The following should work (I don't know if you explicitly need to declare the string as an object or not, let me know if it isn't working):

Code:
var max_title_length = 30;

for(...) {
    
     if(threads[x].title.length > max_title_length)
    {
        threads[x].title = threads[x].title.substr(0, max_title_length) + '...';
    }

    // ... format/output code
}
Reply With Quote
  #3  
Old 07-26-2010, 05:32 AM
Kyle55155 Kyle55155 is offline
 
Join Date: Mar 2010
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks Danny,

I entered your code exactly as you posted but it didn't work. I was able to get the following code to work:

<script language="" type="text/javascript">
<!--
for (x = 0; x < 3; x++)
{
document.writeln("<a href=\"showthread.php?t="+threads[x].threadid+"\">");

var tread = threads[x].title;

if (tread.length > 30)
{
document.writeln(tread.substring(0,30))
document.writeln("...&nbsp;")
}
else
{

document.writeln(tread)
document.writeln("&nbsp;")

}

document.writeln("</a>");
document.writeln("Posted "+threads[x].threaddate+" at "+threads[x].threadtime+" by "+threads[x].poster+"<br />");
}
//-->
</script>

I'm sure it could be cleaned up.

One thing I still need is code that will count out to the character limit and then break at the end of the closest word such that the last word in the string is not a partial word.

Any idea how to add that?

Thanks
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:56 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05718 seconds
  • Memory Usage 2,174KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (3)post_thanks_box
  • (3)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit_info
  • (3)postbit
  • (2)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete