I have 3 arrays $personal1 , $personal2 and $business , each one holds 1 field from each query,
so for example 'Mr' 'John Smith' 'Johnscorp Ltd'.
I am trying to construct the following if query >>
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
What it does is check to see if the salutation is blank or not, if the field is blank the business name is echo'd instead.
The problem I have is how do I merge the personal and personal2 into one array ?.
I am not sure if I can do this :
$personal2 = $userinfo->leadname;
$personal1 = $userinfo->salutation;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal1 & $personal2;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if I can do this ?
$personal = $userinfo->salutation,$userinfo->leadname;
$business = $userinfo->businessname;
if ($personal1=="")
$name = $business;
else
$name = $personal;
echo '<h1>NAME:';
echo $name;
echo '</h1>';
or if both are incorrect as I dont seem to be getting any results :-S .
I think the first example you have should be working, just replace
$name = $personal1 & $personal2;
with
$name = $personal1 . ' ' . $personal2;
this will join these string with a space between them
PS: Didn't you think strings instead of arrays ?
I'm a bit puzzled because you've stated multiple times that you're using arrays, however in your example, it appears that you're using objects.
Anyways, if all you need is to merge $personal1 and $personal2,
instead of using:
$name = $personal1 & $personal2;
you can just use:
$name = $personal1 + $personal2;
If it's a string you need:
$name = $personal1 . ' ' . $personal2;
array_merge()
or just the + operator
Related
I have made a code that takes the first name and last name from an email, the $firstname is uppercase but $lastname is not. Why?
<html>
<body>
<?php
$email = "test.testt#testing.com";
$firstname = ucfirst(strtok(strtok($email, "#"), "."));
$lastname = substr(strtok(strtok($email, "#"), ".") . ' ' . strtok("."), strrpos(strtok(strtok($email, "#"), ".") . ' ' . strtok("."), ' '));
$lastname = ucfirst($lastname);
echo $firstname.$lastname;
?>
</body>
</html>
Output: Test testt
any help would be greatly appreciated
Besides the actual problem being the space, your code does a lot of searching and chopping strings up.
You could simplify it by using explode() with first the # and then a .. Then using ucfirst on each part of the last operation...
$names = explode("#", $email);
// Get first 2 parts of name and split it by the .
[$firstname, $lastname] = explode(".", $names[0], 2);
$firstname = ucfirst($firstname);
$lastname = ucfirst($lastname);
echo $firstname . ' ' . $lastname;
The space was indeed the problem, I fixed it by using $lastname = ucfirst(str_replace(' ', '', $lastname));
How can I merge two variables?
My code:
$cases = $message->case_number;
$messageText = $message->password;
if (!empty($message->template)) {
$cases = str_replace('_CASE_', $cases, $message->template->text);
$messageText = str_replace('_MESSAGE_', $messageText, $message->template->text);
}
How can I merge $cases and $messageText?
Edit for clarity, from comments below
I want to replace $messageText with MESSAGE and $cases with CASE in just 1 variable something like
$test= str_replace('CASE', $cases, 'MESSAGE',$messageText, $message->template->text);
You can pass both replacement items in to array_replace() at the same time using an array...
if (!empty($message->template)) {
$output = str_replace(['_CASE_', '_MESSAGE_'],
[$cases, $messageText],
$message->template->text);
}
Use the dot to concatenate strings, like this:
$firstname = 'Dave';
$middlename = 'Brexit';
$lastname = 'Davis';
$fullname = $firstname . ' ' . $middlename . ' ' . $lastname; // 'Dave Brexit Davis'
You can always merge two or more variables together by just putting dot between them.
$cases = $message->case_number;
$messageText = $message->password;
if (!empty($message->template)) {
$cases = str_replace('_CASE_', $cases, $message->template->text);
$messageText = str_replace('_MESSAGE_', $messageText, $message->template->text);
$merged = $cases.$messageText;
}
I'm trying to do something very basic but I can't figure out how.
basically i'm trying to convert the mysql result ($row) into the following format (literal strings):
"0784562627828" => "James",
"0786636363663" => "David",
I have all the data stored in the database and I can get them echoed on my page like so:
$phone = $row['phone'];
$name = $row['name'];
$list .=''.$phone.'';
echo $list;
could someone please advise on this?
Thanks
Just assign them inside an array like you normally would:
$array = array();
while(your fetch here) {
$array[$row['phone']] = $row['name'];
}
To check its contents, you can use var_dump($array) or print_r($array)
Or just straight up show them, like the one you formatted:
while(your fetch here) {
echo '"' . $row['phone'] . '"' . ' => ' . '"' . $row['name'] . '"' . '<br/>';
}
you mean something like this?
$list = array();
$list[$phone] = $name;
Can you do something like
$list = [];
foreach($rows as $row) {
$list[$row['phone']] = $row['name'];
}
ik have a html form where i can select some options. I want to write those values comma separated to my database. This is the code i have
$genretotal = $_POST['genre'];
$genre0 = $genretotal[0];
$genre1 = $genretotal[1];
$genre2 = $genretotal[2];
$genre3 = $genretotal[3];
$genre4 = $genretotal[4];
$genre5 = $genretotal[5];
$genre6 = $genretotal[6];
$genre7 = $genretotal[7];
$genre = $genre0 . "," . $genre1 . "," . $genre2 . "," . $genre3 . "," . $genre4 . "," . $genre5 . "," . $genre6 . "," . $genre7;
How can i leave out the empty values?
Try with implode and array_filter
implode(',', array_filter($_POST['genre']));
Why so?
$genre = join(',', array_filter($_POST['genre'], function($sItem)
{
//here I assume your 'not empty' matches PHP empty() function
//if not, then add desired conditions
return !empty($sItem);
}));
$genretotal = $_POST['genre'];
if(isset($genretotal) && count($genretotal)>0)
{//This check array is null or not
$gen_arr = implode(",",$genretotal);
}//end if
echo $gen_arr;
//This is the code you avoid empty values
I have names in the form of Lastname, Firstname. In my database I have a different field for both the first and last.
I would like to use PHP to read everything before the comma as the lastname and everything after the comma as the firstname. What is the best way to accomplish this?
list($Lastname,$Firstname) = explode(",",$Name);
<?php
$names = explode( "," , $allNames);
// $names[0] and names[1] are first and last names
?>
with the explode function.
<?php
list($firstname, $lastname) = explode(',','Lastname, Firstname',2);
echo $firstname.' '.$lastname;
?>
If you'll use list();
while( list($fname,$lname) = explode(", ", $db->fetch() ) ) {
echo $lname . " " . $fname . "<br />";
}
Without list() and assining an array;
$name = explode( ", ", $db->fetch()->nameField );
// may be you want to do something with that array
// do something
// echoing
foreach( $name as $fname=>$lname ) {
echo $lname . " " . $fname . "<br />"
}
As nobody has mentioned it yet, to expressly meet the question requirements, you'll need to use the third parameter to explode()
list($lastname, $firstname) = explode(',', $name, 2);