-2

我试图从函数中调用 $row (数据库数组),但基于变量调用列名。

这是完整的代码...您可以在以下位置查看结果

http://tlcs.stuart-pinfold.co.uk/test.php?id=20

http://tlcs.stuart-pinfold.co.uk/test.php?id=21

<?php
include("includes/db.php"); // includes all the db connections

$id = $_GET['id'];

$sql = "SELECT * FROM users WHERE UserID='".$id."'";
$set = mysql_query($sql);
$row = mysql_fetch_array($set);

function checkAvailability($day)
{
 $check = "UserCommute".$day;
 if($row[$check]=="1")
 {
  echo '<img src="/tick.jpg" alt="Available on this day" />';
 }
 else
 {
  echo '<img src="/cross.jpg" alt="Not available on this day" />';
 }
 echo " = ".$check."<br/>";
}

echo "Full Name:<br/>".$row['UserFullName']; // this works perfectly

echo "<br/><br/>Using the function...<br/>";

echo checkAvailability('Mon');
echo checkAvailability('Tue');
echo checkAvailability('Wed');
echo checkAvailability('Thu');
echo checkAvailability('Fri');
echo checkAvailability('Sat');
echo checkAvailability('Sun'); // these always return a cross even when the database entry is 1

echo "<br/>Using hard-coded row values...<br/>";

echo $row['UserCommuteMon']." = UserCommuteMon<br/>";
echo $row['UserCommuteTue']." = UserCommuteTue<br/>";
echo $row['UserCommuteWed']." = UserCommuteWed<br/>";
echo $row['UserCommuteThu']." = UserCommuteThu<br/>";
echo $row['UserCommuteFri']." = UserCommuteFri<br/>";
echo $row['UserCommuteSat']." = UserCommuteSat<br/>";
echo $row['UserCommuteSun']." = UserCommuteSun<br/>"; // these work perfectly, returning 0s or 1s, matching the database

?>

它只返回十字架,从不蜱。

我哪里错了?

4

3 回答 3

5

您正在定义一个函数,在函数内部没有$row.

要么在那里定义一个新查询,以检查用户在给定日期是否可用,要么删除该函数并比较没有该函数的字段。

所以不checkAvailability('Mon');要这样做:

if($row["UserCommuteMon"] === 1) {

}

或赋予$row您的功能。喜欢checkAvailability($row, 'Mon');

并记得清理你的$_GET['id']以防止 SQL 注入。

于 2012-12-16T10:35:07.647 回答
1

您的代码中存在多个问题,可以大大改进。

考虑切换到匿名函数:

$checkAvailabilityDay = function($day) use ($row)
{
    $key   = "UserCommute$day";
    $check = $row[$key] == 1;
    echo '<img src="/', $check ? 'tick' : 'cross', '.jpg" alt="',
         $check ? 'Available' : 'Not available', ' on this day" />',
         "$key<br />\n";
}

它们允许从定义它们的范围内使用(导入)变量。它在这里用于$row变量。这些函数可以像任何其他函数一样使用,例如作为所有日期输出的回调:

array_map($checkAvailabilityDay, ['Mon', Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']);

我希望这是有帮助的。您也应该mysql_*出于多种原因放弃,考虑切换到 PDO 或 mysqli。

于 2012-12-16T10:57:24.957 回答
0

谢谢大家。我最终(在我看来)效率低得多:

<?php

if($row['UserCommuteMon']===1)
{
 echo '<img src="/tick.jpg" alt="Travels on this day" class="availIcon" />';
}
else
{
 echo '<img src="/cross.jpg" alt="Does not travel on this day" class="availIcon" />';
}
echo "Mon";

if($row['UserCommuteTue']===1)
{
 echo '<img src="/tick.jpg" alt="Travels on this day" class="availIcon" />';
}
else
{
 echo '<img src="/cross.jpg" alt="Does not travel on this day" class="availIcon" />';
}
echo "Tue";

if($row['UserCommuteWed']===1)
{
 echo '<img src="/tick.jpg" alt="Travels on this day" class="availIcon" />';
}
else
{
 echo '<img src="/cross.jpg" alt="Does not travel on this day" class="availIcon" />';
}
echo "Wed";

etc...

?>
于 2012-12-16T10:52:15.407 回答