Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to call $row (database array) from within a function but calling the column name based on a variable.

Here is the full code... You can see the results at

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

and

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

?>

It only returns crosses, never ticks.

Where am I going wrong?

share|improve this question

closed as too localized by hakre, DaveRandom, tereško, PeeHaa, NikiC Dec 16 '12 at 13:06

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
can you var_dump $row[$check] inside your function to see what it is returning? –  Mr D Dec 16 '12 at 10:34
    
if($row[$check]===1) –  Ay34 Dec 16 '12 at 10:34
4  
Your script seems to be vulnerable to SQL injections! –  Gumbo Dec 16 '12 at 10:37
    
... and uses the deprecated mysql_* extensions. Switch to one of the newer APIs: php.net/manual/en/mysqlinfo.api.choosing.php –  ZombieHunter Dec 16 '12 at 10:46
    
del-ref: stackoverflow.com/questions/13900254/… –  hakre Dec 16 '12 at 10:46

3 Answers 3

up vote 4 down vote accepted

You're defining a function, inside the function there is no scope of $row.

Either define a new query there that will check if the user is available on a given day or remove the function and compare the field without the function.

So instead of doing checkAvailability('Mon'); do:

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

}

or give $row to your function. Like checkAvailability($row, 'Mon');

And remember to clean your $_GET['id'] to prevent SQL-injections.

share|improve this answer
    
Exactly! $row does not exist inside your checkAvailability() function –  kernelpanic Dec 16 '12 at 10:37
    
@Tim - Giving $row to the function didn't make any difference. And the real code is safe from SQL injections - this was just a stripped-down version of it. –  Stuart Pinfold Dec 16 '12 at 10:55
    
You have to change your function to function checkAvailability($row, $day). I think it's nicer to not use the function but do it directly in the code as you edited. –  Tim Dec 16 '12 at 12:26

You have mutliple problems in your code and it can greatly be improved.

Consider switching to anonymous functions:

$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";
}

They allow to use (import) variables from the scope they are defined in. It is used here for the $row variable. The functions can be used like any other function, for example as a callback for the output of all days:

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

I hope this is helpful. You should also drop mysql_* for many reasons, consider to switch to PDO or mysqli.

share|improve this answer

Thanks all. I've ended up with the (in my opinion) much more inefficient:

<?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...

?>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.