[ask-php] Variable on Variable


Status
Not open for further replies.

dpnux

Expert 1.0
Buset dah ... gue kira loe mau buat runnable php script hahahaha. kalau begitu mah, mendingan loe buat array dan replacementnya, nggak usah pake eval. eval mengurangi perfomance soalnya :).

PHP:
<?php 

//default
$app_id = 1;
$user_id = 1;

//contoh kalau ada parameter dari form yang bisa disetup
if(isset($_POST['uid']) && is_numeric($_POST['uid'])) $user_id = $_POST['uid'];

//keyword yang bos definisikan 
$keyword = array(':app_id', ':user_id');
//replacement untuk keyword yang telah di definisikan, kalau di atas 2, di sini kudu 2 juga
$replace = array($app_id, $user_id);

$get_query = 'SELECT * FROM ants_apps WHERE app_id = :app_id';
//fill the new variable and show em
echo $getDbQuery = str_replace($keyword, $replace, $get_query);

//run your query here
$hasil2 = mysql_query($getDbQuery) or die('awk, mati gue : '. mysql_error()); 
$query_saved2 = mysql_fetch_array($hasil2); 
echo $query_saved2['app_name'];

ada cara lain. bisa menggunakan preg_replace_callback kemudian di explode misalnya ["app_id":"1"] atau ["user_id":"10"], ini sudah masuk ke parsing jatuhnya :).

ini proyek iseng khan, bukan PR dari klien? hahaha

links:
http://www.jonasjohn.de/snippets/php/preg-replace-callback.htm
http://blog.joshuaeichorn.com/archives/2005/08/01/using-eval-in-php/
 

anthoplara

Beginner 2.0
haha,, PR dari client dah kelar seminggu kemaren Om,, ini iseng aza daripada capek komplen ke hostingan mulu...

Punya saya juga marsing
PHP:
 $no_id = 1; 
$get_query = str_replace(':numb', "$no_id", $get_query);
slain pake parsing, ada cara lain ga ea.??
 
Status
Not open for further replies.

Top