As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

PHP help

Nite-ManNite-Man Registered User regular
edited November 2009 in Help / Advice Forum
I'm trying to create a page using php. It should look like:

74049939.jpg

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

Posts

  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    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?

    admanb on
  • Nite-ManNite-Man Registered User regular
    edited October 2009
    admanb wrote: »
    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?



    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.

    Nite-Man on
    The first significant thing living here taught me is conformity costs money, and everybody pays.
  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited October 2009
    Well, what values are stored in the database's 'nation' field?

    admanb on
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited October 2009
    Nite-Man wrote: »
    admanb wrote: »
    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?



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

    SeñorAmor on
  • GothicLargoGothicLargo Registered User regular
    edited November 2009
    I agree with the previous, I don't see the "nation" tabledata line.

    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.

    GothicLargo on
    atfc.jpg
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited November 2009
    <?=
    

    is shorthand for
    <?php echo
    

    fyi.

    SeñorAmor on
  • ImperfectImperfect Toronto, Ontario, CanadaRegistered User regular
    edited November 2009
    SeñorAmor wrote: »
    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
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited November 2009
    SeñorAmor wrote: »
    <?=
    

    is shorthand for
    <?php echo
    

    fyi.
    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
  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    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.

    admanb on
  • ImperfectImperfect Toronto, Ontario, CanadaRegistered User regular
    edited November 2009
    Shorttags are fine if you're not running on shared hosting and can control them yourself.

    Imperfect on
  • SzechuanosaurusSzechuanosaurus Registered User, ClubPA regular
    edited November 2009
    Imperfect wrote: »
    Shorttags are fine if you're not running on shared hosting and can control them yourself.

    But are overall a bad habit to learn.

    Szechuanosaurus on
  • InfidelInfidel Heretic Registered User regular
    edited November 2009
    I don't understand the hate for short tags. I've never run into a host that actually wouldn't enable them.

    <?php is just silly. And in the event that I ever ran into the issue it would take all of five minutes to fix.

    Infidel on
    OrokosPA.png
  • HeartlashHeartlash Registered User regular
    edited November 2009
    Infidel wrote: »
    I don't understand the hate for short tags. I've never run into a host that actually wouldn't enable them.

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

    Heartlash on
    My indie mobile gaming studio: Elder Aeons
    Our first game is now available for free on Google Play: Frontier: Isle of the Seven Gods
  • EchoEcho ski-bap ba-dapModerator mod
    edited November 2009
    Heartlash wrote: »
    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.

    Echo on
  • ImperfectImperfect Toronto, Ontario, CanadaRegistered User regular
    edited November 2009
    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.

    Imperfect on
Sign In or Register to comment.