Jump to content

Displaying only if table data meets criteria


myotch
Go to solution Solved by jcbones,

Recommended Posts

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 governments.

 

This code works fine when displaying Virginia information.  However, I would like to hide the Independent Cities heading in every other state.  For some reason, this code still echoes the Independent Cities heading in states without independant cities in the database.

 

Any thoughts or wisdom?

 

(sorry for pasting the whole bulky code)

$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";
            
            $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=\"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);
            /* ********************************************************* */
            
            $query2 = "SELECT * FROM cities WHERE st_id=$statecode AND visible=1 ORDER BY city_name ASC";
            $result2= mysql_query($query2) or die("Query $query failed : " . mysql_error());
            $city_id = $row['city_id'];
            $city_name = $row['city_name'];
            
            if ($city_id!="") {
                echo "<h3>Independent cities of " . $state_name; "</h3>\n";
                echo "<table width=100%><tr><td valign=top width=33%>\n";
                $count2 = 1;
                $col2 = 0;
                $rowcount2 = (int)(mysql_num_rows($result2) / 3) + 1;
                $remainder2 = mysql_num_rows($result2) - ($rowcount2 - 1) * 3;
                /* ********************************************************* */
                while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                    echo "<a href=\"services.php?county=".$row["city_id"]."\">".$row["city_name"] . "</style></a><br />\n";
                    $count2 = $count2 + 1;
                    if ((($remainder2 > 0) && ($count2 == ($rowcount2 + 1))) || (($remainder2 <= 0) && ($count2 == $rowcount2))) {
                        $col2 = $col2 + 1;
                            if ($col2!=3) {
                            echo "</td><td valign=top width=33%>\n";
                            $count2 = 1;
                            if ($remainder2 > 0)
                                $remainder2 = $remainder2 - 1;
                    }
                }
            
                
            }
            /* ********************************************************* */
            echo "</td></tr></table><br>\n";
            mysql_free_result($result2);
            /* ********************************************************* */
            
            
            
            }
            
        ?>
Edited by myotch
Link to comment
Share on other sites

Yeah, several:

 

1. Don't use "SELECT *" unless you are using all the fields in the table

 

2. Don't use the mysql_ functions. Use either mysqli_ or PDO. I didn't change it in the code below as there are some statements not included.

 

3. No need to define a variable, if you are only going to use it once

 

$state_name = $row["st_name"];

Just use $row["st_name"]

 

4. Don't echo the actual DB error to the page in a production environment. It gives malicious people information they can use to get into your system

 

5. The code to output the counties is waaaaaay over-complicated. I can't even tell what you are doing. I am going to assume you are displaying the records in three columns, the first third in the first column, the second third in the second, etc. rather than having them display left to right.

 

6. If you are going to run logic more than once, don't copy/paste the code. Create a function and call it multiple times. That way when you have a bug you only need to fix it in one place. Also, by doing this you are assured of consistent results.

 

This was a significant rewrite without any testing, so there may be a few errors to resolve

 

 

<?php
 
//Function to create output for counties/cities of a state
function createEntityOutput($stateName, $headerFormat, $entityData, $entityFormat, $columns)
{
    //If no records, return empty string
    if(!count($entityData)) { return ''; }
    
    //Create the output
    $output  = "<h2>" . sprintf($headerFormat, $stateName) . "</h2>\n";
    $output .= "<table width='100%'>\n";
    //Create needed variables
    $recTotal = count($entityData);
    $recPerCol = ceil($recTotal / $recCols);
    $colWidth = floor(100 / $columns);
    $recCount = 0;
    //Create entity output
    $output .= "<tr>\n";
    foreach($entityData as $data)
    {
        $countyCount++;
        //Open new column, if needed
        if($recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }
        //Create output for single entitiy
        $output .= sprintf($entityFormat, $data['id'], $data['name']);
        //Close column, if needed
        if($recCount%$recPerCol==0 || $recCount==$recTotal) { $output .= "</td>\n"; }
    }
    $output .= "</tr>\n";
    $output .= "</table>\n";
    return $output;
}
 
//Run query to get state data
$query = "SELECT st_name
          FROM state
          WHERE st_id=$statecode";
$result = mysql_query($query) or die("Query $query failed : " . mysql_error());
$state = mysql_fetch_assoc($result);
$stateName = $state['st_name'];
 
//Run query to get county data
$query = "SELECT county_id AS id, county_name AS name
          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());
//Create county output
$countyData = array();
while($countyData[] = mysql_fetch_assoc($result)) {}
$countyTable = createEntityOutput($stateName, "Counties of %s", $countyData, "<a href='services.php?county=%d'>%s</a><br />\n", 3);
 
//Run query to get state data
$query = "SELECT city_id AS id, city_name AS name
          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());
//Create city output
$cityData = array();
while($cityData[] = mysql_fetch_assoc($result)) {}
$cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);
 
?>
<?php echo $countyTable; ?>
<br>
<?php echo $cityTable; ?>
Link to comment
Share on other sites

Thanks, Psycho.

 

I tried your code as is, and it calls all the information flawlessly.  It doesn't put the results in a 3 column table, and in states where there are no cities, it still displays the heading for the cities section.

 

I'll use your code as a base and try to tweak it through.

 

Again, thanks - this is a big improvement over the antiquated code I'm working with.

Link to comment
Share on other sites

You shouldn't be getting any independent cities from the database, unless they exist in the state you are requesting.  This would point to a database problem.  Perhaps normalization is in order, or there is an error in the input of the cities table.

Link to comment
Share on other sites

The problem is that it is echoing the header for the independent cities section when the table cities doesn't have cities for the indexed state.

 

That's the main problem, for which the OP was posted.

 

Intention:

 

if (cities for the called state is not empty)

echo heading;

echo list of states in a three-column table;

else (if cities for the called state is populated)

don't echo anything;

 

 

The actual result for a state with no independent cities:

 

HEADING

(no list)

 

The actual result for a state with independent cities:

 

HEADING

(list, in a single column)

Link to comment
Share on other sites

  • Solution

Sorry, I read the code wrong. 

 

There are a couple of problems in the function, compare it to this one:

function-by Psycho

 

function createEntityOutput($stateName, $headerFormat, $entityData, $entityFormat, $columns)
{
    //If no records, return empty string
    if(!count($entityData)) { return ''; }
    
    //Create the output
    $output  = "<h2>" . sprintf($headerFormat, $stateName) . "</h2>\n";
    $output .= "<table width='100%'>\n";
    //Create needed variables
    $recTotal = count($entityData);
    $recPerCol = ceil($recTotal / $columns);
    $colWidth = floor(100 / $columns);
    $recCount = 0;
    //Create entity output
    $output .= "<tr>\n";
    foreach($entityData as $data)
    {
        //Open new column, if needed
        if(++$recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }
        //Create output for single entitiy
        $output .= sprintf($entityFormat, $data['id'], $data['name']);
        //Close column, if needed
        if($recCount%$recPerCol==0 || $recCount==$recTotal) { $output .= "</td>\n"; }
    }
    $output .= "</tr>\n";
    $output .= "</table>\n";
    return $output;
}

 

Then:

Replace

while($cityData[] = mysql_fetch_assoc($result)) {}
$cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);
 

With

$cityTable = NULL;if(mysql_num_rows($result) > 0) { while($cityData[] = mysql_fetch_assoc($result)) {}
 $cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);}
 
Link to comment
Share on other sites

@jcbones: maybe I am not seeing something in the code I provided, but I don't see how it is possible that the header would show when there are no records or how the changes you provided would make any difference. Here is what I see:

$result= mysql_query($query) or die("Query $query failed : " . mysql_error());
//Create city output
$cityData = array();

while($cityData[] = mysql_fetch_assoc($result)) {}

After the query is run, an empty array is defined to hold the records from the query. Then a while() loop is used to append those records to that array. Therefore, if there were no records - the array would still be empty.

$cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);

Next, the function to create the output is called passing the above array as one of the parameters. Again, either that array is empty (if there were no results from the query) or it contains the results from the query

function createEntityOutput($stateName, $headerFormat, $entityData, $entityFormat, $columns)
{
    //If no records, return empty string
    if(!count($entityData)) { return ''; }

Then the very first lines in the function check to see if that passed array is empty or not. If so, it returns an empty string. Otherwise, it continues with creating the output - including the header, which the OP states is being displayed.

 

Therefore, I have to assume the current problem is due to one of three things:

 

1. The OP modified the code I provided such that the header is always displayed (e.g. maybe it is hard coded later in the code rather than being dynamically created with the function.

 

2. The result of the query is not empty. Perhaps the "data" in the records returned are empty, but the query is returning results.

 

3. There is a gap in the code I provided that I am not seeing.

Edited by Psycho
Link to comment
Share on other sites

I copied and pasted the code as supplied here.  The only, and I mean only difference is creating variables for thestate_name and a field called state_note, for which I call in other parts of the page - sidebar, specifically.  (psycho, it really is superior code to what I had originally had).

 

jcbones's tweak made the heading disappear when there are no cities within the called state code.  In the database, there are currently, absolutely no references to any other indexed state other than Virginia, so testing on this is rather easy.

 

Thanks to you both!  I mean, a whole bunch!

 

Today is dedicated to fixing the column problem.  It's still showing up in a single and very long column for both county and city.

 

Cheers!

Link to comment
Share on other sites

I agree Psycho, but for some reason when I was cruising your function, I missed the whole "return empty string if there is no data" line.  Your function had that covered, it was my bad on calling that out when I shouldn't have.  There was some undefined variables, but I know those were spoilers to make the OP use the errors that he SHOULD have turned on.  I was just in a helping mood.

Myotch, I tested the returns from the function for the columns.  They were returned as they should be.  Perhaps you could give us an update on the exact code you are now working with.  I wonder also if you could give us a print_r of the $cityData variable, at the very bottom of the script.

 

 

echo '<pre>' . print_r($cityData,true) . '</pre>'; //place at the very bottom of your script, run the script, then copy paste the results:
Link to comment
Share on other sites

Sure!  I've started the process of udating  to PDO....I'll move it over to a sandbox and revert back to it's older self.

 

Here's the code (with the incomplete updates commented out):

//Function to create output for counties/cities of a state
function createEntityOutput($stateName, $headerFormat, $entityData, $entityFormat, $columns)
{
    //If no records, return empty string
    if(!count($entityData)) { return ''; }
    
    //Create the output
    $output  = "<h2>" . sprintf($headerFormat, $stateName) . "</h2>\n";
    $output .= "<table width='100%'>\n";
    //Create needed variables
    $recTotal = count($entityData);
    $recPerCol = ceil($recTotal / $recCols);
    $colWidth = floor(100 / $columns);
    $recCount = 0;
    //Create entity output
    $output .= "<tr>\n";
    foreach($entityData as $data)
    {
        $countyCount++;
        //Open new column, if needed
        if($recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }
        //Create output for single entitiy
        $output .= sprintf($entityFormat, $data['id'], $data['name']);
        //Close column, if needed
        if($recCount%$recPerCol==0 || $recCount==$recTotal) { $output .= "</td>\n"; }
    }
    $output .= "</tr>\n";
    $output .= "</table>\n";
    return $output;
}
 
//Run query to get state data

//$cmt = $dhb->prepare('SELECT * FROM state WHERE st_id=$statecode');
//$cmt->execute();
//$data = $cmt->fetchAll();
$query = "SELECT *
          FROM state
          WHERE st_id=$statecode";
   $result = mysql_query($query) or die("Query $query failed : " . mysql_error());
   $state = mysql_fetch_assoc($result);
$stateName = $state['st_name'];
$stateNote = $state['state_notes'];
 
//Run query to get county data
//$cqt = $dhb->prepare('SELECT county_id AS id, county_name AS name FROM counties WHERE st_id=$statecode and visible=1 ORDER BY county_name ASC');
//$cqt->execute();
//$data = $cq->fetchAll();
$query = "SELECT county_id AS id, county_name AS name
         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());
Create county output
$countyData = array();
while($countyData[] = mysql_fetch_assoc($result)) {}
$countyTable = createEntityOutput($stateName, "Counties of %s", $countyData, "<a href='services.php?county=%d'>%s</a><br />\n", 3);
 
//Run query to get state data
$query = "SELECT city_id AS id, city_name AS name
          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());
//Create city output
$cityData = array();
$cityTable = NULL;if(mysql_num_rows($result) > 0) { while($cityData[] = mysql_fetch_assoc($result)) {}
 $cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);}
Link to comment
Share on other sites

Oh crap!  Forgot to include this with the code above.  It's in there, just didn't cut and paste properly.

//Function to create output for counties/cities of a state
function createEntityOutput($stateName, $headerFormat, $entityData, $entityFormat, $columns)
{
    //If no records, return empty string
    if(!count($entityData)) { return ''; }
    
    //Create the output
    $output  = "<h2>" . sprintf($headerFormat, $stateName) . "</h2>\n";
    $output .= "<table width='100%'>\n";
    //Create needed variables
    $recTotal = count($entityData);
    $recPerCol = ceil($recTotal / $recCols);
    $colWidth = floor(100 / $columns);
    $recCount = 0;
    //Create entity output
    $output .= "<tr>\n";
    foreach($entityData as $data)
    {
        $countyCount++;
        //Open new column, if needed
        if($recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }
        //Create output for single entitiy
        $output .= sprintf($entityFormat, $data['id'], $data['name']);
        //Close column, if needed
        if($recCount%$recPerCol==0 || $recCount==$recTotal) { $output .= "</td>\n"; }
    }
    $output .= "</tr>\n";
    $output .= "</table>\n";
    return $output;
}
 
//Run query to get state data

//$cmt = $dhb->prepare('SELECT * FROM state WHERE st_id=$statecode');
//$cmt->execute();
//$data = $cmt->fetchAll();
$query = "SELECT *
          FROM state
          WHERE st_id=$statecode";
   $result = mysql_query($query) or die("Query $query failed : " . mysql_error());
   $state = mysql_fetch_assoc($result);
$stateName = $state['st_name'];
$stateNote = $state['state_notes'];
 
//Run query to get county data
//$cqt = $dhb->prepare('SELECT county_id AS id, county_name AS name FROM counties WHERE st_id=$statecode and visible=1 ORDER BY county_name ASC');
//$cqt->execute();
//$data = $cq->fetchAll();
$query = "SELECT county_id AS id, county_name AS name
         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());
Create county output
$countyData = array();
while($countyData[] = mysql_fetch_assoc($result)) {}
$countyTable = createEntityOutput($stateName, "Counties of %s", $countyData, "<a href='services.php?county=%d'>%s</a><br />\n", 3);
 
//Run query to get state data
$query = "SELECT city_id AS id, city_name AS name
          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());
//Create city output
$cityData = array();
$cityTable = NULL;if(mysql_num_rows($result) > 0) { while($cityData[] = mysql_fetch_assoc($result)) {}
 $cityTable = createEntityOutput($stateName, "Independent cities of %s", $cityData, "<a href='services.php?city=%d'>%s</a><br />\n", 3);}
 
?>
<?php echo $countyTable; ?>
<br>
<?php 

		echo $cityTable;
	 ?>
</div>
<div class="span4">
<h3>Defendant Services in <?php echo $stateName; ?></h3>
<?php echo $stateNote; ?>
Edited by myotch
Link to comment
Share on other sites

Here's how the old code from the OP renders:

10615623_10152664736103550_3715589737328

 

Here's how the new code renders (note: single column):

 

10636020_10152664737028550_3065099970601

 

And the New Code, for the state of Virginia (which does not render in the old code):

10670130_10152664737508550_7468423321688

 

Try for yourself live at www.defbail.com and www.defbail.com/sandbox

 

Domain is the old code, sandbox is the new code.  As soon as the wrinkle is ironed, I'll delete the non-sandbox files.

 

Here's the print-r:

Array(    [0] => Array        (            [id] => 1            [name] => Alexandria        )    [1] => Array        (            [id] => 2            [name] => Bristol        )    [2] => Array        (            [id] => 3            [name] => Buena Vista        )    [3] => Array        (            [id] => 4            [name] => Charlottesville        )    [4] => Array        (            [id] => 5            [name] => Chesapeake        )    [5] => Array        (            [id] => 6            [name] => Colonial Heights        )    [6] => Array        (            [id] => 7            [name] => Covington        )    [7] => Array        (            [id] => 8            [name] => Danville        )    [8] => Array        (            [id] => 9            [name] => Emporia        )    [9] => Array        (            [id] => 26            [name] => etersburg        )    [10] => Array        (            [id] => 10            [name] => Fairfax        )    [11] => Array        (            [id] => 11            [name] => Falls Church        )    [12] => Array        (            [id] => 12            [name] => Franklin        )    [13] => Array        (            [id] => 13            [name] => Fredericksburg        )    [14] => Array        (            [id] => 14            [name] => Galax        )    [15] => Array        (            [id] => 15            [name] => Hampton        )    [16] => Array        (            [id] => 16            [name] => Harrisonburg        )    [17] => Array        (            [id] => 17            [name] => Hopewell        )    [18] => Array        (            [id] => 18            [name] => Lexington        )    [19] => Array        (            [id] => 19            [name] => Lynchburg        )    [20] => Array        (            [id] => 20            [name] => Manassas        )    [21] => Array        (            [id] => 21            [name] => Manassas Park        )    [22] => Array        (            [id] => 22            [name] => Martinsville        )    [23] => Array        (            [id] => 23            [name] => Newport News        )    [24] => Array        (            [id] => 24            [name] => Norfolk        )    [25] => Array        (            [id] => 25            [name] => Norton        )    [26] => Array        (            [id] => 27            [name] => Poquoson        )    [27] => Array        (            [id] => 28            [name] => Portsmouth        )    [28] => Array        (            [id] => 29            [name] => Radford        )    [29] => Array        (            [id] => 30            [name] => Richmond        )    [30] => Array        (            [id] => 31            [name] => Roanoke        )    [31] => Array        (            [id] => 32            [name] => Salem        )    [32] => Array        (            [id] => 33            [name] => Staunton        )    [33] => Array        (            [id] => 34            [name] => Suffolk        )    [34] => Array        (            [id] => 35            [name] => Virginia Beach        )    [35] => Array        (            [id] => 36            [name] => Waynesboro        )    [36] => Array        (            [id] => 37            [name] => Williamsburg        )    [37] => Array        (            [id] => 38            [name] => Winchester        )    [38] => )
Edited by myotch
Link to comment
Share on other sites

Even using your supplied array, the function returns the correct 3 columns. 

 

Source

 

	
		
		
		
			
				<h2>Independent cities of Virginia</h2>
		
		
			
				 
			
				<table width='100%'>
		
		
			
				 
			
				<tr>
		
		
			
				 
			
				<td valign='top' width='33%'>
		
		
			
				 
			
				<a href='services.php?city=1'>Alexandria</a><br />
		
		
			
				 
			
				<a href='services.php?city=2'>Bristol</a><br />
		
		
			
				 
			
				<a href='services.php?city=3'>Buena Vista</a><br />
		
		
			
				 
			
				<a href='services.php?city=4'>Charlottesville</a><br />
		
		
			
				 
			
				<a href='services.php?city=5'>Chesapeake</a><br />
		
		
			
				 
			
				<a href='services.php?city=6'>Colonial Heights</a><br />
		
		
			
				 
			
				<a href='services.php?city=7'>Covington</a><br />
		
		
			
				 
			
				<a href='services.php?city=8'>Danville</a><br />
		
		
			
				 
			
				<a href='services.php?city=9'>Emporia</a><br />
		
		
			
				 
			
				<a href='services.php?city=26'>etersburg</a><br />
		
		
			
				 
			
				<a href='services.php?city=10'>Fairfax</a><br />
		
		
			
				 
			
				<a href='services.php?city=11'>Falls Church</a><br />
		
		
			
				 
			
				<a href='services.php?city=12'>Franklin</a><br />
		
		
			
				 
			
				</td>
		
		
			
				 
			
				<td valign='top' width='33%'>
		
		
			
				 
			
				<a href='services.php?city=13'>Fredericksburg</a><br />
		
		
			
				 
			
				<a href='services.php?city=14'>Galax</a><br />
		
		
			
				 
			
				<a href='services.php?city=15'>Hampton</a><br />
		
		
			
				 
			
				<a href='services.php?city=16'>Harrisonburg</a><br />
		
		
			
				 
			
				<a href='services.php?city=17'>Hopewell</a><br />
		
		
			
				 
			
				<a href='services.php?city=18'>Lexington</a><br />
		
		
			
				 
			
				<a href='services.php?city=19'>Lynchburg</a><br />
		
		
			
				 
			
				<a href='services.php?city=20'>Manassas</a><br />
		
		
			
				 
			
				<a href='services.php?city=21'>Manassas Park</a><br />
		
		
			
				 
			
				<a href='services.php?city=22'>Martinsville</a><br />
		
		
			
				 
			
				<a href='services.php?city=23'>Newport News</a><br />
		
		
			
				 
			
				<a href='services.php?city=24'>Norfolk</a><br />
		
		
			
				 
			
				<a href='services.php?city=25'>Norton</a><br />
		
		
			
				 
			
				</td>
		
		
			
				 
			
				<td valign='top' width='33%'>
		
		
			
				 
			
				<a href='services.php?city=27'>Poquoson</a><br />
		
		
			
				 
			
				<a href='services.php?city=28'>Portsmouth</a><br />
		
		
			
				 
			
				<a href='services.php?city=29'>Radford</a><br />
		
		
			
				 
			
				<a href='services.php?city=30'>Richmond</a><br />
		
		
			
				 
			
				<a href='services.php?city=31'>Roanoke</a><br />
		
		
			
				 
			
				<a href='services.php?city=32'>Salem</a><br />
		
		
			
				 
			
				<a href='services.php?city=33'>Staunton</a><br />
		
		
			
				 
			
				<a href='services.php?city=34'>Suffolk</a><br />
		
		
			
				 
			
				<a href='services.php?city=35'>Virginia Beach</a><br />
		
		
			
				 
			
				<a href='services.php?city=36'>Waynesboro</a><br />
		
		
			
				 
			
				<a href='services.php?city=37'>Williamsburg</a><br />
		
		
			
				 
			
				<a href='services.php?city=38'>Winchester</a><br />
		
		
			
				 
			
				<a href='services.php?city=0'></a><br />
		
		
			
				 
			
				</td>
		
		
			
				 
			
				</tr>
		
		
			
				 
			
				</table>
		
		
			
				

 

Compare this to your source.

Link to comment
Share on other sites

OK, narrowing it down....

 

The sandbox page's problem area is using this source:

<div class="row">
<div class="span5">
<h2>Counties of Virginia</h2>
<table width=100%>
<tr>
<a href='defendant_services.php?county=3214'>Accomack</a><br />
</td>
<a href='defendant_services.php?county=3215'>Albemarle</a><br />
</td>
<a href='defendant_services.php?county=3216'>Alleghany</a><br />
</td>...

And the original code is diplaying this source code:

<div class="row">
<div class="span5">
<h2>Counties of Virginia<h4>Click on a county link below to view more information on Defendant Services in that area.</h4><hr>
<p></p>
<table width=100%><tr><td valign=top width=33%>
<a href="defendant_services.php?county=3214">Accomack</style></a><br />
<a href="defendant_services.php?county=3215">Albemarle</style></a><br />
<a href="defendant_services.php?county=3216">Alleghany</style></a><br />

So it seems the problem would be with this line of PHP not being called:

 $output .= "<tr>\n";

nor this one:

if($recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }

since neither are being called.

Edited by myotch
Link to comment
Share on other sites

 

$recCount = 0;
//Create entity output
$output .= "<tr>\n";
foreach($entityData as $data)
{
    $countyCount++;
    //Open new column, if needed
    if($recCount%$recPerCol==1) { $output .= "<td valign='top' width='{$colWidth}%'>\n"; }

 

The code is incrementing the variable $countyCount and that condition is expecting $recCount. Change $countyCount++; to $recCount++;

  • Like 1
Link to comment
Share on other sites

Didn't work.

 

Well, that was helpful. If you aren't even going to put forth the effort, I don't know why I bother, When I am feeling generous I do test code before posting. But, in this situation it requires a specific DB setup and I am not going to go to that trouble just to test some code to work out some minor errors. I did all of that on-the-fly and I stated as much.

 

Did you look at the HTML source? WHat has changed?

Link to comment
Share on other sites

My heartfelt apologies. 

 

There was no change in the source.  I do recognize the considerable work you and jcbones put into this, and I am grateful.

 

Do you think it would be  better to get this code right and then use the correct php code as the reference to convert to pdo? 

 

Maybe because of my frustration, my thinking at this point is, if I need to change to PDO anyway, maybe I need to focus on that. I've got a PDO testing file now and saved my working index using pdo connection.  There's tutorials about using pdo to get a 3 column html table in echoing db data.

Link to comment
Share on other sites

I tested the updated function that was posted in post #6 above, it renders the counties/cities in 3 separate columns as you desire.  I do notice that you are not using the function posted in post #6.  Perhaps, you could look at that post again carefully, and compare it to what you have.

Link to comment
Share on other sites

Do you think it would be  better to get this code right and then use the correct php code as the reference to convert to pdo? 

 

Maybe because of my frustration, my thinking at this point is, if I need to change to PDO anyway, maybe I need to focus on that. I've got a PDO testing file now and saved my working index using pdo connection.  There's tutorials about using pdo to get a 3 column html table in echoing db data.

 

No. There should be zero dependency on how you GET the data and how you DISPLAY the data. That is why I built the display function to take an array as a parameter instead of passing the DB Result object. If you pass the object, then the display functionality would be dependent on how you are storing and getting the data. I.e. if you change to PDO then the process of using that data in the function would have to change. You want to separate logic to reduce dependencies. You should reduce the process of getting the DB results and putting into array into a single function/method. Then when you change the manner in which you store or retrieve data you only need to modify that function/method.

Link to comment
Share on other sites

Well, marked this "solved"!  #6 was the key.

 

It was very much user error, and the fact that I still don't know what I did wrong probably speaks volumes.  But a cut&paste into a different HTML editor worked perfectly and the three columns displayed as wanted. 

 

I really am sorry for being a pain in the butt.  This little site is my current project, but it means a lot more.  Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.