Jump to content

[SOLVED] Help Displaying Table in php Code


mcubedma

Recommended Posts

I have a PHP script that has a drop down to select a record, then it should display some text from a database and then display a table of information below it. But, the table is displaying at the top instead.

 

You can view at

http://innovativemgtsolutions.com/li...locdisplay.php

 

I'd appreciate it if you could take a look. Thanks very much.

 

Here is the complete code, although my problem is with the car_desc section:

 

<?php 
//connect to database 

$link = mysql_pconnect('localhost', 'xxxxx', 'xxxxx'); 

if (!$link) { 

    die('Could not connect: ' . mysql_error()); 

} 

mysql_select_db("_loc", $link); 

if (!$_POST)  { 
    //haven't seen the selection form, so show it 
    $display_block = "<h1>Select an Entry</h1>"; 

     
    //get parts of records 
    $get_list_sql = "SELECT mbr_id, 
                     CONCAT_WS(', ', l_name, f_name) AS display_name 
                     FROM member_name ORDER BY l_name, f_name"; 
    $get_list_res = mysql_query($get_list_sql, $link) or die(mysql_error()); 

    if (mysql_num_rows($get_list_res) < 1) { 
        //no records 
        $display_block .= "<p><em>Sorry, no records to select!</em></p>"; 

    } else { 
        //has records, so get results and print in a form 
        $display_block .= " 
        <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\"> 
        <p><strong>Select a Record to View:</strong><br/> 
        <select name=\"sel_id\"> 
        <option value=\"\">-- Select One --</option>"; 

        while ($recs = mysql_fetch_array($get_list_res)) { 
            $id = $recs['mbr_id']; 
            $display_name = stripslashes($recs['display_name']); 

            $display_block .= "<option value=\"".$id."\">".$display_name."</option>"; 
        } 

        $display_block .= " 
        </select> 
        <p><input type=\"submit\" name=\"submit\" value=\"View Selected Entry\"></p> 
        </form>"; 
    } 
    //free result 
    mysql_free_result($get_list_res); 

} else if ($_POST) { 
    //check for required fields 
    if ($_POST["sel_id"] == "")  { 
        header("Location: wedfortest.php"); 
        exit; 
    } 

    //get master_info 
    $get_master_sql = "SELECT concat_ws(' ', f_name, l_name) as display_name 
                       FROM member_name WHERE mbr_id = '".$_POST["sel_id"]."'"; 
    $get_master_res = mysql_query($get_master_sql, $link) or die(mysql_error()); 

    while ($name_info = mysql_fetch_array($get_master_res)) { 
        $display_name = stripslashes($name_info['display_name']); 
    } 

    $display_block = "<p><strong>Showing Record for LOC Member: ".$display_name."</strong></p>"; 

    //free result 
    mysql_free_result($get_master_res); 

    //get all addresses 
    $get_addresses_sql = "SELECT address, city, state, zip 
                          FROM address WHERE mbr_id = '".$_POST["sel_id"]."'"; 
    $get_addresses_res = mysql_query($get_addresses_sql, $link) or die(mysql_error()); 

    if (mysql_num_rows($get_addresses_res) > 0) { 

        $display_block .= ""; 

        while ($add_info = mysql_fetch_array($get_addresses_res)) { 
            $address = stripslashes($add_info['address']); 
            $city = stripslashes($add_info['city']); 
            $state = stripslashes($add_info['state']); 
            $zipcode = stripslashes($add_info['zip']); 

            $display_block .= "".$display_name."<br>$address<br>$city, $state $zipcode<br>"; 
        } 

        $display_block .= ""; 
    } 

    //free result 
    mysql_free_result($get_addresses_res); 

        //get all email 
    $get_email_sql = "SELECT email FROM email 
                      WHERE mbr_id = '".$_POST["sel_id"]."'"; 
    $get_email_res = mysql_query($get_email_sql, $link) or die(mysql_error()); 

     if (mysql_num_rows($get_email_res) > 0) { 

        $display_block .= ""; 

        while ($email_info = mysql_fetch_array($get_email_res)) { 
            $email = stripslashes($email_info['email']); 
             
            $display_block .= "<a href='mailto:$email'>$email</a>"; 
        } 

        $display_block .= ""; 
    } 
     
    //get all tel 
    $get_tel_sql = "SELECT h_phone, b_phone, c_phone, f_number FROM phone 
                    WHERE mbr_id = '".$_POST["sel_id"]."'"; 
    $get_tel_res = mysql_query($get_tel_sql, $link) or die(mysql_error()); 

    if (mysql_num_rows($get_tel_res) > 0) { 

        $display_block .= ""; 

        while ($tel_info = mysql_fetch_array($get_tel_res)) { 
            $h_phone = stripslashes($tel_info['h_phone']); 
            $b_phone = stripslashes($tel_info['b_phone']);             
            $c_phone = stripslashes($tel_info['c_phone']); 
            $f_number = stripslashes($tel_info['f_number']); 
                         
            $display_block .= "<p>Home Phone: $h_phone<br>Work Phone: $b_phone<br>Cell Phone: $c_phone<br>Fax: $f_number</p>"; 
        } 

        $display_block .= ""; 
    } 

    //free result 
    mysql_free_result($get_tel_res); 

        //get all car_desc 
$get_car_sql = "SELECT cardesc_id, year, model, body_num, engine_num, designer, builder, body_style, cond FROM car_desc 
WHERE mbr_id = '".$_POST["sel_id"]."'"; 
$get_car_res = mysql_query($get_car_sql, $link) or die(mysql_error()); 

if (mysql_num_rows($get_car_res) > 0) { 

echo "<table width='100%' border='1' cellpadding='2' cellspacing='2' bgcolor='#ffffff'>"; 
echo "<tr>"; 
echo "<th>Car ID</th><th>Year</th><th>Model</th><th>Body Number</th><th>Engine Number</th><th>Designer</th><th>Builder</th><th>Body Style</th><th>Condition</th>"; 
echo "</tr>"; 

while ($car_info = mysql_fetch_array($get_car_res)) { 
    $cardesc_id = stripslashes($car_info['cardesc_id']); 
    $year = stripslashes($car_info['year']); 
    $model = stripslashes($car_info['model']); 
    $body_num = stripslashes($car_info['body_num']); 
    $engine_num = stripslashes($car_info['engine_num']); 
    $designer = stripslashes($car_info['designer']); 
    $builder = stripslashes($car_info['builder']); 
    $body_style = stripslashes($car_info['body_style']); 
    $cond = stripslashes($car_info['cond']); 
     
    echo "<tr>"; 
    echo "<td>$car_id</td><td>$year</td><td>$model</td><td>$body_num</td><td>$engine_num</td><td>$designer</td><td> 
    $builder</td><td>$body_style</td><td>$cond</td>"; 
    echo "</tr>"; 
} 

echo "</table>"; 

} 

//free result 
mysql_free_result($get_car_res); 
     
        $display_block .= "<br/> 
    <p align=\"center\"><a href=\"".$_SERVER["PHP_SELF"]."\">select another</a></p>"; 
} 
?> 
<html> 
<head> 
<title>My Records</title> 
</head> 
<body> 
<?php echo $display_block; ?> 
</body> 
</html> 

Link to comment
https://forums.phpfreaks.com/topic/70223-solved-help-displaying-table-in-php-code/
Share on other sites

That's because the table is echoed partway through the script, while the rest is only assembled inside $display_block through the script and then echoed at the end of it.

 

To fix it, instead of echoing the table data, just store it inside another $display_table (or whatever name suits you) variable and echo it after the $display_block.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.