Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

php - Display "First Name" Data Based On First Letter

I am attempting to build a sign-in sheet that allows users to select the first letter of their first name, it would then grab all the users from the database and output them in a selectable list that then allows the user to choose their name and then inputs that they 'signed-in' into the database with a timestamp of when they clicked on their name.

Right now I'm stuck on the best way to have PHP go into the table, and do a look up on all the "First Names" that start with the letter that is chosen, and output the First_Name and Last_Name.

Example: I select "P" cause my name is "Pietro Aretino" it then gives me the output of every member whose First_Name starts with "P" and includes their last name as well. I find my name in the list "Pietro Aretino", I select that, it then sends stores my sign-in information in the database as well, which I suppose would be, my First/Last Name, and the timestamp.

But, I digress, for right now, I'm not sure how to have PHP do that lookup by First Name and then output the list

Right now I have every button A-Z labeled in military-alphabet (alpha, bravo etc.), when "A" is selected it runs the "alpha" function down below. Trying to figure out what to put into the "alpha" "bravo" "charlie" etc. functions that will make PHP go to the database and do a lookup and output the lists.

...here is what I have so far:

<?php
$servername = "localhost";
$username = "*********";
$password = "*********";
$dbname = "********** ";

//Create Connection
global $CONNECTION;
$CONNECTION = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($CONNECTION->connect_error) {
    die("Connection To MySQL Database Failed With the Following Error: " . $connection->connect_error);
}
global $SQL; 
$SQL = "SELECT first_name, last_name FROM ppSD_member_data";
global $RESULT; 
$RESULT = $CONNECTION->query($SQL);
if (isset($_POST['alpha'])) {
    alpha();
}
if (isset($_POST['bravo'])) {
    bravo();
}
if (isset($_POST['charlie'])) {
    charlie();
}
if (isset($_POST['delta'])) {
    delta();
}
if (isset($_POST['eko'])) {
    eko();
}
if (isset($_POST['foxtrot'])) {
    foxtrot();
}
if (isset($_POST['golf'])) {
    golf();
}
if (isset($_POST['hotel'])) {
    hotel();
}
if (isset($_POST['india'])) {
    india();
}
if (isset($_POST['juliet'])) {
    juliet();
}
if (isset($_POST['kilo'])) {
    kilo();
}
if (isset($_POST['lima'])) {
    lima();
}
if (isset($_POST['mike'])) {
    mike();
}
if (isset($_POST['november'])) {
    november();
}
if (isset($_POST['oscar'])) {
    oscar();
}
if (isset($_POST['papa'])) {
    papa();
}
if (isset($_POST['quebec'])) {
    quebec();
}
if (isset($_POST['romeo'])) {
    romeo();
}
if (isset($_POST['sierra'])) {
    sierra();
}
if (isset($_POST['tango'])) {
    tango();
}
if (isset($_POST['uniform'])) {
    uniform();
}
if (isset($_POST['victor'])) {
    victor();
}
if (isset($_POST['whiskey'])) {
    whiskey();
}
if (isset($_POST['xray'])) {
    xray();
}
if (isset($_POST['yankee'])) {
    yankee();
}
if (isset($_POST['zulu'])) {
    zulu();
}
echo <<<_END
<html>
    <head>
    <title>The Yard Members</title>
    </head>
<body>
<div align="center">
<h3>Please Select The Letter of Your First Name</h3>
<form method="post" action="display_members.php">
<input type="submit" name="alpha" id="name" value="A" style="height:75px; width:75px" />
<input type="submit" name="bravo" id="name" value="B" style="height:75px; width:75px" />
<input type="submit" name="charlie" id="name" value="C" style="height:75px; width:75px" />
<input type="submit" name="name" id="name" value="D" style="height:75px; width:75px" />
<br>
<input type="submit" value="E" style="height:75px; width:75px" />
<input type="submit" value="F" style="height:75px; width:75px" />
<input type="submit" value="G" style="height:75px; width:75px" />
<input type="submit" value="H" style="height:75px; width:75px" />
<br>
<input type="submit" value="I" style="height:75px; width:75px" />
<input type="submit" value="J" style="height:75px; width:75px" />
<input type="submit" value="K" style="height:75px; width:75px" />
<input type="submit" value="L" style="height:75px; width:75px" />
<br>
<input type="submit" value="M" style="height:75px; width:75px" />
<input type="submit" value="N" style="height:75px; width:75px" />
<input type="submit" value="O" style="height:75px; width:75px" />
<input type="submit" value="P" style="height:75px; width:75px" />
<br>
<input type="submit" value="Q" style="height:75px; width:75px" />
<input type="submit" value="R" style="height:75px; width:75px" />
<input type="submit" value="S" style="height:75px; width:75px" />
<input type="submit" value="T" style="height:75px; width:75px" />
<br>
<input type="submit" value="U" style="height:75px; width:75px" />
<input type="submit" value="V" style="height:75px; width:75px" />
<input type="submit" value="W" style="height:75px; width:75px" />
<input type="submit" value="X" style="height:75px; width:75px" />
<br>
<input type="submit" value="Y" style="height:75px; width:75px" />
<input type="submit" value="Z" style="height:75px; width:75px" />
</form>
</body>
</div>
</html>
_END;
function alpha() {
#while($row = $RESULT->fetch_assoc()) {
    echo "This Is The Alpha Function";
    }
function bravo() {
    echo "This is the Bravo Function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
function charlie() {
    echo "This is the charlie function";
}
#}
/*if(array_key_exists('name', $_POST)){
    search();
}
function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
if ($result->num_rows > 0) {
//Output data of each row
    while($row = $result->fetch_assoc()) {
    echo "Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
    }
} else {
    echo "No Members";
}
*/
$CONNECTION->close();
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As people are saying the code is a bit ridiculous. I'm just going to share this in the hopes of teaching you something. Not really looking to answer your question. In fact I'd suggest just deleting the question as I'm not sure how much utility it offers others. And "How to do a wildcard SQL search" is obviously a dupe question.

Anyway... Could be condensed down to something like:

//example.com?search_letter=A

$pattern = isset($_REQUEST('search_letter')) ? $_REQUEST('search_letter') : null;

if (empty($pattern)) {
    die('missing search paramter');
}

$pattern = $pattern . '%';
$sql = 'SELECT * FROM users WHERE first_name LIKE ?';

$sth->execute([$pattern]);
$results = $sth->fetchAll();

What changes? What is the variable? A letter. Does how you render the output for A differ from how you will interpret and output B? No. So keep it DRY (Don't repeat yourself).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...