Jump to content

[solved] Php Issues


Jamesking56

Recommended Posts

Hi!I got a big Problem with a Function I am coding.Here is my function:FUNCTION:

	function projects ( $location )	{		// Get Projects		$query = mysql_query("SELECT * FROM projects WHERE active = 1") or error('FATAL','Could not get projects!');		$count = mysql_num_rows($query);				if ( $count == 0 )		{			$project = 0;		}		else if ( $count == 1 )		{			$noarray = true;		}				// Check Location		if ( $location == '' || !$location )		{			return false;		}		else if ( $location == 'sidebar' )		{			// Sidebar Project Links						if(!$noarray)			{				while( $project = mysql_fetch_assoc($query) )				{					echo "<li><a href=\"" . $project['site'] . "\" title=\"Go To " . $project['name'] . "\" target=\"_blank\">" . $project['name'] . "</a></li>";				}			}			else if ( $project == 0 )			{				// No Projects				echo "<li>No Projects Yet!</li>";			}			else			{				echo "<li><a href=\"" . $query['site'] . "\" title=\"Go To " . $query['name'] . "\" target=\"_blank\">" . $query['name'] . "</a></li>";			}		}		else if ( $location == 'projects' )		{			// Projects Page		}		else		{			// Location Not Found			return false;		}	}

Now, When I use projects('sidebar'); on a page, it doesn't do it right!In my table, I have 1 Row, It outputs: "No Projects Yet!"In my table, I have 0 Rows, It outputs: "" (nothing)In my table, I have 2 Rows, It outputs: "Link1" and then "link2" (as it should)How can I fix it?

Link to comment
Share on other sites

When there is 1 table row its supposed to not use an while loop but it meant to display only that 1 rowWhen there are 2 or more, it uses the while loop to display them allWhen there is 0 rows, its meant to print "No Projects Yet..."Understand?

Link to comment
Share on other sites

I think regardless of whether you have one result or more, you need to export the results to an array.Try to remove the array checks, and leave only the "all or nothing" ones.

Link to comment
Share on other sites

You can also just walk through the code and see what happens. First you get the records and count them. If the count is 0, then you set $project to 0. You don't bother to initialize $project, so if $count is not 0 then $project will be undefined. If count is 1, then you set $noarray to true. You also don't bother to initialize $noarray, so if $count is not 1 then $noarray will be undefined. Then inside your if statement you're checking if $noarray is false. It's always going to evaluate to false unless $count is 1, so when $count is 0 $noarray is going to evaluate to false and it's going to execute the while loop to try and print the results out. So you're trying to print records when $count is 0. If $noarray is true, and $count is 1, then you're going into the next if statement and checking if $project is 0. That's always going to be true, you're never setting $project to anything other than 0. So when $count is 1, you're going into the second if statement to print no projects. So you're trying to print records when $count is 0, and when $count is 1 you're printing that there are no projects. It never even makes it to the last else where it prints a single row (if it did, you would get an error there because you're not printing the row correctly; you're trying to print $query which is a mysql result resource, not a row).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...