私はオンラインショップを書いていますが、誰かが私を助けてくれることを望んでいる機能の問題に遭遇しました。この機能は、製品が別の形式で存在するかどうかを判断し、サイドバーからそれらにリンクするように設計されています。たとえば、製品がDVD、Blu-Ray、およびデジタルダウンロードに存在する場合があります。
これは私の関数コードです:
function formatExists($format, $pid) {
global $conn; global $dbname; global $loginid;
mysql_select_db($dbname, $conn);
// Get author and description of the requested product
$query = "SELECT description, author FROM products WHERE productid = $pid";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$author = $row['author'];
$description = $row['description'];
}
// Find other products with the same description & author in requested format
$query = "SELECT productid FROM products WHERE format = $format AND description = '$description' AND author = '$author'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$rpid = $row['productid'];
}
if($rpid) {
return $rpid;
} else {
return 0;
}
}
コードの別の部分にループを記述しました。これは、データベースからすべての形式を取得し、それらに対して上記の関数を実行して、製品が使用可能な形式を見つけようとします。
$query = "SELECT formatid, description, postage FROM formats";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['formatid'];
$desc = $row['description'];
$post = $row['postage'];
... STUFF ...
}
'...STUFF...'が'$bodycontent。=$id'; "の場合、データベース内のすべてのフォーマットIDをエコーしました。これは、私が期待した動作です。ただし、次のように変更した場合:
$pid = formatExists($id, $productid);
if($pid) {
$query = "SELECT price FROM products WHERE productid = $pid";
$result = mysql_query($query);
$pricedata = mysql_fetch_array($result, MYSQL_ASSOC);
$pprice = $pricedata['price'];
$bodycontent .= "<span>";
if($pid != $productid) {
$bodycontent .= "<a href='$siteroot/index.php?page=product&id=$pid'>";
}
$bodycontent .= "$desc - $$pprice";
if($postage) {
$bodycontent .= " + P&P";
}
if($pid != $productid) {
$bodycontent .= "</a>";
}
$bodycontent .= "</span>";
}
目的の方法で動作を停止し、最初のフォーマットIDの応答を返し始めました。
'formatExists($ id、$ productid)'を'formatExists(2、$ productid)'に手動で変更すると、価格とリンクが更新されるため、関数は正しく機能します。ただし、何らかの理由で、カテゴリごとに1回実行されるのではなく、ループ内で1回だけ実行されます。
どんな助けでも大歓迎です。