0

How to replace value in database to another value during view?

Let say in database , 'type' data stored in database is 1 but during view i need to replace to string value "home loan"

Sample data in database

This is my coding in php:

$loans = mysql_query("SELECT * FROM loans");

echo "<table cellspacing='2'>";

echo "<tr><th>ID</th><th>Name</th><th width=70>Type</th><th width=70>Amount</th><th width=70>Duration</th><th>Installment</th><th></th><th></th></tr>";
?>
<form method="post" action="">
<?php
while ($row = mysql_fetch_array($loans)) {

    echo "<tr>";

    echo "<td>" . $row["loan_id"] . "</td>";

    echo "<td>" . $row["name"] . "</td>";

    echo "<td>" . $row["type"] . "</td>";

    echo "<td>" . $row["amount"] . "</td>";

    echo "<td>"  . $row["duration"] .  "</td>";

    echo "<td>"  . $row["installment"] .  "</td>";

    echo "</tr>";

}

echo "</table>";

5 Answers 5

2

You should create another table with values associated with each of the types and join the value on that table.

Table loan_types

id | loan_type
1  | Home Loan
2  | Other Loan

Then your query

<?php
$loans = mysql_query("SELECT loans.*, loan_types.`loan_type` FROM loans LEFT JOIN loan_types ON loans_types.`id` = loans.`type`");
?>

<table cellspacing='2'>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th width=70>Type</th>
        <th width=70>Amount</th>
        <th width=70>Duration</th>
        <th>Installment</th>
    </tr>
    <?php while ($row = mysql_fetch_array($loans)) { ?>
    <tr>
        <td><?php echo $row["loan_id"]; ?></td>
        <td><?php echo $row["name"]; ?></td>
        <td><?php echo $row["loan_type"]; ?></td>
        <td><?php echo $row["amount"]; ?></td>
        <td><?php echo $row["duration"]; ?></td>
        <td><?php echo $row["installment"]; ?></td>
    </tr>
    <?php } ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

try this

  if($row["type"]==1){ 
     echo "<td> Home Loan </td>";
  } else { 
     echo "<td>other type</td>"; 
  } 

Hope it will help

Comments

1

A simple if condition will work for this.

if($row['type'] == 1) {
    echo "<td>home loan</td>";
} else {
    echo "<td>" . $row["type"] . "</td>";
}

Comments

0

i like this approach:

$types = array(1=>"Home Loan");

$echo_type = isset($types[$row['type']])?$types[$row['type']]:$row['type'];
echo "<td>".$echo_type."</td>";

Comments

0
while ($row = mysql_fetch_array($loans)) {

   printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",$row["loan_id"],$row["name"], ((int)$row["type"] === 1 ? 'home loan':'other'),$row["amount"], $row["duration"], $row["installment"]) ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.