Issue with PHP conditional for displaying different images

@trufflepig

The idea is to simplify what your doing so as to find why your data is not showing the first row. A step towards this is simplifying your script. To this end, what I gave you removed everything except your main data query.

You can do :


echo '<table ...>';
while($row= mysql_fetch_array($result){
  echo '<tr>';
 //set variables from $row array
 [... variables here ...]
 echo '<td align="left" width="200">' . $realname. '<br>' . $detail . '</td>';   
  if ($availableuser == $username){    
    echo '<td><img src="images/' . $imageAvailable . '" width="100" height="150"></td>';   
  } else if (!$username == $availableuser){     
    echo '<td><img src="images/' . $imageUnavailable . '" width="100" height="150"></td>';   
  } 
echo '</tr>';
}
echo '</table>'; 

Once you put the variables into the above script then you should be able to see if all the data is being output to the screen. If it is then it is your other processing that is causing the messed up output.

The reason you got this all on one line as the original while was outputing a single row. So the revised sudo code above should output individual rows.

Can you try to work that out and let us know what you get please?

Steve

I tried this but it only outputs one single row with all the records in it…

Add this line just below the while() statement:


		$available = false;

See above I have edited the script in my last post.

Ok so outputing this:


include ('inc/dbconnect.php'); 
// Initialize variables;$type = null;
$date = null;$daynight = null;
if($_POST['checkavailability']) {      
  $type=@$_POST['_Type'];     
  $type = htmlentities($type);    
  $date=@$_POST['_DateSelected'];     
  $date = htmlentities($date);    
  $daynight=@$_POST['_Time'];     
  $daynight = htmlentities($daynight);

  // Build SQL Query  
  // specify the table and field names for the SQL query   
  $query = null;  
  $query = "        
    SELECT     
      RealName      
      , Detail               
      , ImageAvailable               
      , ImageUnavailable           
    FROM                
      users"; 
    $numresults = null;
    $numrows = null;
    $numresults=mysql_query($query); 
    $numrows=mysql_num_rows($numresults); 
    // get results $results = null;
    $result = mysql_query($query) or die("Couldn't execute query"); 
    echo '<table cellpadding="0" cellspacing="0" width="700" border="0">';  
   // Initialize variables;
   $userName = null;
   $realName = null;
   $detail = null;
   $imageAvailable = null;
   $imageUnavailable = null;
   $row = null;
   // display the results returned 
   while ($row= mysql_fetch_array($result)) {    
     echo '<tr>';   
     $userName =  html_entity_decode($row["UserName"]);     
     $realName = html_entity_decode($row["RealName"]);     
     $detail= html_entity_decode($row["Detail"]);     
     $imageAvailable = html_entity_decode($row["ImageAvailable"]);     
     $imageUnavailable = html_entity_decode($row["ImageUnavailable"]);      
     echo '<td align="left" width="200">' . $realname. '<br>' . $detail . '</td>';    
    if ($availableuser == $username){        
        echo '<td><img src="images/' . $imageAvailable . '" width="100" height="150"></td>';     
    } else if (!$username == $availableuser){         
        echo '<td><img src="images/' . $imageUnavailable . '" width="100" height="150"></td>';     
    }   
    echo '</tr>'; 
  } 
echo '</table>'; 
}  

Should output all the records each on their own line right?

Steve

Sorry for missing a line. See the following code along with your requirement of clickable username:


if($_POST['checkavailability']) 
{ 
	$type = mysql_real_escape_string($_POST['_Type']);
	$date = mysql_real_escape_string($_POST['_DateSelected']);
	$daynight = mysql_real_escape_string($_POST['_Time']);
	
	// Build SQL Query
	$query = "SELECT UserName, RealName, Detail, ImageAvailable, ImageUnavailable FROM users";
	$result = mysql_query($query) or die('Error on query: ' . mysql_error());
	
	echo '<table cellpadding="10" cellspacing="10" width="700" border="0"><tr>'; 
   
	// display the results returned
	$counter = 1;
	while ($row = mysql_fetch_array($result)){
		$_query = "SELECT UserName FROM dates WHERE DateAvailable='" . $date . "' AND DayNight='" . $daynight . "' AND UserName='" . $row["UserName"] . "'";
		$_result = mysql_query($_query);
		if($_result && mysql_num_rows($_result) >= 1){
			$click_link = 'http://yoursite.com/?member.php?username=' . $row["UserName"];
			echo '<td>
					<a href="' . $click_link . '">
						<img src="images/' . (($available) ? $row["ImageAvailable"] : $row["ImageUnavailable"]) . '" width="150" height="150">
					</a><br />
					<strong>' . $row["RealName"] . '</strong><br />' . $row["Detail"] . '
				</td>';
		}
		else{
			echo '<td>
					<img src="images/' . (($available) ? $row["ImageAvailable"] : $row["ImageUnavailable"]) . '" width="150" height="150"><br />
					<strong>' . $row["RealName"] . '</strong><br />' . $row["Detail"] . '
				</td>';
		}
		if($counter % 3 == 0) echo '</tr><tr>';
		$counter++;
	}
	echo '</tr></table>';
}

Edit:
Note that you should change the following line for the link:


$click_link = 'http://yoursite.com/?member.php?username=' . $row["UserName"];

Looks like @Raju Gautam has you well taken care of… don’t bother with my posts as I was trying to show you ideas about troubleshooting your own code rather than have someone else write it for you. It is important that you understand how to break down a problem to solve. Part of that is making sure along the way that you are getting all the data you need to parse and present. If so, then then does your filtering or reformatting this main data display properly or does it cause display issues.

Good work @Raju Gautam

Thanks, the formatting works but what happens now is that even if I select a date when I know two users are available, their “Available” image doesn’t show- instead the Unavailable image shows for all users?

Update: think I may have sorted that, by changing the $row[“ImageAvailable”] to $row[“ImageUnavailable”] for the first image output, so they do 2 different things depending on availability.

Looks good. Thanks Raju for sorting, and to Steve for the troubleshooting tips. :slight_smile: