我在PHP中有这样的输出。我想展示Username
,但无法让它发挥作用。我应该如何调用$formLayout
中的用户名,使其显示为:
Thank You Elaine Byrne for your submission
:
// Define the maximum number of submissions.
$max = 1;
// Get the current logged in user.
$user = JFactory::getUser();
// Get a database connection.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Setup the query.
$query->select('COUNT('.$db->qn('Username').')')
->from($db->qn('#__rsform_submissions'))
->where($db->qn('FormId').'='.$db->q($formId))
->where($db->qn('Username').'='.$db->q($user->get('username')));
// You can also count by User ID, just replace the above with:
// ->where($db->qn('UserId').'='.$db->q($user->get('id')));
$db->setQuery($query);
$counter = $db->loadResult();
if ($counter >= $max){
$formLayout = 'Thank You Username for your submission';
}
答案 0 :(得分:2)
TL; DR
$formLayout = 'Thank You ' . $user->get('username') . ' for your submission';
LONGER VERSION
要链接字符串PHP使用.
符号。它适用于双引号"
或单引号'
。双引号和单引号之间的主要区别在于我们可以将变量传递给字符串而不使用点字符将其链接到原始字符串,而使用单引号时我们总是需要“中断”字符串并将变量链接到。
$addition = 'World';
$double_quotes = "Hello, $addition!";
$single_quotes = 'Hello, ' . $addition .'!';
echo $double_quotes; //echoes Hello, World!
echo $single_quotes; //echoes Hello, World!
就PHP函数,数组或对象而言,无论我们使用单引号还是双引号,我们总是需要使用点字符来链接字符串。
$addition = array('World', 'Tony', 'Elaine');
for($i=0;$i<count($addition);$i++)
{
echo "Hello, " . $addition[$i] . "!<br>";
}
//echoes Hello, World!
// Hello, Tony!
// Hello, Elaine!
希望它足够清楚:)