如果需要,我试图显示多个图像。
我不是要求勺子喂食,而是要朝着正确的方向前进。
我当前的echo
打印数据库列:
<table style="margin:0 5%;border-left: 1px solid #000;" id="monsterstable" cellpadding="4" cellspacing="0" width="90%"><thead> <tr>
<th class="tabletop" width="150">NPC Picture</th>
<th class="tabletop">NPC Name </th>
<th class="tabletop">NPC Drops</th>
</tr></thead>
<?php
$query = $_GET['query'];
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM npcs
WHERE (`npc_name` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo '<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>';
echo "<br>Showing results for <b>'".$query."'</b><br>";
echo '<tr><td class="tablebottom"><img src="'.$results['image'].'" width="100px" height="100px""></td><td class="tablebottom">'.$results['npc_name'].'</td>
<td class="tablebottom"><img src="'.$results['location'].'" width="20px" height="20px"></td></tr>';
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo 'No results<br><form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>';
}
}
else{ // if query length is less than minimum
echo '<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form><br><br>Minimum length is '.$min_length.'<br>';
}
?>
</table>
我的SQL:
http://i.stack.imgur.com/XEquI.png
如何添加多个图像并使其回显?
答案 0 :(得分:0)
您想要遍历MySQL行并回显每个img
标记。
foreach($results as $result){
echo "<img src='$results->image' width='20px' height='20px'>";
}
调整您已经循环浏览结果的方式,无论您是否正在使用PDO或Mysqli或诸如此类的东西。这假设您获得了一个名为$results
的结果数组,其中每个元素都包含一个行对象,其中键image
包含您的图片网址。
编辑:
那么,如果我正确地解释您的数据结构,那么就是这样:
echo '<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>';
echo "<br>Showing results for <b>'".$query."'</b><br>";
while($results = mysql_fetch_array($raw_results)){
echo '<tr>
<td class="tablebottom">
<img src="'.$results['image'].'" width="100px" height="100px">
</td>
<td class="tablebottom">'.$results['npc_name'].'</td>
<td class="tablebottom">
<img src="'.$results['location'].'" width="20px" height="20px">
</td>';
}
echo '</tr>';
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
尽管如其他人所述,mysql
已在PHP中弃用,但应使用mysqli
或PDO
。