Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2012, 01:20 PM   PM User | #1
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
Issue with PHP conditional for displaying images

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?


PHP Code:
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 ($rowmysql_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 (
$rowmysql_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) == ) { 
echo 
'</tr><tr>'


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

galahad3 is offline   Reply With Quote
Old 01-11-2012, 05:08 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Old 01-12-2012, 03:18 PM   PM User | #3
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
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 is offline   Reply With Quote
Old 01-12-2012, 04:20 PM   PM User | #4
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
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!

Last edited by galahad3; 01-12-2012 at 07:12 PM..
galahad3 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:00 AM.


Advertisement
Log in to turn off these ads.