...

View Full Version : Issue with PHP conditional for displaying images



galahad3
01-11-2012, 01:20 PM
I'm having some issues with a PHP script I've created for displaying users from a database table.

In simple terms, the page loads the images and some basic details of all users (from the $query call to the database), but for each user it should also determine whether or not the user is "available" (a form on the previous page allows people to pick dates and "day or night", the idea being that while all users are shown, those who match that availability have a "I'm available" image display, whereas those whose availability dfoesn't match have the "I'm NOT available" image show. The $checkuser SQL query runs to determine which users are available.

My problem is that the "Available" image displays for users regardless of whether or not the user is available or not.

I echoed the $checkuser script and it's carrying through the variables ok, and no error message occurs.

It looks as if the if ($availableuser == $username) bit isn't working- or it's assigning $availableuser regardless of whether or not it matches the user.

Any ideas?




include ('inc/dbconnect.php');

if($_POST['checkavailability'])
{


$type=@$_POST['_Type'];
$date=@$_POST['_DateSelected'];
$daynight=@$_POST['_Time'];


// Build SQL Query
$query = "select RealName, Detail, ImageAvailable, ImageUnavailable from users"; // specify the table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// get results
$result = mysql_query($query) or die("Couldn't execute query");
echo '<table cellpadding="0" cellspacing="0" width="700" border="0"><tr>';

// display the results returned
while ($row= mysql_fetch_array($result)) {
$username= $row["UserName"];
$realname= $row["RealName"];
$detail= $row["Detail"];
$imageavailable= $row["ImageAvailable"];
$imageunavailable= $row["ImageUnavailable"];

$checkuser = "SELECT UserName FROM dates WHERE DateAvailable='$date' AND DayNight='$daynight'"; //Check to see if there are Locals

available
//echo $query;
//echo $checkuser;
$checkresult = mysql_query($checkuser) or die("Couldn't execute query");
while ($row= mysql_fetch_array($checkresult)) {
$availableuser= $row["UserName"];
$count++ ;
}

echo '<td align="left" width="200">' . $realname. '<br>' . $detail. '</td>';
if ($availableuser == $username)
{
echo '<td><img src="images/' . $imageavailable . '" width="100" height="150"></td>';
}

if (!$username == $availableuser)
{
echo '<td><img src="images/' . $imageunavailable . '" width="100" height="150"></td>';
}

if ( ($count % 3) == 0 ) {
echo '</tr><tr>';
}

$count++ ;
}
echo '</tr></table>';
}

sunfighter
01-11-2012, 05:08 PM
I can't give you line numbers, because I removed blank lines and reorganized the code. I shall start at the top and work down if that helps.

You build a query
$query = "select RealName, Detail, ImageAvailable, ImageUnavailable from users";
and then execute it twice.
$numresults=mysql_query($query);
and
$result = mysql_query($query) or die("Couldn't execute query");
The weird thing is the first one uses
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
which are not used anywhere else. So first thing I'd do is removed those last two lines.
In the WHILE loop you set $username= $row["UserName"]; but UserName is not in the query. $realname should be blank. and so I am surprised you are getting any results from this script.

Inside of that loop you do another query
$checkuser = "SELECT UserName FROM dates WHERE DateAvailable='$date' AND DayNight='$daynight'";
Bad, bad , bad to do a query in a loop.

They are related by name. RealName and UserName are the same RIGHT? Do a join and get all the info before doing the loop.

$count++ ; This line throws an error for me because $count was never declared or set to zero.

And your problem
if (!$username == $availableuser)
$username should be blank as I have said. Check that.
And I think you want to do the compare
if ($username != $availableuser)
and I really think a else statement would work better here.

galahad3
01-12-2012, 03:18 PM
Thanks for the reply. I'm not sure what code to change it for based on your comments, but I found the original problem was due to UserName not being specified in the $query.

However, I have another issue now- if a user is available on a particular day and time, and someone requests that day and time, then that user is displayed, with their "Available" image (which is great) but none of the other users (the ones who aren't available) are displayed. I want all users to be shown, whether or not they're available. The ones who are available, based on the criteria in the form, will have their "available" image show, and those who are not will have theior "unavailable" image show.

Although at the moment, if the user selects a date / time when no one is available, it DOES list all the users (although it only lists their images- it doesn't list their details EXCEPT for the first user in the table, which is bizarre)

Any ideas how this can be solved?

galahad3
01-12-2012, 04:20 PM
Think I may have fixed that by using if ($availableuser !== $username) instead of if (!$availableuser == $username) - seems to work!

BUT Only problem now seems to be the arrangement of the rows. As you can see I set some code for it to add a new row every three records that are output- but for some reason the first row of records (images etc.) has two entries (the third table cell is empty) and then all the following rows are fine and have three each. I can't see where it's getting an instruction to only output two on the first table row!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum