Search code examples
phpmysqlinsert

Show a part of a text according to user


I have a form that has 3 types of options:

    <form action="main.php?page=values" method="post">
    <select name=values>
    <option value="A">1</option>
    <option value="B">2</option>
    <option value="C">3</option>
    </select>
</form>

In the page "values", there is the insert into a MySql db, and every option has a sample text.

if (query('get',$_POST['values'])=='A') {
$text = "Text A (Sub-text A)";
} elseif (query('get',$_POST['values'])=='B') {
$text = "Text B (Sub-text B)";
} elseif (query('get',$_POST['values'])=='C') {
$text = "Text C (Sub-text C)";
} 

query_ok("INSERT INTO chat (id, user, text) VALUES ('$id', '$login', '$text')");

When I have to select and show this insert, I would like to show this text in 2 different ways:

a) If user = $login -> Text A/B/C (Sub-text A/B/C)
b) If user != $login -> Text A/B/C

What can I do in order to have this result.

This is the select part:

$show= query_ok("SELECT chat.*, user.*
FROM chat
LEFT JOIN user ON user.name = chat.user
WHERE chat.id = ".$_SESSION['id_chat']." 
ORDER BY id", 'result');

while ($row = query_ok($show, 'fetch'))
{
//results
}

Solution

  • Use array, ex :

    if (query('get',$_POST['values'])=='A') {
        $text = [
            'user' => 'Text A (Sub-text A)',
            'not_user' => 'Text A'
        ];
    } elseif (query('get',$_POST['values'])=='B') {
        $text = [
            'user' => 'Text B (Sub-text B)',
            'not_user' => 'Text B'
        ];
    } elseif (query('get',$_POST['values'])=='C') {
        $text = [
            'user' => 'Text C (Sub-text C)',
            'not_user' => 'Text C'
        ];
    }
    

    AND

    a) If user = $login -> $text['user]

    b) If user != $login -> $text['not_user]