Jump to content
Larry Ullman's Book Forums

ten120963

Members
  • Posts

    11
  • Joined

  • Last visited

Posts posted by ten120963

  1. Here is all of it:

     

     

    <?php

     

    define('TITLE', 'Test');

    include('templates/header.html');

    print '<h2>Test</h2>';

     

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle the form

     

    // need the database connection

    include('mysql_connect.php');

     

    // validate the form data

     

    $problem = FALSE;

    if (!empty($_POST['name'])) {

     

    // prepare the values for storing:

    $name = trim(strip_tags($_POST['name']));

    } else {

    print '<p style="color: red;">Please submit all fields</p>';

    $problem = TRUE;

    }

     

    if (!$problem) {

     

    $statusarray = array ('Name' => 0);

     

    // Define the query:

    $statusquery = 'SELECT name, status FROM users ORDER BY name';

     

    // run the query

    if ($sq = mysql_query($statusquery, $dbc)) {

     

    // retrieve and process every record:

    while ($statusrow = mysql_fetch_array($sq)) {

     

    $name = $statusrow['name'];

    $status = $statusrow['status'];

    $statusarray[$name] = $status;

     

    } // end of fetch

     

    foreach ($statusarray as $aname => $astatus) {

    print "<p>$aname: $astatus</p>\n";

    }

     

    } else { // Query didn't run.

    print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

    . '.</p><p>The query being run was: ' . $statusquery . '</p>';

    } // End of query IF.

     

    $stickytest = '';

    if(isset($_POST['submit'])) {

    $stickytest = $_POST['name'];

    }

     

    print "$stickytest";

    print $_POST['name'];

     

    if ($astatus <> 'P') {

    print "Invalid choice.";

    $valid_choice = 'FALSE';

    } else {

    $valid_choice = 'TRUE';

    }

     

    if ($valid_choice <> 'FALSE') {

     

    // define the query

     

    $query = "INSERT INTO users (id, name) VALUES (0, '$name')";

     

    // execute the query

     

    if (@mysql_query($query, $dbc)) {

    print '<p>Name has been added.</p>';

    } else {

    print '<p style="color: red;">Could not add the name because:<br />' .

    mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';

    }

     

    }

     

    } // no problem

     

    mysql_close($dbc); // close the connection

     

    } // end of form submission if

     

    // display the form

    ?>

     

    <div><p>Please enter your name:</p>

    <form action="add_name.php" method="post">

     

    <table cellpadding="2" cellspacing="2" align="left">

    <tr>

    <td>Name:</td>

    <td><input type="text" name="name" size="20"

    value="<?php if (isset($_POST['name'])) { print htmlspecialchars($_POST['name']); } ?>" /></td>

    </tr>

     

    <?php

     

    $teams = array ('user1', 'user2');

     

    foreach ($teams as $key => $value) {

     

    print "<tr><td>$value:</td><td><select name=\"$value\">";

     

    include('mysql_connect.php');

     

    $query = "SELECT * FROM $value";

     

    if ($r = mysql_query($query, $dbc)) { // run the query

     

    // if sticky add selected parm

     

    while ($row = mysql_fetch_array($r)) {

     

    if ($stickytest == $row['name']) {

    print "HI";

    print "<option selected=\"selected\" value=\" {$row['name']} \"> {$row['name']} </option>";

    } else {

    print "HO";

    print "<option value=\" {$row['name']} \"> {$row['name']} </option>";

    }

     

    }

     

    } else { // query did not run

    print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

    . '.</p><p>The query being run was: ' . $query . '</p>';

    } // end of query if

     

    mysql_close($dbc); // close the connection

     

    print "</select></td></tr>";

     

    }

     

    ?>

     

    </table>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <input type="submit" name="submit" value="Enter Name" />

     

    </form>

     

    <?php include('templates/footer.html');

    ?>

     

    </div>

    </body>

    </html>

  2. I have seen an example of creating a sticky Drop Down box, but I'm a little confused on how to get it to work.

     

    In the 'if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle the form' section, I have:

     

     

    $stickytest = '';

    if(isset($_POST['submit'])) {

    $stickytest = $_POST['name'];

    }

     

    print "$stickytest";

    print $_POST['name'];

     

    For this code, the two things that are printed are in fact equal during my testing.

     

    Then, in my '} // end of form submission if' '// display the form' code, I have:

     

     

    if ($stickytest == $row['name']) {

    print "HI";

    print "<option selected value=\" {$row['name']} \"> {$row['name']} </option>";

    } else {

    print "HO";

    print "<option value=\" {$row['name']} \"> {$row['name']} </option>";

    }

     

    I am expecting to see either HI or HO during my testing, and I am seeing neither.

  3. My code:

     

     

    $names = array(); // create a variable to hold the query results

     

    $query = "SELECT name FROM users ORDER BY name ASC";

     

    // run the query

     

    if ($r = mysql_query($query, $dbc)) {

     

    // retrieve every record and add to a dropdown box

    while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {

    $names[] = $row; // add the row in to the results array

    }

     

    // query did not run

     

    } else {

    print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

    . '.</p><p>The query being run was: ' . $query . '</p>';

    } // end of query if

     

    mysql_close($dbc); // close the connection

     

    // set up combo box

     

    print '<tr><td>Name:</td><td><select name="name"';

     

    foreach ($names as $num => $tname) {

    print "<option value=\"$num\">$tname</option>";

    }

     

    print "</select></td>";

     

    I get a dropdown box filled with the word 'Array' several times.

  4. That could be it, because I do have two of '$row = mysql_fetch_array($r)' in my code. I am trying to create a form that has a table with a pull down menu of these 32 users, then a pull down menu with values in one row, then the same two pull down menus in the row below before i close the table. So, I am using the statement above two times to get the data twice. Would using $row2 and $q to get the data the second time solve the problem, or is there a more efficient way to get the data once to populate 2 different drop downs?

  5. I have a database table with 32 records in it. I have written PHP code to cycle thru the records, and add the name field from each record into a pull down menu box. So far, I have tried 'select name from users order by name asc' and just 'select name from users', and each time, only 31 of the 32 names are displayed. Has anyone had this happen?

     

     

    $query = "SELECT name FROM users ORDER BY name ASC";

     

    // run the query

     

    if ($r = mysql_query($query, $dbc)) {

     

    // retrieve every record and add to a dropdown box

    while ($row = mysql_fetch_array($r)) {

    print "<option value=\" {$row['name']} \"> {$row['name']} </option>";

    }

     

    // query did not run

     

    } else {

    print '<p style="color: red;">Could not retrieve the data because:<br />' . mysql_error($dbc)

    . '.</p><p>The query being run was: ' . $query . '</p>';

    } // end of query if

     

    print "</select></td>";

  6. I have just completed reading PHP For the Web, and just started reading PHP and MYSQL for Dynamic Web Sites. Both are wonderful books. I have a few other PHP/MYSQL books. A few are ok, and a few are terrible. These books are great.

     

    I am only on page 45 of the second book, but I noticed that in the first book, print is used where in the second book, echo is used. Also, in the first book, $_GET is used, where in the second, $_REQUEST is used.

     

    Is there benefits to using echo and $_REQUEST? Please let me know. Thanks.

×
×
  • Create New...