[SOLVED] Making a text segment a variable for loop.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
feelthesun
Forum Newbie
Posts: 3
Joined: Wed Sep 29, 2004 3:46 pm
Location: Indianapolis, IN
Contact:

[SOLVED] Making a text segment a variable for loop.

Post by feelthesun »

Hey everyone, new to PHP (and this forum!), 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

Code: Select all

<?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++; 
} 
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Do you mean like:

Code: Select all

$i=0; 
for($i=0;$i<$num;$i++)
{ 
 $name=mysql_result($result,$i,"name"); 
 $title=mysql_result($result,$i,"title"); 
 $story=mysql_result($result,$i,"story"); 
?>
feelthesun
Forum Newbie
Posts: 3
Joined: Wed Sep 29, 2004 3:46 pm
Location: Indianapolis, IN
Contact:

Post by feelthesun »

I don't know what I mean. *L* I think I have an online friend helping me at the moment. The code at the bottom takes a long text from the database and cuts it off at the 20 word mark, but I need to put that in a loop so that it does it for every entry in the database.
Joe wrote:Do you mean like:

Code: Select all

$i=0; 
for($i=0;$i<$num;$i++)
{ 
 $name=mysql_result($result,$i,"name"); 
 $title=mysql_result($result,$i,"title"); 
 $story=mysql_result($result,$i,"story"); 
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Mabey you should think of something along the lines of:

Code: Select all

<?php
while (true) 
{ 
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $name=$row['name'];
 $title=$row['title'];
 $story=$row['story']; 
?>
feelthesun
Forum Newbie
Posts: 3
Joined: Wed Sep 29, 2004 3:46 pm
Location: Indianapolis, IN
Contact:

Post by feelthesun »

Thanks for the help. Resolved!
Post Reply