Jump to content

formatting the data retrieved from MySQL


ento

Recommended Posts

I'm trying to modify code from a book called "PHP with MySQL" where data is read from a MySQL db, edited via a form and written back.

 

It's all technically working but the data is written to the screen from left to right as follows (where the top row is the heading and the second row is the data retrieved from the database in an edit box.):

 

First Name | Last Name |

<David> | <Beckham> |

 

I have tons of fields (but only have firstname/lastname in the script for now), so the headings scroll left to right on the screen and I find it really annoying. I'd like things to go top-to-bottom so it looks like this:

 

First Name | <David>

Last Name | <Beckham>

 

I've been playing around with the code and trying to add <tr>'s here and there but I can't get it.  At best, it looks like this:

 

First Name

Last Name

<David>

<Beckham>

 

I'm hoping it's something simple, but I just can't see it.

 

Any help would be appreciated... the full code is below

# cat editrecord.php
<html>
<head>

<style>
body {font-family:arial;}
.error {font-weight:bold; color:#FF0000;}
</style>

<title>Edit Contact Records</title>
</head>

<body>

<h2>Edit Contact Records:</h2>

<?
//connect to the database
include("dbinfo.php");
$link = mysqli_connect($server, $username, $password, $database);

// has the form been submitted?
if ($_POST) {
    foreach($_POST as $k => $v) {
        $v = trim($v);
        $$k = $v;
    }


    // build UPDATE query
    $update = "UPDATE contact_records SET 
        firstname='$firstname',
        lastname='$lastname' 
        WHERE Id=$id";


    // execute query and check for success
    if (!mysqli_query($link, $update)) {
        $msg = "Error updating data";
    } else {
        $msg = "Record successfully updated:";

        // write table row confirming data
        $table_row = '
        <tr>
            <td>' . $firstname . '</td>
            <td>' . $lastname . '</td>
        </tr>';
        }

        // if not posted, check that an Id has been passed via the URL
        } else {
        if (!IsSet($_GET['id'])) {
            $msg = "No customer selected!";
        } else {
            $id = $_GET['id'];

            //build and execute the query
            $select = "SELECT firstname, lastname FROM contact_records where id=$id";
            $result = mysqli_query($link, $select);

            // check that the record exists
            if (mysqli_num_rows($result)<1) {
                $msg = "No customer with that ID found!";
            } else {
            // set vars for form code
            $form_start = "<form method=\"post\"action=\"" . $_SERVER['PHP_SELF'] . "\">";
            $form_end = '
            <tr>
                <td colspan="2"><input type="submit" value="Submit changes" /></td>
                <td colspan="3"><input type="reset" value="Cancel" /></td>
            </tr>
            </form>';

        // assign the results to an array
        while ($row = mysqli_fetch_array($result)) {
            $firstname = $row['firstname'];
            $lastname = $row['lastname'];

            // write table row with form fields
            $table_row = '
            <tr>
                <td><input type="text" name="firstname" value="' . $firstname . '" size="10" /></td>
                <td><input type="text" name="lastname" value="' . $lastname . '" size="10" /></td>
            </tr>';
        }
        // end 'if record exists' if
        }
    //end 'if ID given in URL' if
    }
// end 'if form posted' if
}

// close connection
mysqli_close($link);

// print error/success message
echo (IsSet($msg)) ? "<div class=\"error\">$msg</div>" : "";
?>

<table border="1" cellpadding="5">
<!-- Show start-of-form code if form needed -->
<? echo (IsSet($form_start)) ? $form_start : "";
?>

<input type="hidden" name="id" value="<? echo $id ?>" />
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
    </tr>
    <!-- Show appropriate table row code (none set if there were errors) -->
    <? echo (IsSet($table_row)) ? $table_row : "";
?>

<!-- Show end-of-form code if we are displaying the form -->
<? echo (IsSet($form_end)) ? $form_end : ""; ?>
</table>

<br /><a href="records.php">Back to customer list</a>
</body>
</html>

 

Link to comment
Share on other sites

I think I'm making this out to be a lot harder than it should be... I just removed the column heading definition from the last part of the script and defined each line separately... it seems to work

 

Old code:

// write table row with form fields
            $table_row = '
            <tr>
                <td><input type="text" name="firstname" value="' . $firstname . '" size="10" /></td>
                <td><input type="text" name="lastname" value="' . $lastname . '" size="10" /></td>
            </tr>';

 

New code:

// write table row with form fields
            $table_row = '
            <tr>
                <td>First Name</td>
                <td><input type="text" name="firstname" value="' . $firstname . '" size="10" /></td>
            </tr>
            <tr>
                <td>Last Name</td>    
                <td><input type="text" name="lastname" value="' . $lastname . '" size="10" /></td>
            </tr>';

 

Old code:

<input type="hidden" name="id" value="<? echo $id ?>" />
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
    </tr>
    <!-- Show appropriate table row code (none set if there were errors) -->
    <? echo (IsSet($table_row)) ? $table_row : "";
?>

 

New code:

<input type="hidden" name="id" value="<? echo $id ?>" />
    <!-- Show appropriate table row code (none set if there were errors) -->
    <? echo (IsSet($table_row)) ? $table_row : "";
?>

 

 

Feel free to suggest more efficient ways to do this :-)

Link to comment
Share on other sites

insted of

<table>
<tr>
<td>first name</td><td>last name</td>
</tr>
<tr>
<td> <?php echo $firsttname ?> </td><td><?php echo $lastname ?></td>
</tr>
</table>

 

use

 

<table>
<tr>
<td>first name</td><td> <?php echo $firsttname ?> </td>
</tr>
<tr>
<td> Last Name </td><td><?php echo $lastname ?></td>
</tr>
</table>

 

then for each extra row add before the </table> tag

 


<tr>
<td>column name</td><td> <?php echo $var ?> </td>
</tr>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.