Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Column name in the output of mysql query needs to be changed using php

    Dear Friends,

    I want the result of a database query needs to be sorted .

    I am having a table in which the "ID", "first" "last", "address" , and "position" are the column names . "first " refers to the first name of the employee .

    When I sort the result using the following code , the result is getting sorted , but as I have stored the coulumn names as "first" and "last" , the reulting page also contains "last" , "first" which is not user friendly . I want to change the coulmn names of the table using the php code . Its very difficult for me to go and change the names in the database , because everywhere in our application we have referred with "first" , " last"

    The code that I have taken from codewalkers.com is :


    <?php
    /* set the allowed order by columns */
    $default_sort = 'id';

    $allowed_order = array ('id', 'first', 'last', 'address', 'position');
    /* if order is not set, or it is not in the allowed
    * list, then set it to a default value. Otherwise,
    * set it to what was passed in. */
    if (!isset ($_GET['order']) ||
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
    } else {
    $order = $_GET['order'];
    }
    /* connect to db */
    mysql_connect ('localhost','root');
    mysql_select_db ('mydb');
    echo $order;
    /* construct and run our query */
    $query = "SELECT * FROM employees ORDER BY $order";
    $result = mysql_query ($query);
    /* make sure data was retrieved */
    $numrows = mysql_num_rows($result);
    if ($numrows == 0) {
    echo "No data to display!";
    exit;
    }
    /* now grab the first row and start the table */
    $row = mysql_fetch_assoc ($result);
    echo "<TABLE border=1>\n";
    echo "<TR>\n";
    foreach ($row as $heading=>$dav) {
    /* check if the heading is in our allowed_order
    * array. If it is, hyperlink it so that we can
    * order by this dav */
    echo "<TD><b>";
    if (in_array ($heading, $allowed_order)) {
    echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading

    \">$heading</a>";
    } else {
    echo $heading;
    }
    echo "</b></TD>\n";
    }echo "</TR>\n";
    /* reset the $result set back to the first row and
    * display the data */
    mysql_data_seek ($result, 0);
    while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $dav) {
    echo "<TD>$dav</TD>\n";
    }
    echo "</TR>\n";
    }echo "</TABLE>\n";
    ?>

    I need the coulmn names to be "EmployeeID", "FirstName", "LastName", "Address", "Position"

    Please help me in this .

    I welcome all your valuable suggestions

    Regards,
    David

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    there are many ways to do the replacement , just one of them would be to alter your query so..

    PHP Code:
    <?
    $query 
    "SELECT 
        ID as EmployeeID, 
        first as FirstName,
        last as LastName, 
        address,
        position 
        FROM employees 
        ORDER BY $order
    "
    ;
    ?>
    ...again this is only one way , whether its the best for your situation is hard to say
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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