当前位置: 动力学知识库 > 问答 > 编程问答 >

php - display text if in mysql there is a number

问题描述:

I have a database with 4 columns, id, user, vipplan and expire.

What i want to do, is this:

i have my vipplan table, and:

if number 0 is in database: then display "N/A"

if number 1 is in database: then display "Sr.Pro"

if number 2 is in database: then display "No seas"

if number 3 is in database: then display "Level0"

But i do not know how to do it, i post my code:

<?php

echo "<h2>Resultados:</h2><p>";

if(isset($_POST['user']))

{

$find = $_POST['user'];

if ($find == "")

{

echo "<p>Olvidaste poner tu usuario -.-!!!";

exit;

}

mysql_connect("localhost", "root", "2513") or die(mysql_error());

mysql_select_db("invasionvip") or die(mysql_error());

$find = strtoupper($find);

$find = strip_tags($find);

$find = trim ($find);

$iname = mysql_query("SELECT * FROM users WHERE usuario LIKE '%$find%'")

or die(mysql_error());

while($result = mysql_fetch_array( $iname ))

{

echo "id:" .$result['id'];

echo "<br>";

echo "usuario:".$result['usuario'];

echo "<br>";

echo "Plan VIP:".$result['planvip'];

echo "<br>";

echo "Vencimiento :".$result['vencimiento'];

echo "<br>";

}

$anymatches = mysql_num_rows($iname);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query...<br><br>";

}

echo "<b>Searched For:</b> " .$find;

}

?>

and my form is very simple:

<form class="pure-form" method="post" action="user.php">

<center>Aqui podrás ver tus servicios activos, como V.I.P, vehiculos, skins, etc.</center><br/>

<span style="text-align:left;font-size:25px;">Usuario:&nbsp;</span>

<input type="text" name="user">

<button type="submit" class="pure-button">Search</button>

</form>

thank you, :)

网友答案:

I believe what you are after is a case select. You should put this in the select line of your query.

网友答案:

there are multiple way that you can achieve this ourput i thing the easiest way is switch statement second if else if and else third one is array notation which @b0s3 define earlier.

Switch example

switch(expression) {
    case 1:
        echo "Sr.Pro";
        break;
    case 2:
        echo "No seas";
        break;
    case 3:
        echo "Level0";
        break;
    default:
        echo "N/A";
}

if else example

if( $databaseValue == 1 ){
    echo "Sr.Pro";
}else if( $databaseValue == 2 ){
    echo "No seas";
}else if( $databaseValue == 3 ){
    echo "Level0";
}else{
    echo "N/A";
}

Array Example

$yourValues = ['N/A', 'Sr.Pro', 'No seas', 'Level0'];

echo $yourValues[$databaseValue];

now it's up to your which one you like to use

分享给朋友:
您可能感兴趣的文章:
随机阅读: