所以我有一个包含员工姓名的数据库,当你搜索员工姓名时,会显示一个列表,其中我有一张Notes表,使用bootstrap"alert alert info"横幅作为注释,但我希望它这样做:如果数据库上调用Notes的表显示WARNING:在其中将横幅更改为"alert alert danger",有什么帮助吗?
这是我的Search.php 代码
<?php
mysql_connect("***", "***", "**") or die('Error : ' . mysql_error());
mysql_select_db("a7172755_search")or die("Could not find Database");
//Collect
$output = '';
if(isset($_POST['FirstName'])) { //Search box
$searchq = $_POST['FirstName']; //NOT VALID
$searchq = preg_replace("#[^0-9a-z]#i", "",$searchq);
$query = mysql_query("SELECT * FROM search WHERE FirstName LIKE '%$searchq%' OR LastName LIKE '%$searchq%' OR DateofBirth LIKE '%$searchq%' OR Address LIKE '%$searchq%' OR Occupation LIKE '%$searchq%' OR Warrents LIKE '%$searchq%' OR Record LIKE '%$searchq%' OR Email LIKE '%$searchq%' OR Phone LIKE '%$searchq%' OR Mobile LIKE '%$searchq%' OR Notes LIKE '%$searchq%' OR Photo LIKE '%$searchq%' OR OnlineName LIKE '%$searchq%' OR FaceBookName LIKE '%$searchq%' OR WarningNotes LIKE '%$searchq%'") or die("Could not search!"); //Gets the Table
$count = mysql_num_rows($query);
if($count == 0) {
$output = '<h3 class="alert alert-warning" style="text-align:center;">No one under that name could be found!</h3>';
}else{
while($row = mysql_fetch_array($query)) {
$fname = $row['FirstName'];
$lname = $row['LastName'];
$dob = $row['DateofBirth'];
$address = $row['Address'];
$occupation = $row['Occupation'];
$warrents = $row['Warrents'];
$record = $row['Record'];
$email = $row['Email'];
$phone = $row['Phone'];
$mobile = $row['Mobile'];
$notes = $row['Notes'];
$photo = $row['Photo'];
$onlinename = $row['OnlineName'];
$facebookname = $row['FaceBookName'];
//$id = $row['ID']; Not Needed
$output .= '<img src ="Photo/'.$photo.'.jpg" style="width:100px;height:100px;"><div style="margin-left: 300px;"><p>First Name: '.$fname.'<br>Last Name: '.$lname. '<br>Date of Birth: '.$dob.'<br>Address: '.$address.'<br>Occupation: '.$occupation.'<br>Warrents '.$warrents.'<br>Criminal Record: '.$record.'<br>Email Adress: '.$email.'<br>Phone: '.$phone.'<br>Mobile: '.$mobile.'<br>Online Name: '.$OnlineName.'<br>FaceBook Name/URL: '.$FaceBookName.'<div class="alert alert-info">Notes: '.$notes.'</div><br><button type="button" class="btn btn-primary">More >></button></p></div><hr>';
}
}
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<link rel="shortcut icon" href="img/favicon.ico">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/et-lineicon.css" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/ie-emulation-modes-warning.js"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<h2 class="form-signin-header" style="margin-left: 500px;" >Persons Search</h2>
<br><br><br><br><br><br><br>
<?php print("$output");?>
</div>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
也更改while循环:
while($row = mysql_fetch_array($query)) {
$fname = $row['FirstName'];
$lname = $row['LastName'];
$dob = $row['DateofBirth'];
$address = $row['Address'];
$occupation = $row['Occupation'];
$warrents = $row['Warrents'];
$record = $row['Record'];
$email = $row['Email'];
$phone = $row['Phone'];
$mobile = $row['Mobile'];
$notes = $row['Notes'];
$photo = $row['Photo'];
$onlinename = $row['OnlineName'];
$facebookname = $row['FaceBookName'];
//$id = $row['ID']; Not Needed
// Check the notes text and set the label for the div
if($notes == "WARNING") {
$label = "danger";
} else {
$label = "info";
}
$output .= '<img src ="Photo/'.$photo.'.jpg" style="width:100px;height:100px;"><div style="margin-left: 300px;"><p>First Name: '.$fname.'<br>Last Name: '.$lname. '<br>Date of Birth: '.$dob.'<br>Address: '.$address.'<br>Occupation: '.$occupation.'<br>Warrents '.$warrents.'<br>Criminal Record: '.$record.'<br>Email Adress: '.$email.'<br>Phone: '.$phone.'<br>Mobile: '.$mobile.'<br>Online Name: '.$OnlineName.'<br>FaceBook Name/URL: '.$FaceBookName.'<div class="alert alert-'.$label.'">Notes: '.$notes.'</div><br><button type="button" class="btn btn-primary">More >></button></p></div><hr>';
}
我们需要知道表的名称和字段。
例如:
<?php
$fetch_alert = mysql_fetch_object(mysql_query("SELECT `note_label`, `alert_text` FROM `notes` WHERE `id` = '1'"));
if($fetch_alert->note_label == "WARNING") {
echo"<div class=alert alert-danger"">".$fetch_alert->alert_text."</div>";
} else {
echo"<div class=alert alert-info"">".$fetch_alert->alert_text."</div>";
}
?>