variable from a string


#1

simple little thing here, i haev this little string to get some results from the database

[php]for ($i = 0; $i < count($news); $i++) {
echo $news[$i][‘topic_title’];
};[/php]

however i would like to put the text that gives out into a variable, so if i was to call it later in the script, it would display the results there.

Thanks in advance


#2

[php]for ($i = 0; $i < count($news); $i++) {
echo $news[$i][‘topic_title’];
$SomeVariable = $news[$i][‘topic_title’];
};[/php]


#3

that only shows the last record. i need it to store all of them that are given out


#4

Am I missing something? Are they not already stored in the Array news? Just use that where you need it.


#5

Well this is using a php system called phpbb fetch all, put its the php bit i need. The answer you gave, gives the following…

[size=99px]fig1[/size]
news5

however if you let the string run, and echo it creates this…

[size=99px]fig2[/size]
news1news2new3news4news5

which is what i want

i think its overwriting the variable, when i want all of the output to be the variable so if i was to call the variable i would get fig2


#6

Then you want something like this:

[php]$SomeVariable = “”;
for ($i = 0; $i < count($news); $i++) {
echo $news[$i][‘topic_title’];
$SomeVariable .= $news[$i][‘topic_title’];
};[/php]


#7

Indeed my first suggestion was incorrect as it will only contain the LAST value from the array and (as it changes on each loop.)

However, if you are looking to have the entire thing, then you already have it. It’s in the array of $news. It just has 2 indexes. apparently the fiirst one is numerical and the second one is words.

i.e.
$NEWS[1][‘topic_title’]
$NEWS[1][‘topic_subject’]
$NEWS[2][‘topic_title’]
$NEWS[2][‘topic_subject’]
etc…

Just call the array again when and/or where you need it. You can either loop through the values or call them by their appropriate indexes.


#8

Da Warriah’s method worked best, bw for that, can you put bits between so if iwas to put a l in between, or would i have to use the second method


#9

oh don’t worry i worked it out, for thoose users who are interested. you list the variables

$SomeVariable = “”;
for ($i = 0; $i < count($news); $i++) {
$SomeVariable .= $news[$i][‘topic_title’];
$SomeVariable .= " l ";

};

thanks anyway to you two :) give yourselfs pats on the back