有没有更好的方法来编写下面的函数,以获得更快的响应



我使用mySQL作为主数据库,并从中查询以下代码:

<?php
if (isset($_GET['ID'])) {
$config = parse_ini_file("cfg.ini");
$con = mysqli_connect($config['host'],$config['user'],$config['password'],$config['dbname']) or die ("Bad Connect: ".mysqli_connect_error());
$ID = mysqli_real_escape_string($con, $_GET['ID']);
$sql = "SELECT * FROM rec_table WHERE rec_ID='$ID'";
$result = mysqli_query($con, $sql);
}else {
header('Location: 2-page.php');
}
?>
<?php
while ($row = mysqli_fetch_assoc($result)) {
//    echo("<title>Search - $row[item_name]</title>");
foreach ($result as $r) {
echo("<table>");
echo("<tbody>");
echo("<ul>");
echo("<li><img src='/SearchEngine/Test/$r[icon_name].png' align='center' class='border'>&nbsp &nbsp  1 x $r[rec_Name]</li>");
echo("<li><img src='/SearchEngine/Test/$r[ID1_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY1] x $r[ITEM1]</li>");
if(empty($r['ITEM2'])) {
//show nothing
} else {      
echo("<li><img src='/SearchEngine/Test/$r[ID2_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY2] x $r[ITEM2]</li>");
}
if(empty($r['ITEM3'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID3_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY3] x $r[ITEM3]</li>");
}
if(empty($r['ITEM4'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID4_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY4] x $r[ITEM4]</li>");
}
if(empty($r['ITEM5'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID5_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY5] x $r[ITEM5]</li>");
}
if(empty($r['ITEM6'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID6_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY6] x $r[ITEM6]</li>");
}
if(empty($r['ITEM7'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID7_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY7] x $r[ITEM7]</li>");
}
if(empty($r['ITEM8'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID9_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY8] x $r[ITEM8]</li>");
}
if(empty($r['ITEM9'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID10_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY9] x $r[ITEM9]</li>");
}
if(empty($r['ITEM9'])) {
//show nothing
} else {
echo("<li><img src='/SearchEngine/Test/$r[ID10_icon].png' align='center' class='border'>&nbsp &nbsp  $r[QTY10] x $r[ITEM10]</li>");
}
echo("</ul>");
}
echo("</tbody>");
echo("</table>");
}  
?>

我的代码创建了一个列表,它将在其中填充搜索到的ID的图标和名称。问题是,是否有其他更好的方法来检查ITEMX是否为空,并且不在列表中显示图标或空字符串?

谢谢!

完全未经测试,但您可能会尝试这样的方法。一个接受两个参数并返回格式化HTML字符串的简单函数可以在循环中调用。

<?php

if( empty( $_GET['ID'] ) )exit( header('Location: 2-page.php') );
else{
function displayicon( $r=array(), $i=false ){
if( !empty( $r ) && array_key_exists( sprintf('ITEM%d', $i), $r ) && array_key_exists( sprintf('QTY%d', $i), $r ) ){
return sprintf(
'<li><img src="/SearchEngine/Test/%s.png" align="center" alt="" class="border" />%s x %s</li>',
$r[ sprintf('ID%d_icon',$i) ],
$r[ sprintf('QTY%d',$i) ],
$r[ sprintf('ITEM%d',$i) ]
);
}
return false;
}


$id=filter_input( INPUT_GET, 'ID', FILTER_SANITIZE_NUMBER_INT );
$obj = (object)parse_ini_file('cfg.ini');
$con = new mysqli( $obj->host, $obj->user, $obj->password, $obj->dbname );

$sql="select * from rec_table where rec_id=?";
$stmt=$con->prepare( $sql );
$stmt->bind_param( 's', $id );
$stmt->execute();
$result=$stmt->get_result();

if( $result ){
echo "
<table>
<tbody>
<tr>
<td>
<ul>
<li><img src='/SearchEngine/Test/$r[icon_name].png' align='center' class='border'>&nbsp &nbsp  1 x {[$r['rec_Name']}</li>"; 

while( $row = $result->fetch_assoc() ) {
foreach( $row as $r ){
for( $i=1; $i <= 9; $i++ ){
$li=displayicon( $i );
if( !empty( $li ) )echo $li;
}
}
}

echo "
</ul>
</td>
</tr>
</tbody>
</table>";          
}
}
?>

相关内容

  • 没有找到相关文章