doulangbi6869 2012-06-11 16:01
浏览 40
已采纳

如何使用一个参数从PHP中的url中识别variables_x列表?

I have a members site where users are given up to 7 web page templates to display their products on. Each template has the same field names, differentiated by _1, _2... _7 at the end of each.

For example:

// Template 1 would have the following list of variables
product_name_1
product_descript_1
product_color_1
product_price_1

// Template 2 would have the following list of variables
product_name_2
product_descript_2
product_color_2
product_price_2

// through Template 7

I am able to display any variables for a specific user within a web page, by use of a query string identifying their user_id in the url, ie

http://domain.com/folder/page.php?id=78

I then $_Get any variable by simply identifying it in the PHP file, ie

$product_name_1
$product_descript_1
$product_color_1
$product_price_1

My problem is that the url must identify WHICH template, since each template identifies a specific product, ie _1, _2, ..._7. How do I use a parameter in the url, such as

http://domain.com/folder/page.php?id=78&parameter=_7

...to identify all variables ending with _7, which would appear in the web page? The parameter used in the url would identify the variables to be used in the web page, whether _1, _2, etc.

UPDATE

I have tried the various answers with only partial success, ie "Array_x" is displayed when using any particular variable along with the suggested code. There may be a conflict with the rest of the code I'm using in page.php, as follows:

$db_connection = new mysqli("", "", "");
if ($db_connection->connect_errno) {
   echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM table_name WHERE id = '$id' LIMIT 1") or die(mysql_error()); 
$row = mysql_fetch_object($query);

$prop_address=array(
"_1"=>"prop_address_1",
"_2"=>"prop_address_2",
"_3"=>"prop_address_3"
//Through to Temp 7
);

$prop_address{$_GET['parameter']}=$row->prop_address;

    echo " $prop_address{$_GET['parameter']} ";

"Array_x" displays (where x=1, 2, 3, etc is used as the parameter in url, ie http://domain.com/page.php?id=72&parameter=1), instead of the actual value held in the database table for $product_name{$_GET['parameter']}. For some reason, the code is not picking up the value of the variable from the database table.

  • 写回答

3条回答 默认 最新

  • dongsong8932 2012-06-19 14:41
    关注

    I couldn't get any of the answers given to work. I found an example given by a user for php variable variables in the PHP manual here and found it to work. I incorporated it into my code as follows:

      $id = $_GET['id']; 
      $query = mysql_query("SELECT * FROM table_name WHERE id = '$id' LIMIT 1") or die(mysql_error()); 
      $row = mysql_fetch_object($query);
    
      for( $i = 1; $i < 8; $i++ )
      {
      $product_name[$_GET['parameter']] = "product_name_" . $i; 
      $product_descript[$_GET['parameter']] = "product_descript_" . $i; 
      $product_color[$_GET['parameter']] = "product_color_" . $i; 
      $product_price[$_GET['parameter']] = "product_price_" . $i; 
      }
      ${$product_name[1]}  = "$row->product_name_1"; 
      ${$product_name[2]}  = "$row->product_name_2"; 
      ${$product_name[3]}  = "$row->product_name_3"; 
      ${$product_name[4]}  = "$row->product_name_4"; 
      ${$product_name[5]}  = "$row->product_name_5"; 
      ${$product_name[6]}  = "$row->product_name_6"; 
      ${$product_name[7]}  = "$row->product_name_7"; 
    
      // List 7 variables and values for each field name
    
      echo "${$prop_name[$_GET['par']]}";
    

    The only problem is that mysql injection is possible with this code. If anyone could suggest a solution, I would greatly appreciate it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?