Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Making a text segement a variable for loop.

    Hey everyone, new to PHP, and having issues with this, and none of my PHP buddies are online to help me out, so I was hoping someone on here might be able to tell me real quick. I don't think what I'm trying to do is hard. Or maybe it is. At the bottom of the code, there's a chunk that makes a "text segement" for a longtext field in the database. I need to make it so that it is a variable that I can loop.

    Can anyone help me format it to a variable that I can loop? Sounds simple to me, but I can't figure it out, but I've only been at PHP for a few weeks, so I'm still trying to figure it all out.

    Thanks in advance for any help you can give me!

    S

    PHP Code:
    <?php

    $username
    ="blood_admin";
    $password="*********";
    $database="blood_bands";

    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die("Unable to select databse");

    $query="SELECT * FROM stories";

    $result=mysql_query($query);

    $num=mysql_num_rows($result);

    ?>

    <?
    $i
    =0;
    while (
    $i $num) {

    $name=mysql_result($result,$i,"name");
    $title=mysql_result($result,$i,"title");
    $story=mysql_result($result,$i,"story");

    ?>

    <? 
            $str 
    "$story";
            
    $no_of_words 20;
            
    $str_array split(" ",$str,$no_of_words+1);
            if(
    count($str_array)>$no_of_words) {
                    for(
    $i=0$i $no_of_words$i++) {
                            echo 
    $str_array[$i]." "; }
                    echo 
    "<b>...........(more)</b>";}

    ?>

    <?
    $i
    ++;
    }
    ?>

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what do you mean by "a variable that i can loop" ? Do you wanna repeat the value of that variable or so to make a long text ? Like
    PHP Code:
    $text 'bla';
    $longtext str_repeat($text10000); 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    My stray guess would be that he would want to display the first 20 words and a more link if the text is longer.
    That does not require a loop:
    PHP Code:
    <?
    $story 
    'This is my ultra long text of which I want to see only the first 20 words. I will try to get it working without loops.';

    $story explode(' '$story21);
    $rest_of_story array_pop($story);
    $cut_stroy implode(' '$story) . '... More';
    print 
    $cut_stroy;
    print 
    '<br>' "\n" $rest_of_story;
    ?>
    You will need some sort of regex to avoid multiple spaces put together and you could trim() all those whitespaces from the beginning and end of the text.

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    preg_replace('/\s{2,}/', ' ', $story); should work to remove double spaces. (or more.)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •