query results in one string

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
mixmuis
Forum Newbie
Posts: 1
Joined: Wed Nov 06, 2002 5:36 am

query results in one string

Post by mixmuis »

can i get the query result in 1 string ?

Code: Select all

$result = mysql_query("SELECT * FROM frame77"); 
for($i=0;$i < mysql_numrows($result);$i++){ 

$mod = "<a href='article.php?". mysql_result($result,$i,'name')."'>itemname</a><br>";

}

echo"$mod";
the $mod must be used in a other file, so i would like to use it as 1 string.

someone who can help me?
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

You could add each iteration to an array by changing

Code: Select all

$mod = "<a href='article.php?". mysql_result($result,$i,'name')."'>itemname</a><br>";
to

Code: Select all

$modї] = "<a href='article.php?". mysql_result($result,$i,'name')."'>itemname</a><br>";
(note square brackets)

Then

Code: Select all

$string = implode(" ", $mod);
to convert the array into a string, with each element seperated by a space. Then pass the string.

You could probably be more efficient by using SELECT name FROM frame77 instead of selecting everything. I suspect there is also a better way of looping, I prefer a different way, but if this is what you like, only change a little at a time or suddenly it won't work an you won't know why!
Post Reply