[PHP] String

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I have an example string that goes like this for instance:

Code:
$str = '34' . $text1 . '34' . $text2 .'23';

The variables $text1 and $text2 haven't been set yet and will be set later from a mysql query like this:

Code:
$str = '34' . $text1 . '34' . $text2 .'23';
$text1=mysql_result($sql,$i,"text1");
$text2=mysql_result($sql,$i,"text2");
echo $str;

How can I make it so that when it's echoing the string that it puts the variables in, as I want to be able to use the string in lots of different statements and queries.

I hope i explained this properly.

Thanks.
 
PHP:
$str = '34' . $text1 . '34' . $text2 .'23';
$text1=mysql_result($sql,$i,"text1");
$text2=mysql_result($sql,$i,"text2");
echo $str;

Should be:

PHP:
$text1=mysql_result($sql,$i,"text1");
$text2=mysql_result($sql,$i,"text2");
$str = '34' . $text1 . '34' . $text2 .'23';
echo $str;

I really cant see why you want to do it the other way...

If you need to do it your way you need to use eval().
 
Last edited:
In short it won't work unless you use something really, really messy like eval() and store the shebang as a literal string to then parse it later on, so are you sure you can't design the script differently? :)
 
Well, It's just I am using 5 (possibly more all with different results) of the exact same string, so I thought it would be more "efficient"?
 
Ah, I see. You could use some sort of mini template system, such as embedding something like %TEXT1% and %TEXT2% into the string, then doing a str_replace() for each value once you know them :)
 
Didn't want to start a new thread for something small.

But with PHP + MYSQL, can you make it only select the data in the for instance the bottom five records so it doesn't have to have all the data in memory for sorting later, would that be more efficient?

Currently i am using something similar to this to get the last 5 records after:

Code:
//* sort it decending in query *//


$i=0;
while ($i < 5) {
//* SQL Stuff *//
$i++;
}
 
Last edited:
You can choose which rows you'd like by using the LIMIT option in a SELECT query, which is written after the ORDER statement (ie, in 99% of cases, at the end of the query), for example...
Code:
SELECT field1, field2 from table WHERE field1 = 'x' ORDER BY field2 DESC LIMIT offset, rowcount

So the following would order by field2 then give you only the 1st - 5th rows of the result set:

Code:
SELECT field1, field2 from table WHERE field1 = 'x' ORDER BY field2 DESC LIMIT 0, 5

Infact if the offset is 0 you can also omit it :)
 
Back
Top Bottom