 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
Pippin
Joined: 27 Jan 2010 Posts: 7
|
Posted: Thu Jan 28, 2010 9:53 am html table at bottom of webpage (using php) |
|
|
|
Hello,
I am new to computer programming, yet I find myself creating a web database application using php and mysql. The program is working great. However, displaying my results in the format I want is my problem. I have the results being displayed in a html table, however the table insists on positioning itself at the bottom of my webpage. I cannot figure out why this is happening. I cannot find any <br> that would push the table down and the container that the table is going into is an appropriate size. Does anyone have an idea why this is happening? Below is my code. Keep in mind that the html is intertwined with the php (Sorry if this is a problem...It is the best I can do at the moment)
One final note. If I display the results of the search without using a table (just display as a string), the results display perfectly on the page. The moment I try to display using a table, the table ends up at the bottom of the page. Also, FYI the "writeToScreen" function is just an echo statement.
If you need to see more of the code just let me know. This is the section where I try to output the results.
| Code: |
writeToScreen("<h4><center> Search results for <i>". $find . "</i> in TSN, Common Name, and Scientific Name</center></h4>");
writeToScreen("<table width='100%' border='2' bordercolor='navy' cellpadding='5'><tr><td align=center><b>TSN</b></td><td align=center><b>Scientific Name</b></td><td align=center><b>Common Name(s)</b></td></tr>");
writeToScreen("<form method=\"post\" action=\"Result.php?go\" id=\"searchform\"/>");
if($search_topic == "All") {
$iter=0;
while($line=GET_LINE($dataFile)){
$data=explode("--",$line);
$parkName = $park[trim($data[5])];
if(FIND_STRING($line,$find,$search_span,$parkName,$metropark)>0) {
$bioTSN[$iter]=$data[0];
$accTSN[$iter]=$data[1];
$Sci[$iter]=$data[3];
$Com[$iter]=$data[4];
$iter+=1;
}
}
if($iter==0){
writeToScreen("No results found. Please modify search.");
exit;
}
$n=$iter;
$kNoDup=1;
$bioTSNNoDup[$kNoDup]=$bioTSN[1];
$accTSNNoDup[$kNoDup]=$accTSN[1];
$SciNoDup[$kNoDup]=$Sci[1];
$ComNoDup[$kNoDup]=$Com[1];
for ($k=1;$k<$n;++$k) {
//remove duplicates from search strings and only display once using scientific name.
$foundDuplicate=0;
for ($k2=1;$k2<$kNoDup;$k2++) {
if($Sci[$k]==$SciNoDup[$k2]) {
set_time_limit(90);
if($bioTSN[$k]==$bioTSNNoDup[$k2]) {
$foundDuplicate=1;
}
}
}
if($foundDuplicate==0){
$bioTSNNoDup[$kNoDup]=$bioTSN[$k];
$accTSNNoDup[$kNoDup]=$accTSN[$k];
$SciNoDup[$kNoDup]=$Sci[$k];
$ComNoDup[$kNoDup]=$Com[$k];
$kNoDup++;
}
}
$n=count($SciNoDup);
for($k=1;$k<=$n;++$k){
writeToScreen("<tr><td><input name='Result_Found' value='".$bioTSNNoDup[$k].",".$accTSNNoDup[$k]."' type='radio'>".$bioTSNNoDup[$k]."</td><td>".$SciNoDup[$k]."</td><td>".$ComNoDup[$k]."</td></tr>");
}
writeToScreen("</table>");
writeToScreen("<input value=\"Get Info\" name=\"go\"class=\"body\" title=\"Get Information\" type=\"submit\"/></form>");
}
|
Any ideas are greatly appreciated!
Cheers,
Pippin[/code] |
|
PayneLess Designs

Joined: 28 Feb 2007 Posts: 4883 Location: MS
|
Posted: Thu Jan 28, 2010 4:46 pm |
|
|
|
| Can't see anything strange in the HTML coding other than incorrect table coding which would not necessarily cause your problem. Since I can't run PHP on my computer, do you have a link to the actual generated HTML online so I can take a look there? |
|
kanenas

Joined: 14 Dec 2004 Posts: 352
|
Posted: Thu Jan 28, 2010 9:41 pm Re: html table at bottom of webpage (using php) |
|
|
|
One problem (probably the cause of the behavior you describe) is you're overlapping the tags for the table and the form. HTML doesn't work that way. An HTML document has a tree structure; an element must be entirely contained within its parent, which means you have to close elements in the opposite order they're opened. Also, the only valid children of <table> are <caption>, <col>, <colgroup>, <thead>, <tfoot> <tbody> and <tr>, more or less in that order. Any other element must be made a child of a <th> or <td> within the table. Solution: make the table a child of the form. Keep the <input> elements children of the table cells.
On an unrelated note, you can make use of the fact that PHP arrays can use strings for indices as well as integers to both simplify the code and improve performance.
Don't use exit or die in web scripts when you're outputting HTML. If you do, the result will be malformed HTML.
Consider:
| Code: |
writeToScreen('<form method="post" action="Result.php?go" id="searchform">')
if($search_topic == "All") {
while($line=GET_LINE($dataFile)){
$data=explode("--",$line);
$parkName = $park[trim($data[5])];
if(FIND_STRING($line,$find,$search_span,$parkName,$metropark)>0
&& ! isset($critters[data[2]]))
{
$critters["$data[2],$data[0],"] = array_combine(array('bio', 'acc', 'sci', 'comm'),
array_slice($data, 0, 4));
}
}
if(count($critters) < 1){
writeToScreen("No results found. Please modify search.");
} else {
writeToScreen('<table class="animals"><thead><tr><th></th><th>Scientific Name</th><th>Common Name(s)</th></tr></thead><tbody>');
$parity = 0;
foreach ($critters as $animal){
$id = "$animal[bio],$animal[acc]";
// advantage of using echo: no need to concat strings
echo "<tr class='", ($parity ? 'odd' : 'even'), "'><td><input name='Result_Found' id='$id' value='$animal[bio],$animal[acc]' type='radio' /><label for='$id'>$animal[bio]</label></td><td>$animal[sci]</td><td>$animal[comm]</td></tr>");
$parity = !$parity;
}
writeToScreen('</table>');
}
}
writeToScreen('<input value="Get Info" name="go" class="body" title="Get Information" type="submit"/></form>'); |
Don't use <center> or <b>, use CSS.
| Code: |
.animals {
width: 100%;
border-spacing: 1px;
border-color: navy;
}
.animals td {
padding: 0.3em;
border: 1px solid navy;
}
table thead th {
font-weight: bold;
text-align: center;
}
table tr.odd {
background-color: #DDD;
}
|
As for data storage, you should strongly consider using a real database if you are storing any real amount of data. Parsing all of a file with every request will result in terrible performance.
| Pippin wrote: |
| I cannot find any <br> that would push the table down |
This speaks of a misunderstanding about HTML. While break elements can affect layout, layout is a separate matter. In a well designed document, HTML defines the structure and style sheets define the layout, along with various built-in styling. Even in poorly designed documents, much of the layout is decided by the browser's built in style sheets.
| Pippin wrote: |
| Also, FYI the "writeToScreen" function is just an echo statement. |
Does it do any additional processing? If not, why use it? |
|
Pippin
Joined: 27 Jan 2010 Posts: 7
|
Posted: Sun Jan 31, 2010 6:20 pm |
|
|
|
First off sorry for the delayed reply. Kanenas, my brother has a PhD and I am pretty sure you are smarter than he is! I cannot thank you enough for your help. Since I am new at html/computer programming I am sure I have plenty still to learn!
As for my "writeToScreen" function being an echo statement.
| Quote: |
| Does it do any additional processing? If not, why use it? |
After you brought that point up I thought I should really look at my functions sheet more. When I was just getting started my brother was helping me get going. He wrote the "writeToScreen" function. When I questioned him as to why not just use an echo statement, his reply was "This is easier just do it". Well what I now realize is the "writeToScreen" function is an echo statement with an "<br>/n" tag attached to it. This lovely little bit of code was causing me all of sorts of problems. I could smack myself for not looking at the darn function he wrote...I just assumed he knew something I didnt. So thank you for pointing out that the "writeToScreen" function didn't make sense to you either!!
Thank you for your help and advice with the "exit and die" statements and the formating issues too. I am going through my code and adjusting it (using your suggestions!).
| Quote: |
As for data storage, you should strongly consider using a real database if you are storing any real amount of data.
|
Lastly, what exactly do you mean by using a real database? I am using mysql. To give you a quick background on the data. I am building a biological species database that allows users to search for any animal/plant/insect/etc. and find out if it has been identified in a particular park, when it was identified, and which park(s). Now because we scientists have to be a pain in the arse we have a tendency to rename a species multiple times. This means that person A may know an animal by one name, but person B may know it by a different name. As a result the database must contain all possible names for a given animal. However, to have the database search and combine all possible names every time a user runs a search would take forever. As a result I have a table that contains only the names and synonyms of the animals. This table is built whenever the database is updated. So when a user needs to search the database, the program will search the name table for all the possible names of the animal they are searching for. Once they select the animal they want more information on, the program can use the key associated with that name to pull up the rest of the information about that animal in the other mysql tables.
That is at least what I have attempted to do. The program is running pretty quickly. However, if you have any suggestions I am all ears! If you are interested in learning more or want to see pictures of my program please let me know. Thank you again! I appreciate your help.
Cheers, Pippin |
|
kanenas

Joined: 14 Dec 2004 Posts: 352
|
Posted: Wed Feb 03, 2010 7:01 pm DB design |
|
|
|
| Pippin wrote: |
| First off sorry for the delayed reply. |
No need for apologies. It's not my project, and I'm under no deadline.
| Pippin wrote: |
| my brother has a PhD and I am pretty sure you are smarter than he is! |
What field? As a mathie, I reserve the right to make fun of certain disciplines ;-)
It's really more a matter of knowledge than intelligence (though I'm not exactly lacking in that department). Even though your brother may be intelligent, if he hasn't studied computer science he won't be aware of the pitfalls and issues of different designs. Many scientists, for instance, learn enough programming to get by but produce ugly, hard to maintain code. Similarly, developers tend to know less about statistical analysis than they should, which gets them into trouble from time to time.
| Pippin wrote: |
| Lastly, what exactly do you mean by using a real database? I am using mysql. |
That's exactly what I meant. The code you posted had earmarks of flat-file data storage (the GET_LINE and FIND_STRING functions, the use of "explode"), so I thought you were using a flat file. Is the sample code what you're currently using, or merely an example created for the question? What's your current DB design (i.e. the table structure)? Much of the sample code could be replaced with a single SQL statement and the right DB design.
If in the real code you're using the old mysql driver (the mysql_* functions), switch to the more modern PDO driver. It supports prepared statements, which are more efficient for repeated queries in a single HTTP request and aren't vulnerable to SQL injection when you use parameters.
| Pippin wrote: |
| Now because we scientists have to be a pain in the arse we have a tendency to rename a species multiple times. |
Like M. gigas & T. rex, or F. tigris & P. tigris? You mean you don't get it perfect the first time like we programmers?
| Pippin wrote: |
| I am building a biological species database [...] However, if you have any suggestions I am all ears! |
Note that database design is a discipline unto itself, though not a terribly difficult one. What you describe sounds like the usual approach for a case like this: have a table of aliases. Usually, it would have two columns: a species ID (which could be the canonical species name or an arbitrary value) and a name (synonym, combination &c). I'd probably use an arbitrary value for the ID, which makes changing the canonical species name very easy, should you ever need to: just update the row in table `species`. The code for the design looks like this:
| Code: |
CREATE TABLE species (
id INT PRIMARY KEY,
name VARCHAR(256) NOT NULL, -- canonical species name
...
) ENGINE=InnoDB;
CREATE TABLE synonymy (
id INT NOT NULL,
alias VARCHAR(256) NOT NULL,
FOREIGN KEY (id) REFERENCES species (id) ON DELETE CASCADE,
UNIQUE (alias, id)
) ENGINE=InnoDB;
CREATE TABLE sightings (
species INT NOT NULL,
`where` ...,
`when` ...,
...
FOREIGN KEY (species) REFERENCES species (id) ON DELETE CASCADE
) ENGINE=InnoDB; |
Other advantages to using an arbitrary ID are that you'll never mistakenly enter a sighting using the wrong name and it can be more noticable when you leave out an alias for the canonical name.
Instead of an arbitrary ID, you could use the canonical name and have MySQL update the synonymy.name column whenever species.name is updated by using an ON UPDATE clause:
| Code: |
CREATE TABLE species (
name VARCHAR(256) PRIMARY KEY, -- canonical species name
...
) ENGINE=InnoDB;
CREATE TABLE synonymy (
name VARCHAR(256) NOT NULL, -- or however species.name is defined
alias VARCHAR(256) NOT NULL,
FOREIGN KEY (name) REFERENCES species (`name`) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE (alias, name)
) ENGINE=InnoDB;
... |
In either case, you'll need to ensure the canonical name appears as an alias, or use a slightly more complex query to fetch the species ID when given a species name.
If you're new to database design, read Databases from scratch for an introduction. If you're mathematically inclined, try Codd's seminal "A Relational Model of Data for Large Shared Data Banks". For a more complete covering of relational databases, try Maurer and Scherbakov's "Introduction to Databases and Relational Data Model". For other suggestions, check out the following questions on StackOverflow:
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|