开发者

Tabular data modification (on output) of MySQL Select Statement

Greetings! This is my first question.

I've searched all over the web as well as on SO, but I believe it is too specific for me to find an answer. I'm guessing my code may need a logical rebuild.

Background of problem:

I am building a simple site containing user-editable inventory data (a list of used RVs) for a friend's business. This is basically my first big project with php, well, ever, and I'm on a tight deadline (we're in season right now!), so my code is absolute filth. To add to that, I'm mixing procedural and object-oriented programming like a nutjob.

Explanation of problem:

Some of the data in the MySQL table "rv_list" - namely, the 'status' and the 'condition' fields (see attached screenshot to get a clearer idea), are currently being stored as 1-digit integers.

I thought that it would be very easy to (using php regex or something) later convert these numbers to strings, as they were on the input form. As it turns out, with the method I finally settled on outputting the data, I cannot find a way to convert them after the select statement is done.

Code:

<?php
//unfinished query - "condition" is the column in the db table that I'd like to change, "status" will also need to be changed in a similar way for the outward-facing public-viewed list
$query = 'select * from rv_list where status="1" or status="2" order by modified desc';
$rvfieldquery = 'show columns from rv_list';
$result = $conn->query($query);

//check on the status of our query
if ($result)
{
//begin output of html table, followed by a loop for rows, and within, a nested loop for the datacells.
//based on function 3: http://www.barattalo.it/2010/01/29/10-php-usefull-functions-for-mysqli-improved-stuff/
    echo '<table>';
    echo '<th>Edit Status</th>';
    while ($field = $result->fetch_field())
    {
        echo '<th class="columntitle">'.$field->name.'</th>';
    }
    echo '<th>DELETE</th>';
    $shadecounter = 0;
    while ($row = $result->fetch_assoc())
    {
        //shade every other row for usability       
        $shadecounter++;
        if (is_float($shadecounter/2)) $shade = "lightgray"; 
        else $shade = "white";      
        echo '<tr style="background:'.$shade.';">';
        //Get rv_id key, attach to name of edit and delete buttons
        echo '<td><input type="submit" name="edit '.$row['rv_id'].'" value="Edit"></input></td>';
        //Insert row data
        foreach ($row as $td)
        {
        //PROBLEM AREA HERE! - can I do an if statement or something here to check what column $td comes from?  Confused.
        echo '<td class="data">'.$td.'</td>';
        }
        //for delete record button, off-screen on right side of each record
        echo '<td><input style="background:red;" type="submit" name="del '.$row['rv_id'].'" value="DELETE"></input></td>';
        echo '</tr>';
    }
    echo '</table>';
}
else
{
exit;
}

Output Screenshot (check highlights):

http://i.stack.imgur.com/N8P8r.jpg

Extra Information:

Key to the status integer list (rather, what I would like it to display):

  1. Available
  2. On Hold
  3. Sold / Deactivated

Key to the condition integer list:

  1. New
  2. Used - Excellent
  3. Used - Good
  4. Used - Fair
  5. Used - Poor

Please let me know if I have forgotten anythi开发者_开发百科ng crucial in describing this problem.

Last but not least, thank you very much for your time spent reading this question!


$status_map = array(1 => 'Available', 2 => 'On Hold', 3 => 'Sold / Deactivated');
$condition_map = array(1 => 'New', 2 => 'Used - Excellent', 3 => 'Used - Good', 4 => 'Used - Fair', 5 => 'Used - Poor');

foreach ($row as $colname => $value)
{
    if($colname == 'status')
        $value = $status_map[$value];

    if($colname == 'condition')
        $value = $condition_map[$value];

    echo '<td class="data">'.$value.'</td>';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜