显示“名字"基于首字母的数据 [英] Display "First Name" Data Based On First Letter

查看:96
本文介绍了显示“名字"基于首字母的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个允许用户选择其名字的第一个字母的登录表,然后它将从数据库中获取所有用户并将其输出到可选择列表中,然后允许用户选择他们的名字,然后输入他们登录"到数据库的时间,并带有他们点击他们名字的时间戳.

现在,我一直停留在使PHP进入表中的最佳方法上,并查找所有以所选字母开头的名字",并输出First_Name和Last_Name. /p>

示例:我选择"P",因为我的名字是"Pietro Aretino",然后它给我其First_Name以"P"开头并包括其姓氏的每个成员的输出.我在"Pietro Aretino"列表中找到了我的名字,选择了它,然后它还将我的登录信息也存储在数据库中,我的名字/姓氏和时间戳也将存储在该数据库中.

但是,我离题了,目前,我不确定如何让PHP按名字进行查找,然后输出列表

现在我有标记在军事字母表(α,喝彩等)中,当A"被选择它运行阿尔法"功能下面向下每个按钮A-Z.试图弄清楚将什么放入"alpha","bravo","charlie"等函数中,这些函数将使PHP进入数据库并进行查找并输出列表.

...这是我到目前为止所拥有的:

<?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();
?>

解决方案

人们说的代码有点荒谬.我只是想分享一下,希望能教给您一些东西.并不是真的想回答您的问题.实际上,我建议只是删除问题,因为我不确定它可以为其他人提供多少实用程序.而且如何进行通配符SQL搜索"显然是一个重复的问题.

无论如何...可能会浓缩为:

//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();

有什么变化?什么是变量?一封信.呈现A的输出的方式与解释和输出B的方式是否有所不同?不,所以请保持干燥(不要重复自己).

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();
?>

解决方案

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).

这篇关于显示“名字"基于首字母的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆