Displaying section (heading, mysql data) only if table has data

I am working on a project that involves providing details for county government, and if applicable, cities independent of county oversight. Virginia is a state with a lot of independent cities, so I wanted to echo them in a separate list underneath the county list.

The code below still only displays a list of county names even if there are cities for that state.

Any thoughts or wisdom?

(sorry for pasting the whole bulky code)

[php] $query = “SELECT * FROM state WHERE st_id=$statecode”;
$result = mysql_query($query) or die("Query $query failed : " . mysql_error());
$row = mysql_fetch_assoc($result);
$state_name=$row[“st_name”];
$state_note=$row[“state_notes”];

		echo "<h2>Counties of " . $state_name; "</h2>\n";
		echo "<h4>Click on a county link below to view more information on Defendant Services in that area.</h4><hr>\n<p></p>\n";

		$query = "SELECT * FROM counties WHERE st_id=$statecode AND visible=1 ORDER BY county_name ASC";
		$result = mysql_query($query) or die("Query $query failed : " . mysql_error());
	
		echo "<table width=100%><tr><td valign=top width=33%>\n";

		$count = 1;
		$col = 0;
		$rowcount = (int)(mysql_num_rows($result) / 3) + 1;
		$remainder = mysql_num_rows($result) - ($rowcount - 1) * 3;
		/* ********************************************************* */
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo "<a href=\"defendant_services.php?county=".$row["county_id"]."\">".$row["county_name"] . "</style></a><br />\n";
			$count = $count + 1;
			if ((($remainder > 0) && ($count == ($rowcount + 1))) || (($remainder <= 0) && ($count == $rowcount))) {
				$col = $col + 1;
				if ($col!=3) {
					echo "</td><td valign=top width=33%>\n";
					$count = 1;
					if ($remainder > 0)
						$remainder = $remainder - 1;
				}
			}
		}
		/* ********************************************************* */
		echo "</td></tr></table><br>\n";
		mysql_free_result($result);
		/* ********************************************************* */
		
		/* vv Here is where the code will check for independent cities, and display a section for them if they exist vv */
		
		$query = "SELECT * FROM cities WHERE st_id=$statecode AND visible=1 ORDER BY city_name ASC";
        		$result= mysql_query($query) or die("Query $query failed : " . mysql_error());
       		$city_id = $row['city_id'];
       		$city_name = $row['city_name'];
       		
       		if ($city_id != "") {
       			echo "<h3>Cities of " . $state_name; "</h3>\n";
			echo "<h4>Click on a city link below to view more information on Defendant Services in that area.</h4><hr>\n<p></p>\n";
			echo "<table width=100%><tr><td valign=top width=33%>\n";
			
			$count = 1;
			$col = 0;	
			$rowcount = (int)(mysql_num_rows($result) / 3) + 1;
			$remainder = mysql_num_rows($result) - ($rowcount - 1) * 3;
			/* ********************************************************* */
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				echo "<a href=\"defendant_services.php?city=".$row["city_id"]."\">".$row["city_name"] . "</style></a><br />\n";
				$count = $count + 1;
				if ((($remainder > 0) && ($count == ($rowcount + 1))) || (($remainder <= 0) && ($count == $rowcount))) {
				$col = $col + 1;
				if ($col!=3) {
					echo "</td><td valign=top width=33%>\n";
					$count = 1;
					if ($remainder > 0)
						$remainder = $remainder - 1;
				}
			}
		}
		/* ********************************************************* */
		echo "</td></tr></table><br>\n";
		mysql_free_result($result);
		/* ********************************************************* */
			
       		}
       		else {
       			echo "";
       		}[/php]

Update your code to mysqli or PDO. Then, use prepared statements.

Those will solve your first huge issues. Then we can move on to how you should be handling the sql statements.

Sponsor our Newsletter | Privacy Policy | Terms of Service