Most efficient way to get last initial? - php

I'm working on a custom forum. It will use an email and password to log in, and the forum will display your first name and last initial. The database stores their first name and full last name.
What would be the easiest way to make it abbreviate the last name to an initial?
It currently uses this to display full name:
$name = $row['user_firstname'] . " " . $row['user_lastname'];
Thanks!

$name = $row['user_firstname'] . " " . $row['user_lastname'][0];
Just add [0] to the end to get the first character.

And to make it prettier:
$name = ucfirst(strtolower($row['user_firstname'] )). " " . ucfirst($row['user_lastname'][0]);
so MYFIRSTNAMe lastName still shows as MyfirstnameL

Related

PHP ECHO table with dynamic dropdown as one column using FOR EACH

I am a bit stuck I am attempting to build out a table with HTML and PHP. Ok got that done. Now I want to add a dynamic dropdown as one of the column options but cant seem to figure out how to mix the PHP, the HTML and the needed FOR EACH loop.
I know its currently wrong but am posting a variable of what I have been trying below.
echo '<td>' . "<select>". "<option value =" . $sf_name . ' '. $sl_name . ">" . $sf_name . ' '. $sl_name . "</option>" .
foreach($Staff_On_Duty as $person){
"<option value =" . $sf_name_option=$person->Staff_First_Name . ' '. $sl_name_option=$person->Staff_Last_Name . ">" . $sf_name_option . ' '. $sl_name_option . "</option>"
}
. "</select>" .'</td>';
I need to have the currently selected individual at the top of the dropdown with the option to change that person out. the first .sf_name comes from higher in the code and gives me the name of the individual currently selected. The foreach runs from the $Staff_On_Duty query to give me everyone working right now. What is the right way to do this?
This is how I would do it, essentially only fill the dropdown and mark the option that should have been selected (other stuff is a matter of preference):
echo("<td><select>");
foreach($Staff_On_Duty as $person){
$sf_name_option=$person->Staff_First_Name; // separated for clarity, you could also use $person->Staff_First_Name everywhere
$sl_name_option=$person->Staff_Last_Name;
echo("<option value = $sf_name_option $sl_name_option");
if (($sf_name_option == $sf_name) && ($sl_name_option == $sl_name)) echo (" selected"); // this is the relevant part, but make sure variable values match!
echo(">$sf_name_option $sl_name_option</option>");
}
echo("</select></td>");
Note that I did not put any quotation marks cause I have no idea what your variables entail, but it would need them if you don't have them in yet, i.e.:
... value=\"$sf_name_option $sl_name_option\" ...

Is it possible to reverse a concatenated/? string. making it into a single (changed) variable

I split this string ($messageText) using a formula like this:
list($msgat, $message) = explode("!", "$messageText", 2);
selecting a row from my database:
with this:
$sql = "SELECT verse FROM ".$datatable." WHERE book LIKE '%".$message."'";
I need to come up with a way to make this: (which works, on a page echoing)
echo " " . $row["book"]. " " . $row["verse"]. "<br>";
something back into a single string: something like this I've been trying.
$answer = " . $row["verse"]. "
thus, I would be able to use the original but changed, variable elsewhere.
I have tried various methods, trying to concoct a solution...
$answer = $row["verse"] ;
etc etc... I am stumped... it is probably right before me I just can't find it.
$answer = " " . $row["book"]. " " . $row["verse"]; //this will store value into single variable answer
echo $answer; // use this if you want to display the result on the page.
this will definitely work if not show us the error so that we can resolve it.

QRcode after scan redirect to custom link

I'm working with QRcode at the moment so far it's so good i can scan it and get a result back from the database.
Im getting the data as follow :
$qrcode = QRcode::png('
number:' . $number. '
Name: ' . $name. '
Lastdate : ' . $lastdate . '
check: ' . $check. '');
My question is how can i scan the QRcode and redirect it to an custom link and display it on the website
Thanks,
Kevin
Recently i have made one application like your requirement only. what you need to do is, what ever data displaying through QR code that data insert into database then you will get one record id and using that record id you can make custom URL with creating new php page.
your code:
$qrcode = QRcode::png('
number:' . $number. '
Name: ' . $name. '
Lastdate : ' . $lastdate . '
check: ' . $check. '');
my suggested code:
array('number' => $number,
'Name '=>$name,
'Lastdate '=> $lastdate,
'check '=> $check);
Insert this array into your database
grab inserted recored id.
$custome_URL = "www.yourwebsitename.com/qrcode_scanneddata.php?id=$record_id";
$qrcode = QRcode::png("$custome_URL");
when ever you scan the URL automatically it will open in web browser.
You can redirect by code like this
header('Location: '.$qrcode->url);

Issues looping through an array

So, I'm using a database to store ship data.
As I load data into the database, I am checking whether the ship already exists. Sometimes, more than one ship has the same name. This code tries to go through the array, pull out all the ships with the same name, and then ask, in turn if that is the right one- if not, then it's yet another with the same name.
$sql = "SELECT Ship_Primary_Key, ship_original_rate, Ship_Launch_Year from Ships WHERE Ship_Name = '" . $shipname . "'";
$result = $conn->query ($sql);
if ($result-> num_rows > 0) //Does the ship name already exist in the DB? {
$ships_in_db = mysqli_fetch_all ($result,MYSQLI_ASSOC);
foreach ($ships_in_db as $row) {
echo "new record is " . $shipname . " as a " . $shiprate . ". Records already include " . $shipname . ", launched " . $row["Ship_Launch_Year"] . " as a " . $row ['ship_original_ra
$yesno = trim(fread(STDIN,5));
if ($yesno == "y" || $yesno == "yes") {
//ship already exists in the DB. Get the Key
echo $shipname . " is not new to the database and the key is " . $row["Ship_Primary_Key"] . "\n";
$shipkey = $row["Ship_Primary_Key"];
break 1;
}
}
//if you get through the loop of ships without assigning a primary key, ship is new
if (empty($shipkey)) {
$shipkey = write_ship_to_DB($shipname,$shiprate,$launchyear,$launchname,$conn);
}
}
So the problem is, I know that I have at least three ships with the same name in the first set of data (that are different). The problem is, it only ever asks about the first one. When I put 'n', it just goes on, and never asks about the second ship with the same name that already exists.
I think it's a problem with the Foreach loop and the break statement.
I'd appreciate any help with this
I've figured out the problem.
Because of the loop- I wasn't resetting the "Ship_Id" variable, which meant that the second or nth time a ship with the same name came around, a new ship wasn't created.
Now I've solved that. So the problem isn't this code at all- it's other code.

Cannot selecting first row on database php pdo mysql

I couldn't understand what is wrong here. I cannot take the first row on database. I also cannot the data which are single according to $ara parameter. Here are the codes:
$ara =$_POST['ara'];
$query=$db->prepare("SELECT * FROM uyeler WHERE
name like '%".$ara."%' or
surname like '%".$ara."%' or
email like '%".$ara."%'
");
$query->execute(array());
if ($num_row = $query->fetchAll(PDO::FETCH_BOTH)){
echo "<li><a href='profil.php?id=".$num_row['user_id']."'>" .$num_row['name']. " " .$num_row['surname']."</a></li>";
}else{
echo "Hiç birşey bulunamadı!";
}
You need to correct your array call:
from
$num_row['user_id']
To
$num_row[0]['user_id']
Your code is tested and works, nice job.
what you need to correct is the following line:
echo "<li><a href='profil.php?id=" . $num_row[0]['user_id'] . "'>" . $num_row[0]['name'] . " " . $num_row[0]['surname'] . "</a></li>";
If you face such problem in the future, it is a good practice to var_dump the results like:
var_dump($num_row);
This will give you a total raw picture of the content.

Categories