Reputation: 59
I need help on concatenation. I have a Title which is displayed below like this:
<h1>: SESSION (<?php echo $_SESSION['id'] ?>) <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
In words the above coding could read like this:
Session(ABB) 1 OF 2
Now Imagine I am creating 2 exams ($_SESSION['initial_count'] = ‘2’), both exams only have 1 question. I want the database to INSERT the VALUES like below:
Session Id Question
ABB1 Who are you
ABB2 Who am I
As you can see above exam ‘ABB1’ which is the first exam which has its own question and exam ‘ABB2’ which is second exam has its own question.
BUT if there is only 1 exam, then I do not want the ‘1’ to be displayed next to the
SessionId, like below:
Session Id Question
ABB Who are you
So how can I concatenate so that if $_SESSION['initial_count'] = ‘1’
then do not include the number 1 next to the sessioId, else if there are multiple $_SESSION['initial_count']
, then concatenate the $sessionMinus
number next to SessionId
?
Below is the code where I want the concatenation to occur.
foreach($_POST['questionText'] as $question)
{
$insertquestion[] = "' ". mysql_real_escape_string( $_SESSION['id'] ) . "' , ' ". mysql_real_escape_string( $_POST['num_questions'] ) . "', ' ". mysql_real_escape_string( $question ) . "'";
}
Upvotes: 1
Views: 542
Reputation: 1032
Ternary operator is your friend.
$insertquestion[] = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $sessionMinus : '') ...;
Upvotes: 3
Reputation: 757
Something like that ?
$sessionId = $_SESSION['id'] . ($_SESSION_['initial_count'] > 1 ? $sessionMinus : '');
echo '<h1> : SESSION ' . $sessionId . ' of ' . $_SESSION_['initial_count'];
Upvotes: 0