Page is a not externally linkable
- WebmasterWorld
-- New To Web Development
---- Output Listing


rocknbil - 3:34 am on Dec 30, 2009 (gmt 0)


Referring back to my original post, I think there were only three errors there, and it looks like you discovered the first, is the "var" word before $allstates.

The second was flushing "$rowData" on each iteration. This is a "temporary place" to hold the query results so that if there is none, you can add "no data" to output. Likely what was happening is it was concatenating all the way through, adding each state's results to itself . . . not what you want.

The third is I complicated the process of printing the state. The "prints only once" really only applies if you are iterating through a states database table, where the state name will be present for every query result. This is really how you should do it, but since you've opted for an array, we can simplify it.

You have a lot going on here that's wrong. Here's the concept:

- Loop through a list of states. For each state,
-- add the state name to $output. No need to compare if you're using a static array.
-- Query DB for entries for this state, store it in $rowData only. this is so if there's nothing, you can output ($output, not ECHO, see below) the "no data" paragraph.
-- if there's data in $rowData, add it to the $output variable only, don't echo it . . .
- loop to next state, rinse and repeat.
- when done, echo $output only. Once.

Give you an example, and how your recent change has made it worse. :-) This point is irrelevant due to the array, but if you do it right using a states database table, you need to know this.

foreach ($allstates as $state){
if ($currState != $state) {
$output .= "<h3>$currState</h3>";

You've just put the OLD state in $output, so it's going to be wrong. If a new state comes in, add $state to the $output, not $currState.

Another, which is relevant . . . you have multiple echo's throughout, and I've no idea what this one will do:

echo $rowData .= " $row[0] <br />";

You're echoing a concatenation command. The echoes you have will only serve to confuse you further, sort of like tasting a half baked cake before it's done. Concatenate all to $output, then echo $output once. Many/most PHP coders echo on the fly; this often makes for a sick spaghetti of code and often, pages that half render while the server "thinks about it" (processes a query, for example.) This is why I do this.

<rant>The break tag is presentational, it's meaningless . . . and an XHTML style break without a doctype in plain old vanilla html . . . these should be lists, and you can style them as you want . . . but I digress.</rant>

Also, in this,

SELECT web, name FROM aaba WHERE state = '$state' ORDER BY name ASC

$row[0] is web, $row[1] is name. I would think name is what you want and web is a url, but . . . however it works, it works. :-)

Okay, if what you have in the last post is sorta working, try this. Warning, it may have errors . . . but at this late hour, I can't see any. :-)


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Anonymize, or the mods here will</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>State Listings</h1>
<?php
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$allstates = Array (
'ALABAMA',
'ALASKA',
'ARIZONA',
'ARKANSAS',
'blank',
// And so on, full state list.
//What is "blank?"
// Think about your select, it will select
// where $state = 'blank'
);
foreach ($allstates as $state) {
$rowData=NULL; // my error, this needs to FLUSH with every state
$output .= "<h3>$state</h3>"; // The if not needed in this scenario.
$query = "SELECT web, name FROM aaba WHERE state = '$state' ORDER BY name ASC";
$result=mysql_query($query);
if (!$result) { die("can't get data from tablename: " . mysql_error()); }
while ($row=mysql_fetch_array($result)) {
$rowData .= "<li>" . $row[0] . "</li>\n";
}
if ($rowData) { $output .= "<ul> $rowData </ul>\n"; }
else { $output .= "<p>No results in $state</p>\n"; }
}
mysql_free_result($result);
echo $output;
?>
</body>
</html>


Thread source:: http://www.webmasterworld.com/new_web_development/4050287.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com