我使用phpLiteAdmin创建了一个数据库,它有很多条目,但它们都是文本或数字。
我想知道我是否可以在此数据库中添加图像部分,或者我该怎么做。我认为我可以做到的另一种方式是使用href标签并将其移动到位。必须有更好的方法来做到这一点。
这是我的php和html的代码。
<head>
<link type="text/css" rel="stylesheet" href="Car_Style.css"/>
</head>
<body>
<header id="header" class="loading" style="opacity: 1";]>
<!-- Logo -->
<div id="logo"></div>
<!-- Nav -->
<nav id="nav">
<ul>
<li><a href="#intro">header</a></li>
<li><a href="#one">Header</a></li>
<li><a href="#two">header</a></li>
<li><a href="#work">My Header</a></li>
<li><a href="#contact">Header</a></li>
</ul>
</nav>
</header>
<!-- This is where all the images go from each of the entries -->
</body>
<?php
try {
# Connect to SQLite database
$dbh = new PDO("sqlite:Car_Sales_Network");
# Prepare SQL statement
$sth = $dbh->prepare('SELECT * FROM Cars_On_Network' );
# Set the Fetch Mode to Associative Array
$sth->setFetchMode(PDO::FETCH_ASSOC);
# Run the query on the database
$sth->execute();
//table printout
echo "<table>";
# Loop through returned records
while($row = $sth->fetch()) {
//print_r($row);spot
//print_r($row);
echo "<table>";
echo "<th>Car Make:</th>";
echo "<th>Car Model:</th>";
echo "<th>Car Badge:</th>";
echo "<th>Car Price:</th>";
echo "<th>Car Transmission:</th>";
echo "<th>Car P Plate Legality:</th>";
echo "<div id='1'>";
echo "<tr>";
echo "<td>". $row["car_make"] . "</td>";
echo "<td>". $row["car_model"] . "</td>";
echo "<td>". $row["car_badge"] . "</td>";
echo "<td>". $row["price"] . "</td>";
echo "<td>". $row["trans"] . "</td>";
echo "<td>". $row["P_Plate_Legal"] . "</td>";
echo "</tr>";
echo "</div>";
//echo $row["Game_ID"];
echo "<br>";
}
echo "</table>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
这是我的CSS。
* {
font-family: Arial;
color: black;
}
table {
padding: 20px;
}
th td {
padding-right: 20px;
padding-left: 20px;
padding-top: 20px;
padding-bottom: 20px;
}
/*********************************************************************************/
/* Header */
/*********************************************************************************/
#header ul li {
color:white;
text-decoration: underline;
}
#header {
position: fixed;
z-index: 10000;
left: 0;
top: 0;
width: 100%;
background: #000000;
background: rgba(0, 0, 0, 0.95);
height: 3em;
line-height: 3em;
box-shadow: 0 0 0.15em 0 rgba(0,0,0,0.1);
}
body {
padding-top: 3em;
}
#logo {
position: absolute;
left: 1em;
top: 0;
height: 3em;
line-height: 3em;
letter-spacing: -1px;
}
#logo a {
font-size: 1.25em;
}
#nav {
position: absolute;
right: 0.5em;
top: 0;
height: 3em;
line-height: 3em;
}
#nav ul {
margin: 0;
}
#nav ul li {
display: inline-block;
margin-left: 0.5em;
font-size: 0.9em;
}
#nav ul li a {
display: block;
color: inherit;
text-decoration: none;
height: 3em;
line-height: 3em;
padding: 0 0.5em 0 0.5em;
outline: 0;
}
显然它没有完成,但我怎么能让每辆车都有一张照片。
干杯。
答案 0 :(得分:0)
首先,您必须找到存储图像的方法。
我们通常不会将图像的二进制数据存储在数据库中。相反,我们将图像文件存储在服务器(或第三方CDN)的某个位置。
然后我们在数据库中创建一个新的varchar(255)
并存储这些图像的网址(例如http://foobar.com/some/image.jpg
)。如需解释,请说我们会在上表中添加一列car_image_url
。
让我们稍微更改PHP代码以添加显示列:
<link type="text/css" rel="stylesheet" href="Car_Style.css"/>
</head>
<body>
<header id="header" class="loading" style="opacity: 1";]>
<!-- Logo -->
<div id="logo">
</div>
<!-- Nav -->
<nav id="nav">
<ul>
<li><a href="#intro">header</a></li>
<li><a href="#one">Header</a></li>
<li><a href="#two">header</a></li>
<li><a href="#work">My Header</a></li>
<li><a href="#contact">Header</a></li>
</ul>
</nav>
</header>
<!-- This is where all the images go from each of the entries -->
</body>
<?php
try {
# Connect to SQLite database
$dbh = new PDO("sqlite:Car_Sales_Network");
# Prepare SQL statement
$sth = $dbh->prepare('SELECT * FROM Cars_On_Network' );
# Set the Fetch Mode to Associative Array
$sth->setFetchMode(PDO::FETCH_ASSOC);
# Run the query on the database
$sth->execute();
//table printout
echo "<table>";
# Loop through returned records
while($row = $sth->fetch()) {
//print_r($row);spot
//print_r($row);
/* added these line */
# If the image field is empty, change to dummy image
if (empty($row['car_image_url'])) {
$row['car_image_url'] = 'http://yourdomain.com/some/dummy.jpg';
}
/* // added these line */
echo "<table>";
echo "<th>Car Make:</th>";
/* added this line */
echo "<th>Car Image:</th>";
/* // added this line */
echo "<th>Car Model:</th>";
echo "<th>Car Badge:</th>";
echo "<th>Car Price:</th>";
echo "<th>Car Transmission:</th>";
echo "<th>Car P Plate Legality:</th>";
echo "<div id='1'>";
echo "<tr>";
echo "<td>". $row["car_make"] . "</td>";
/* added this line */
echo "<td><img src=\"". $row["car_image_url"] . "\" /></td>";
/* // added this line */
echo "<td>". $row["car_model"] . "</td>";
echo "<td>". $row["car_badge"] . "</td>";
echo "<td>". $row["price"] . "</td>";
echo "<td>". $row["trans"] . "</td>";
echo "<td>". $row["P_Plate_Legal"] . "</td>";
echo "</tr>";
echo "</div>";
//echo $row["Game_ID"];
echo "<br>";
}
echo "</table>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
随心所欲地设置CSS样式。