Page 1 of 1

Empty Cell PHP/MySQL

Posted: Tue Jun 29, 2010 4:54 pm
by mirnesc92
I got this...

Code: Select all

$result_topics_last = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forum."' AND type = '0' AND topic_id = '".$forums_topics['id']."' 
											ORDER BY position DESC LIMIT 0,1");
		while($topics_last = mysql_fetch_array($result_topics_last)){
			
			$html .= "<img src='images/lastpost.gif'> ".$topics_last['date_wor']. " ".$topics_last['time'];
			$html .= "<br />";

			$result_users_last = mysql_query("SELECT id, first, last FROM users WHERE id = '".$topics_last['poster_id']."'");
			while($forums_users_last = mysql_fetch_array($result_users_last)){
				$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
			}
		}
The outcome is this...
Image

However, when the cell is empty it gives something like this...
Image

It is an empty block. I tried fixing using all of these if statement...

Code: Select all

if($forums_users_last==""){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if($forums_users_last=="NULL"){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if($forums_users_last==NULL){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if($forums_users_last=="0"){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if($forums_users_last=="false"){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if(!isset($forums_users_last)){
	$html .= "Last post by <a href='#'>---</a>";
}else{
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}
if($forums_users_last){
	$html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}elseif(!$forums_users_last){
	$html .= "Last post by <a href='#'>---</a>";
}
None of these work, so here I am. What do I use in the if statement? Im out of ideas. >.>

Re: Empty Cell PHP/MySQL

Posted: Tue Jun 29, 2010 7:28 pm
by Jade
Have you checked to make sure the query is returning a result set?

Re: Empty Cell PHP/MySQL

Posted: Tue Jun 29, 2010 11:19 pm
by mirnesc92
Jade wrote:Have you checked to make sure the query is returning a result set?
What do you mean? It returns a result when there is one; however, when there is no match I want it to do something else. That is where the if statement would come in, but I don not know what is being returned.

Re: Empty Cell PHP/MySQL

Posted: Wed Jun 30, 2010 7:20 am
by Jade
Ohh okay. I understand what you're saying now. Try this:

Code: Select all

if(!$forums_users_last['id']){
        $html .= "Last post by <a href='#'>---</a>";
}else{
        $html .= "Last post by <a href='?action=profile&upid=".$forums_users_last['id']."'>".$forums_users_last['first']."</a>";
}

Re: Empty Cell PHP/MySQL

Posted: Wed Jun 30, 2010 1:39 pm
by mirnesc92
Didn't work, still returning either full string or nothing.

Re: Empty Cell PHP/MySQL

Posted: Wed Jun 30, 2010 8:06 pm
by mirnesc92
Any other ideas? This is starting to hold back my entire project, because I have the same problem on a number of thing I am working on.

Re: Empty Cell PHP/MySQL

Posted: Thu Jul 01, 2010 4:28 am
by aneesme
if(mysql_num_rows($result_users_last) > 0) {
//Put your while code here
}

Regards,
Anees

Re: Empty Cell PHP/MySQL

Posted: Thu Jul 01, 2010 6:01 pm
by mirnesc92
Didn't work either =\

Re: Empty Cell PHP/MySQL

Posted: Fri Jul 02, 2010 8:45 am
by Jade
I think you have a problem with your query ie you're always getting last post data. Can you run this code and then copy/paste the results?

Code: Select all

$result_topics_last = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forum."' AND type = '0' AND topic_id = '".$forums_topics['id']."' ORDER BY position DESC LIMIT 0,1");

$topics_last = mysql_fetch_array($result_topics_last) or die (mysql_error());

print "Listing all topics: ";
print_r($topics_last); //print out the results of your query

while($topics_last = mysql_fetch_array($result_topics_last))
{                       
     $result_users_last = mysql_query("SELECT id, first, last FROM users WHERE id = '".$topics_last['poster_id']."'");
     $forums_users_last = mysql_fetch_array($result_users_last) or die (mysql_error());

     print "<blockquote>Data for " . $topics_last['poster_id'] . ": ";
     print_r($forums_users_last);
     print "</blockquote>";
}


Re: Empty Cell PHP/MySQL

Posted: Fri Jul 02, 2010 2:28 pm
by mirnesc92
It's a bit messy I am still working on it.
http://i45.tinypic.com/25is0mw.png

the code you gave me returned nothing

Here is the whole function.

Code: Select all

function forumIndex($log, $admin){
	
		global $dbHost, $dbUser, $dbPass, $dbDatabase, $ckName;
		
		// STORAGE ---------------------------------------------------------
		// all the "useless" variables are stored here
		$row		= "b";	// used for determing CSS class
		$golfer		= 1;	// used for seperating commas in moderator list
		// end the of storage
		// STORAGE ---------------------------------------------------------
		
		// create body
		// this is the content box in the "middle" div#
		$html  = "<div id='cont_img'><h2>Forum</h2></div>";
		$html .= "<div id='content'>";
		$html .= "<h2>Project Forum</h2>";
		$html .= "<h5><a href='?action=forum&view=index'>Forum</a></h5>";
		
		// MySQL connection for the category table
		// this will display all the categories
		$connection = mysql_connect($dbHost, $dbUser, $dbPass);
		if(!$connection){die(mysql_error());}
		mysql_select_db($dbDatabase, $connection); 
		$result_category = mysql_query("SELECT * FROM category ORDER BY position ASC");
		while($category = mysql_fetch_array($result_category)){
	
			$html .= "<table width='100%'>";
			$html .= "<tr class='row-".$row."'>";
			$html .= "<th class='hometable' colspan='2'>".$category['name']."</th>";
			$html .= "<th class='hometable' width='5%'>topics</th>";
			$html .= "<th class='hometable' width='5%'>replies</th>";
			$html .= "<th class='hometable' width='17%'>last post info</th>";
			$html .= "</tr>";
			
			// FORUMS
			// this will feed us all the information we need from the forums table
			$result_forums = mysql_query("SELECT * FROM forums WHERE category_id = '".$category['id']."' ORDER BY position ASC");
			while($forums = mysql_fetch_array($result_forums)){
				if($row=="a"){$row="b";}
				elseif($row=="b"){$row="a";}
				$html .= "<tr class='row-".$row."'>";
				$html .= "<td width='6%' align='center'><img src='images/forum.png'></td>";
				$html .= "<td width='67%'>";
				$html .= "<p><a href='?action=forum&view=topic&forum_id=".$forums['id']."'>".$forums['forum_name']."</a>";
				$html .= "<br>".$forums['forum_description'];
				$html .= "<br><em>Moderators: </em>";
				
				// split member ids
				// stored in a list (ex. "1,2,4,7) we need to split them into array
				$split_ids = explode(",", str_replace(" ", "", $forums['moderator_ids']));
				// we get a count of number of items in array
				$split_ids_count = count($split_ids);
				
				// USERS
				// this will feed us all the information we need from the forums table
				for($i=0;$i<count($split_ids);$i++){
					$result_users = mysql_query("SELECT * FROM users WHERE id = ".$split_ids[$i]." ORDER BY id DESC");
					while($users = mysql_fetch_array($result_users)){
						if($golfer<$split_ids_count){
							$html .= "<a href='?action=profile&upid=".$split_ids[$i]."'>".$users['first']."</a> , ";
							$golfer = $golfer + 1;
						}else{
							$html .= "<a href='?action=profile&upid=".$split_ids[$i]."'>".$users['first']."</a>";
							$golfer = 1;
							}	
					}
				}
				$html .= "</p>";
				$html .= "</td>";
				
				// TOPICS COUNT
				// this will return number of topics using mysql_num_rows
				$result_topics_count = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forums['id']."' AND type = '1' ORDER BY position DESC");
				$html .= "<td align='center'>".mysql_num_rows($result_topics_count)."</td>";
				
				// POSTS COUNT
				// this will return number of topics using mysql_num_rows
				$result_posts_count = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forums['id']."' AND type = '0' ORDER BY position DESC");
				$html .= "<td align='center'>".mysql_num_rows($result_posts_count)."</td>";
				
				$html .= "<td><p>";
				
				// TOPICS USER
				// this will return the last person who posted by order of 'position'
				$result_topics_date = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forums['id']."' ORDER BY position DESC LIMIT 0,1");
				while($topics_date = mysql_fetch_array($result_topics_date)){
					$html .= "<img src='images/lastpost.gif'> ".$topics_date['date_wor']." ".$topics_date['time'];
				}
				// TOPICS TITLE
				// this will return last active topic
				$result_topics_title = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forums['id']."' AND type = '1' ORDER BY position DESC LIMIT 0,1");
				while($topics_title = mysql_fetch_array($result_topics_title)){
					$html .= "<br>In: <a href='#'>".$topics_title['title']."</a>";		
				}
				// TOPICS USER
				// this will return the last person who posted by order of 'position'
				$result_topics_title = mysql_query("SELECT * FROM topics WHERE forum_id = '".$forums['id']."' ORDER BY position DESC LIMIT 0,1");
				while($topics_title = mysql_fetch_array($result_topics_title)){
					$result_users = mysql_query("SELECT * FROM users WHERE id = ".$topics_title['poster_id']." ORDER BY id DESC");
					while($topics_user = mysql_fetch_array($result_users)){
						$html .= "<br>By: <a href='?action=profile&upid=".$topics_user['id']."'>".$topics_user['first']."</a>";	
					}
				}
				$html .= "</p></td>";
				$html .= "</tr>";
			}
			$html .= "</table>";
		}
	$html .= "</div>";
	mysql_close($connection);
	return($html);
}
This content in the middle is the output result.

http://i47.tinypic.com/2saxj4y.png

As you can see I am trying to make it say By --- In --- Time --- instead of having that blank cell there.

Re: Empty Cell PHP/MySQL

Posted: Sat Jul 03, 2010 12:42 pm
by mirnesc92
bump