特定の条件に基づいて、phpスクリプトのページを表示する必要があります。if条件があり、条件が満たされた場合に「インクルード」を実行しています。
if(condition here){
include "myFile.php?id='$someVar'";
}
問題は、サーバーにファイル「myFile.php」があることですが、引数(id)を使用してこのファイルを呼び出したいので、「id」の値は呼び出すたびに変わります。
誰かがこれを達成する方法を教えてもらえますか?ありがとう。
特定の条件に基づいて、phpスクリプトのページを表示する必要があります。if条件があり、条件が満たされた場合に「インクルード」を実行しています。
if(condition here){
include "myFile.php?id='$someVar'";
}
問題は、サーバーにファイル「myFile.php」があることですが、引数(id)を使用してこのファイルを呼び出したいので、「id」の値は呼び出すたびに変わります。
誰かがこれを達成する方法を教えてもらえますか?ありがとう。
回答:
インクルードが何であるかを想像してみてください。インクルードされたPHPファイルの内容のコピー&ペーストで、その後解釈されます。スコープの変更はまったくないため、インクルードされたファイルの$ someVarに直接アクセスできます($ someVarをパラメーターとして渡すか、いくつかのグローバル変数を参照するクラスベースの構造を検討する場合でも)。
あなたはあなたが求めている効果を達成するためにこのようなことをすることができます:
$_GET['id']=$somevar;
include('myFile.php');
ただし、これにはある種の関数呼び出しのようなインクルードを使用しているようです(異なる引数を使用して繰り返し呼び出すことに言及しています)。
この場合、一度含まれ、複数回呼び出される通常の関数に変えてみませんか?
include("myFile.php?id=$somevar");同一ですか?
include("myFile.php?...意図したとおりに機能しません。
このインクルードをPHPファイルに手動で書き込む場合は、Daffの答えが最適です。
とにかく、最初の質問を実行する必要がある場合は、それを実現するための小さな単純な関数を次に示します。
<?php
// Include php file from string with GET parameters
function include_get($phpinclude)
{
// find ? if available
$pos_incl = strpos($phpinclude, '?');
if ($pos_incl !== FALSE)
{
// divide the string in two part, before ? and after
// after ? - the query string
$qry_string = substr($phpinclude, $pos_incl+1);
// before ? - the real name of the file to be included
$phpinclude = substr($phpinclude, 0, $pos_incl);
// transform to array with & as divisor
$arr_qstr = explode('&',$qry_string);
// in $arr_qstr you should have a result like this:
// ('id=123', 'active=no', ...)
foreach ($arr_qstr as $param_value) {
// for each element in above array, split to variable name and its value
list($qstr_name, $qstr_value) = explode('=', $param_value);
// $qstr_name will hold the name of the variable we need - 'id', 'active', ...
// $qstr_value - the corresponding value
// $$qstr_name - this construction creates variable variable
// this means from variable $qstr_name = 'id', adding another $ sign in front you will receive variable $id
// the second iteration will give you variable $active and so on
$$qstr_name = $qstr_value;
}
}
// now it's time to include the real php file
// all necessary variables are already defined and will be in the same scope of included file
include($phpinclude);
}
?>
私はこの変数変数の構成を頻繁に使用しています。
これはしばらく前のことですが、Iamは、これを処理する最善の方法がbeセッション変数を利用することであるかどうか疑問に思っています。
myFile.phpには、
<?php
$MySomeVAR = $_SESSION['SomeVar'];
?>
そして、呼び出しファイルで
<?php
session_start();
$_SESSION['SomeVar'] = $SomeVAR;
include('myFile.php');
echo $MySomeVAR;
?>
これは、プロセス全体を機能化するための「提案された」必要性を回避しますか?
複数のフィールドセットを含めるajaxフォームを実行しているときに、これに遭遇しました。雇用申請書を例にとってみましょう。私は1つのプロのリファレンスセットから始め、「さらに追加」というボタンがあります。これは、$ countパラメーターを使用してajax呼び出しを実行し、入力セット(名前、連絡先、電話など)を再度含めます。これは、次のようなことを行うため、最初のページの呼び出しで正常に機能します。
<?php
include('references.php');`
?>
ユーザーがajax呼び出しを行うボタンを押すと 、references.phpファイル内に次のようなものがあります。ajax('references.php?count=1');
<?php
$count = isset($_GET['count']) ? $_GET['count'] : 0;
?>
また、パラメーターを渡すサイト全体に、このような他の動的インクルードがあります。この問題は、ユーザーが送信を押してフォームエラーが発生した場合に発生します。したがって、動的にインクルードされる追加のフィールドセットを含めるためにコードを複製しないように、適切なGETパラメーターを使用してインクルードを設定する関数を作成しました。
<?php
function include_get_params($file) {
$parts = explode('?', $file);
if (isset($parts[1])) {
parse_str($parts[1], $output);
foreach ($output as $key => $value) {
$_GET[$key] = $value;
}
}
include($parts[0]);
}
?>
この関数はクエリパラメータをチェックし、それらを$ _GET変数に自動的に追加します。これは私のユースケースではかなりうまくいきました。
呼び出されたときのフォームページの例を次に示します。
<?php
// We check for a total of 12
for ($i=0; $i<12; $i++) {
if (isset($_POST['references_name_'.$i]) && !empty($_POST['references_name_'.$i])) {
include_get_params(DIR .'references.php?count='. $i);
} else {
break;
}
}
?>
特定のユースケースに対応するためにGETパラメータを動的に含めるもう1つの例。お役に立てれば。このコードは完全な状態ではないことに注意してください。ただし、これで、誰もがユースケースをうまく始めることができます。
他の誰かがこの質問に答えている場合、使用するときにinclude('somepath.php');そのファイルに関数が含まれている場合は、varもそこで宣言する必要があります。を含めると$var=$var;常に機能するとは限りません。これらを実行してみてください:
one.php:
<?php
$vars = array('stack','exchange','.com');
include('two.php'); /*----- "paste" contents of two.php */
testFunction(); /*----- execute imported function */
?>
two.php:
<?php
function testFunction(){
global $vars; /*----- vars declared inside func! */
echo $vars[0].$vars[1].$vars[2];
}
?>
これを行う:
NSString *lname = [NSString stringWithFormat:@"var=%@",tname.text];
NSString *lpassword = [NSString stringWithFormat:@"var=%@",tpassword.text];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://localhost/Merge/AddClient.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"insert" forHTTPHeaderField:@"METHOD"];
NSString *postString = [NSString stringWithFormat:@"name=%@&password=%@",lname,lpassword];
NSString *clearpost = [postString stringByReplacingOccurrencesOfString:@"var=" withString:@""];
NSLog(@"%@",clearpost);
[request setHTTPBody:[clearpost dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:clearpost forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@",request);
そして、insert.phpファイルに追加します。
$name = $_POST['name'];
$password = $_POST['password'];
$con = mysql_connect('localhost','root','password');
$db = mysql_select_db('sample',$con);
$sql = "INSERT INTO authenticate(name,password) VALUES('$name','$password')";
$res = mysql_query($sql,$con) or die(mysql_error());
if ($res) {
echo "success" ;
} else {
echo "faild";
}