I'm trying to create a page using php. It should look like:
Except under nation there should be a country name which is a link to a page about the country. All my server side stuff seems to be correct. But I have no idea what the code to do this should look like. I'm really confused.
Thanks
The first significant thing living here taught me is conformity costs money, and everybody pays.
Nite-Man on
0
Posts
admanbunionize your workplaceSeattle, WARegistered Userregular
edited October 2009
We can't do your homework for you, so you're going to have to ask more specific questions. What are you having trouble with?
I'm having trouble with the code for displaying a record from a database as a hypertext link. The part of the table where the country names should go.
It's difficult because I haven't learned much code in this online class. The assignments consist of making slight adjustments to code that the instructor provides.
Nite-Man on
The first significant thing living here taught me is conformity costs money, and everybody pays.
0
admanbunionize your workplaceSeattle, WARegistered Userregular
edited October 2009
Well, what values are stored in the database's 'nation' field?
I'm having trouble with the code for displaying a record from a database as a hypertext link. The part of the table where the country names should go.
It's difficult because I haven't learned much code in this online class. The assignments consist of making slight adjustments to code that the instructor provides.
Ok, a couple things:
1) What is the field name for 'nation'? It looks like you're missing a <td> and a $row.
2) In PHP, double quoted strings are parsed for variables. If you don't have a variable in a double-quoted string, you're making PHP do some extra work for absolutely no reason. Instead, use single-quoted strings. Two alternatives:
echo '<td>' . $row['area'] . '</td>';
(note the single quotes)
echo "<td>$row[area]</td>";
(note the double quotes as well as the removed single quotes around 'area'. In a double quoted string, you do not need to include the single quotes around an array's key)
My preference is to use option #2 as it's easier to read. I would also suggest you have one big long string to echo rather than half a dozen individual echo statements.
2) In PHP, double quoted strings are parsed for variables. If you don't have a variable in a double-quoted string, you're making PHP do some extra work for absolutely no reason. Instead, use single-quoted strings. Two alternatives:
echo '<td>' . $row['area'] . '</td>';
(note the single quotes)
echo "<td>$row[area]</td>";
(note the double quotes as well as the removed single quotes around 'area'. In a double quoted string, you do not need to include the single quotes around an array's key)
My preference is to use option #2 as it's easier to read. I would also suggest you have one big long string to echo rather than half a dozen individual echo statements.
Hope this helps.
Actually, this hasn't been true for years. Double-quotes and single quotes are parsed at basically the same speed. Check out The PHP Benchmark for evidence, and more neat benchmarks about what you should and shouldn't do.
But on the subject of optimisation, here's the #1 best piece of advice you're ever going to get:
DON'T.
Seriously, premature optimization is the root of all evil, or if not all evil, at least the vast majority of programming mistakes I've run into. If you're doing tricky things at the expense of the clarity of your code, you're going to run into problems later when you are trying to fix a bug and you don't quite understand what you did three months ago in the name of saving another thousandth of a second.
Also, consider that until the program is written, you don't know what parts need to be optimized. You could spend a day cutting the run time of record loading in half, only to learn that record loading normally takes a tenth of a second and record DISPLAYING is taking two seconds.
Optimization is best left until very, very, last and only until AFTER you've been solidly convinced that you NEED it (people are complaining) and you KNOW where your program is actually slow.
Imperfect on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
Don't do this - using shorttags is bad, as not all servers will have them enabled and then your scripts will break on the ones that don't have them enabled.
Seguer on
0
admanbunionize your workplaceSeattle, WARegistered Userregular
edited November 2009
I like how the OP has disappeared without explaining his problem, yet half a dozen people are solving problems that don't matter.
I have. It's particularly an issue if you're dealing with academic clients that often use their own hosting platforms.
The best part is that you can't write XML documents with shorttags on, since PHP thinks the XML declaration is a short tag. :P
Always fun to have to start a page with <?php echo("<?xml ...") ?>. I had to make a quick and ugly script that replaced the XML declaration in files with PHP echoing the declaration instead before they got uploaded to the server that I had no control over.
I serve XML documents with PHP shorttags enabled. You just do this:
<<?='?'?>xml version="1.0"<?='?'?>>
Sure it's ugly, but you only have to do it in headers, and so long as you and the compiler understand it, so what?
Alternatively, there's <% and %>, which I believe work in PHP just fine. Not so sure if <%=$stuff%> works, but it looks just weird to me anyway, so I don't bother with 'em.
Posts
Here's part of my code for the table:
<th>*</th><th>*</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>
I'm having trouble with the code for displaying a record from a database as a hypertext link. The part of the table where the country names should go.
It's difficult because I haven't learned much code in this online class. The assignments consist of making slight adjustments to code that the instructor provides.
Ok, a couple things:
1) What is the field name for 'nation'? It looks like you're missing a <td> and a $row.
2) In PHP, double quoted strings are parsed for variables. If you don't have a variable in a double-quoted string, you're making PHP do some extra work for absolutely no reason. Instead, use single-quoted strings. Two alternatives:
(note the single quotes)
(note the double quotes as well as the removed single quotes around 'area'. In a double quoted string, you do not need to include the single quotes around an array's key)
My preference is to use option #2 as it's easier to read. I would also suggest you have one big long string to echo rather than half a dozen individual echo statements.
Hope this helps.
Also, for readability you might want to consider:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<td> <?php echo $row; ?> </td>
<td> <?php echo $row; ?> </td>
<td> <?php echo $row; ?> </td>
<td> <?php echo $row; ?> </td>
<td> <?php echo $row; ?> </td>
<?php
}
Depending on your editor (textpad for example) this will be easier to read after code highlighting.
is shorthand for
fyi.
Actually, this hasn't been true for years. Double-quotes and single quotes are parsed at basically the same speed. Check out The PHP Benchmark for evidence, and more neat benchmarks about what you should and shouldn't do.
But on the subject of optimisation, here's the #1 best piece of advice you're ever going to get:
DON'T.
Seriously, premature optimization is the root of all evil, or if not all evil, at least the vast majority of programming mistakes I've run into. If you're doing tricky things at the expense of the clarity of your code, you're going to run into problems later when you are trying to fix a bug and you don't quite understand what you did three months ago in the name of saving another thousandth of a second.
Also, consider that until the program is written, you don't know what parts need to be optimized. You could spend a day cutting the run time of record loading in half, only to learn that record loading normally takes a tenth of a second and record DISPLAYING is taking two seconds.
Optimization is best left until very, very, last and only until AFTER you've been solidly convinced that you NEED it (people are complaining) and you KNOW where your program is actually slow.
But are overall a bad habit to learn.
<?php is just silly. And in the event that I ever ran into the issue it would take all of five minutes to fix.
I have. It's particularly an issue if you're dealing with academic clients that often use their own hosting platforms.
Our first game is now available for free on Google Play: Frontier: Isle of the Seven Gods
The best part is that you can't write XML documents with shorttags on, since PHP thinks the XML declaration is a short tag. :P
Always fun to have to start a page with <?php echo("<?xml ...") ?>. I had to make a quick and ugly script that replaced the XML declaration in files with PHP echoing the declaration instead before they got uploaded to the server that I had no control over.
Sure it's ugly, but you only have to do it in headers, and so long as you and the compiler understand it, so what?
Alternatively, there's <% and %>, which I believe work in PHP just fine. Not so sure if <%=$stuff%> works, but it looks just weird to me anyway, so I don't bother with 'em.