Can you do this with PHP?

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
Take this for example:

PHP:
$query = 'SELECT field1, field2 FROM thisTable ORDER BY dateline LIMIT 5';

$resultSet = '<li><a href="$1">$2</a></li>';

I will want to replace whatever is in $1 and $2 with variables retrived from the query, such as
PHP:
while(GETeachRESULT)
{
// <li><a href="$1">$2</a></li>
// replace $1 with field1 result
// replace $2 with field2 result

// Echo result: <li><a href="3">This is a link</a></li>
}

So to cut it short, how can I replace $1, $2 with certain variables?
If the result set contains $3 then replace this with the field3 in a query. etc.

Or is there a better way to do it?

Thx
 
Last edited:
the sprintf function above is hard coded to take 2 variables, but if it had 3,4...N variables?

Is there not a way to use preg_replace?

replace $1, $2, ... $N in the resultSet with $array[$1] ?
 
Last edited:
Back
Top Bottom