SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting an Input Form before a Dynamic Table

    I put a screenshot of an add/edit page I'm working on online at http://www.geoworld.org/addedit2.gif

    It's working quite nicely, but I still have several things to fix and a few changes to make. First, notice that the column on the left displays two values, like "Alberta province" and "Alaska state." I would like to display just one value - the name of each state or province.

    Since "Alaska" and "Alberta" come from a field named NameS and "state" and "province" represent a field named TypeS, I thought I could fix that by changing this row...

    PHP Code:
    printf("<tr><td><a href=\"%s?ID=%s\">%s %s</a></td> \n"$PHP_SELF$myrow["ID"], $myrow["NameS"], $myrow["TypeS"]); 
    to this...

    PHP Code:
    printf("<tr><td><a href=\"%s?ID=%s\">%s %s</a></td> \n"$PHP_SELF$myrow["ID"], $myrow["NameS"]; 
    But I get this error message on every row:

    "Warning: printf(): Too few arguments in C:\sites\geoworld\tools\edit\statesrefined.php on line 75"

    I didn't realize PHP operated on a quota system! Do you have any idea how to fix that?

    * * * * * * * * * *

    Next, this table will have about 200 rows, and my Nations table will have about 250 rows. Therefore, I'd like to put the edit form at the top of the page, rather than making visitors scroll down to the bottom.

    In other words, I'd like it to look like this: http://www.geoworld.org/addedit3.gif

    But I'm not sure how to do that. I moved the entire form to the top of the page - above EVERYTHING except the head section. But I got this error message:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\sites\geoworld\tools\edit\statesrefined.php on line 24

    I then tried inserting the form at various places AFTER this...

    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    but I get a variety of parse errors and other error messages. I'm not even sure where to put it. Do you have any idea how to do this?

    I posted the code for the entire page below for reference. Thanks!

    PHP Code:
    <html>
    <head>
    <style type="text/css"> 
    body { font-family: Arial, Verdana, sans-serif; font-size: 80%; line-height: 130%; }
    a:link { color: #00f; text-decoration: none }
    a:visited { color: #00f; text-decoration: none }
    a:hover { color: #f00; background-color: #ff0; text-decoration: none }
    </style>
    </head>
    <body>

    <?php
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    /* Error handling
    if (!$connect)
    {
       errorHandling('Database Error: Unable to connect to the database.'); */
    if ($submit) {
      
    // here if no ID then adding else we're editing
      
    if ($ID) {
        
    $sql "UPDATE states SET SCode='$SCode',NameS='$NameS',TypeS='$TypeS',Capital='$Capital',NameOrigin='$NameOrigin',Nicknames='$Nicknames',Residents='$Residents',NCode='$NCode',ID='$ID' WHERE ID=$ID";
      } else {
        
    $sql "INSERT INTO states (SCode,NameS,TypeS,Capital,NameOrigin,Nicknames,Residents,NCode,ID) VALUES ('$SCode','$NameS','$TypeS','$Capital','$NameOrigin','$Nicknames','$Residents','$NCode','$ID')";
      }
      
    // run SQL against the DB
      
    $result mysql_query($sql);
      echo 
    "Record updated/edited!<p>";
    /* Check if $result contains a resource-ID (is_resource())
    and check mysql_error() if not. */
    } elseif ($delete) {
        
    // delete a record
        
    $sql "DELETE FROM states WHERE ID=$ID";    
        
    $result mysql_query($sql);
        echo 
    "$sql Record deleted!<p>";
    } else {
      
    // this part happens if we don't press submit
      
    if (!$ID) {
        
    // print the list if there is not editing
        
    $result mysql_query("SELECT * FROM states",$db);
    echo 
    "<table>";
    while (
    $myrow mysql_fetch_array($result)) {
    printf("<tr><td><a href=\"%s?ID=%s\">%s %s</a></td> \n"$PHP_SELF$myrow["ID"], $myrow["NameS"], $myrow["TypeS"]);
    printf("<td><a href=\"%s?ID=%s&delete=yes\">(DELETE)</a></td></tr>"$PHP_SELF$myrow["ID"]);
    }
    }
      
    ?>
      <form method="post" action="<?php echo $PHP_SELF?>">
      <?php
      
    if ($ID) {
        
    // editing so select a record
        
    $sql "SELECT * FROM states WHERE ID=$ID";
        
    $result mysql_query($sql);
        
    $myrow mysql_fetch_array($result);
        
    $ID $myrow["ID"];
        
    $SCode $myrow["SCode"];
        
    $NameS $myrow["NameS"];
        
    $TypeS $myrow["TypeS"];
        
    $Capital $myrow["Capital"];
        
    $NameOrigin $myrow["NameOrigin"];
        
    $Nicknames $myrow["Nicknames"];
        
    $Residents $myrow["Residents"];
        
    $NCode $myrow["NCode"];
        
    $ID $myrow["ID"];
        
    // print the id for editing
        
    ?>
        <input type=hidden name="ID" value="<?php echo $ID ?>">
        <?php
      
    }
      
    ?>
      <table width="100%">
      <tr><td>SCode:</td><td><input type="Text" name="SCode" value="<?php echo $SCode ?>"></td>
        <td class="tdarticle" rowspan="9">Type or paste your article in here.<br />
    <textarea name="Article" style="width:100%; height:200px;"></textarea></td></tr>
      <tr><td>NameS:</td><td><input type="Text" name="NameS" value="<?php echo $NameS ?>"></td></tr>
      <tr><td>TypeS:</td><td><input type="Text" name="TypeS" value="<?php echo $TypeS ?>"></td></tr>
      <tr><td>Capital:</td><td><input type="Text" name="Capital" value="<?php echo $Capital ?>"></td></tr>
      <tr><td>NameOrigin:</td><td><input type="Text" name="NameOrigin" value="<?php echo $NameOrigin ?>"></td></tr>
      <tr><td>Nicknames:</td><td><input type="Text" name="Nicknames" value="<?php echo $Nicknames ?>"></td></tr>
      <tr><td>Residents:</td><td><input type="Text" name="Residents" value="<?php echo $Residents ?>"></td></tr>
      <tr><td>NCode:</td><td><input type="Text" name="NCode" value="<?php echo $NCode ?>"></td></tr>
      <tr><td>ID:</td><td><input type="Text" name="ID" value="<?php echo $ID ?>"></td></tr>
      <tr><td colspan=2><input type="Submit" name="submit" value="Enter information"></td></tr>
    </table>
      </form>
      <P>
      <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
      <P>
    <?php
    }
    ?>
    </body>
    </html>

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    concerning the first part: you still have four parameters (%s) in your printf format string, but you pass over only three variables.

    concerning the second part: what kind of error messages?
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme
    Hi,

    concerning the first part: you still have four parameters (%s) in your printf format string, but you pass over only three variables.

    concerning the second part: what kind of error messages?
    Oh, so you mean if I deleted one "%s," then it would allow me to display just one value?

    What kind of error messages? Well, if I put the form immediately after the database connection and error handler, like this...

    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""testuser""TestUser");
    mysql_select_db("world",$db);
    /* Error handling
    if (!$connect)
    {
       errorHandling('Database Error: Unable to connect to the database.'); */

      
    <form method="post" action="<?php echo $PHP_SELF?>">
      <?
    php
      
    if ($ID) {
    I get this message:

    Parse error: parse error, unexpected '<' in C:\sites\geoworld\tools\edit\statesrefined.php on line 36

    The error goes away if I precede the form with echo ' and put '; at the end.

    However, when I display my page, every input form is filled with text that reads something like this:

    <?php echo $SCode ?>

    where SCode is the name of a field.

    Actually, I get the same results if I past the form just before the
    mysql_fetch_array function.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ofcourse you don't close php by ?> and you don't use an echo statement ...

    php will try to parse pure HTML and will spit it out as it should
    the neigbours (free) WIFI makes it just a little more fun

  5. #5
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Oh, so you mean if I deleted one "%s," then it would allow me to display just one value?
    Yes You've removed the fourth variable (i.e. $myrow["TypeS"]), so just also remove the fourth %s.

    I can't quite see why your input forms are filled with php code. But I would do one thing to make the script more legible: separate the html and the php parts. First I would do all the php stuff like variable checking, fetching data from the db, filling local variables and so on. Afterwards, outside of the <?php (...) ?> tags, you can write the pure html. The only exception being the small parts of "inline php" where you are filling the input forms with their default values.
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kleineme,

    "Yes You've removed the fourth variable (i.e. $myrow["TypeS"]), so just also remove the fourth %s."

    Great; that's another good tip to add my notes. I'm having a hard time understanding how the various parts of these things work, so I just chip away, fixing one thing at a time.

    "I can't quite see why your input forms are filled with php code. But I would do one thing to make the script more legible: separate the html and the php parts. First I would do all the php stuff like variable checking, fetching data from the db, filling local variables and so on. Afterwards, outside of the <?php (...) ?> tags, you can write the pure html. The only exception being the small parts of "inline php" where you are filling the input forms with their default values."

    OK. Actually, I think the only html is the form. I moved that outside the PHP tag, and it works just fine.

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •