php mysql多个if语句

时间:2014-03-25 16:27:22

标签: php mysql arrays if-statement

所以我有这个结果数组,我需要运行一个IF语句然后显示数据,具体取决于它是否为yes,我得到了那个部分,现在我需要在另一个中添加另一个IF语句..这是我想要做的一个例子,我知道它错了所以请帮忙。

if (isset($result_array)) {
    foreach ($result_array as $result) {
        if($result['hasChildren']=="yes") { ?>
            <center>
                <table class='kitTable' width='98%' border='1' cellpadding='2' cellspacing='0' bordercolor='556587'>
                    <tr>
                        <td width='70%' class='kitHeading'><?php echo $result['kitHeading'];?></td>
                        <td width='15%' class='kitHeading'><?php echo $result['partHeading'];?></td>
                        <td width='15%' class='kitHeading'><?php echo $result['qtyHeading'];?></td>
                    </tr>
                    <tr>
                        <td><?php echo $result['kitPart1'];?></td>
                        <td class='trCenter'><?php echo $result['childPart1'];?></td>
                        <td class='trCenter'><?php echo $result['childPartQTY1'];?></td>
                    </tr>

                    <?php if($result['kitPart2']=== NULL) { ?>
                        <tr>
                            <td><?php echo $result['kitPart2'];?></td>
                            <td class='trCenter'><?php echo $result['childPart2'];?></td>
                            <td class='trCenter'><?php echo $result['childPartQTY2'];?></td>
                        </tr>
                    <?php } ?>
                </table>
            </center>
            <?php
        }
        elseif($result['hasChildren']=="no") {
            echo "";
        }               
    }
}

这是我的vardump

array(1) { 
    [0]=> array(84) { 
        [0]=> string(2) "29" 
        ["id"]=> string(2) "29" 
        [1]=> string(6) "4004" 
        ["function"]=> string(6) "4004" 
        [2]=> string(30) "Mazda Car Part 4004" 
        ["name"]=> string(30) "Mazda Car Part 4004" 
        [3]=> string(3) "Stock" 
        ["stocktype"]=> string(3) "Stock" 
        [4]=> string(30) "http://i.imgur.com/default.png" 
        ["image"]=> string(30) "http://i.imgur.com/default.png" 
        [5]=> string(22) "mazda" 
        ["vendor"]=> string(22) "mazda" 
        [6]=> string(101) "mazda car part" 
        ["notes"]=> string(101) "mazda car part" 
        [7]=> string(12) "mazda" 
        ["oemproj"]=> string(12) "mazda" 
        [8]=> string(3) "yes" 
        ["hasChildren"]=> string(3) "yes" 
        [9]=> string(9) "Child Part" 
        ["partHeading"]=> string(9) "Child Part" 
        [10]=> string(28) "Child Parts Included with VP" 
        ["kitHeading"]=> string(28) "Child Parts Included with VP" 
        [11]=> string(3) "QTY" 
        ["qtyHeading"]=> string(3) "QTY" 
        [12]=> string(26) "mazda child part 1" 
        ["kitPart1"]=> string(26) "mazda child part 1" 
        [13]=> string(5) "1001" 
        ["childPart1"]=> string(5) "1001" 
        [14]=> string(1) "3" 
        ["childPartQTY1"]=> string(1) "3" 
        [15]=> string(42) "mazda car part 2" 
        ["kitPart2"]=> string(42) "mazda car part 2" 
        [16]=> string(5) "2002" 
        ["childPart2"]=> string(5) "2002" 
        [17]=> string(1) "1" 
        ["childPartQTY2"]=> string(1) "1" 
        [18]=> string(38) "" 
        ["kitPart3"]=> string(38) "" 
        [19]=> string(6) "" 
        ["childPart3"]=> string(6) "" 
        [20]=> string(1) "" 
        ["childPartQTY3"]=> string(1) "" 
        [21]=> string(0) "" 
        ["kitPart4"]=> string(0) "" 
        [22]=> string(0) "" 
        ["childPart4"]=> string(0) "" 
        [23]=> string(0) "" 
        ["childPartQTY4"]=> string(0) "" 
        [24]=> string(0) "" 
        ["kitPart5"]=> string(0) "" 
        [25]=> string(0) "" 
        ["childPart5"]=> string(0) "" 
        [26]=> string(0) "" 
        ["childPartQTY5"]=> string(0) "" 
        [27]=> string(0) "" 
        ["kitPart6"]=> string(0) "" 
        [28]=> string(0) "" 
        ["childPart6"]=> string(0) "" 
        [29]=> string(0) "" 
        ["childPartQTY6"]=> string(0) "" 
        [30]=> string(0) "" 
        ["kitPart7"]=> string(0) "" 
        [31]=> string(0) "" 
        ["childPart7"]=> string(0) "" 
        [32]=> string(0) "" 
        ["childPartQTY7"]=> string(0) "" 
        [33]=> string(0) "" 
        ["kitPart8"]=> string(0) "" 
        [34]=> string(0) "" 
        ["childPart8"]=> string(0) "" 
        [35]=> string(0) "" 
        ["childPartQTY8"]=> string(0) "" 
        [36]=> string(0) "" 
        ["kitPart9"]=> string(0) "" 
        [37]=> string(0) "" 
        ["childPart9"]=> string(0) "" 
        [38]=> string(0) "" 
        ["childPartQTY9"]=> string(0) "" 
        [39]=> string(0) "" 
        ["kitPart10"]=> string(0) "" 
        [40]=> string(0) "" 
        ["childPart10"]=> string(0) "" 
        [41]=> string(0) "" 
        ["childPartQTY10"]=> string(0) "" 
    } 
}

这是我的所有PHP代码与#34; Anonymous Man&#34;答案包括:

<?php

$dbhost = "localhost";
$dbname = "part_lookup_tool";
$dbuser = "dbuser";
$dbpass = "dbpass";


global $part_lookup_tool_db;

$part_lookup_tool_db = new mysqli();
$part_lookup_tool_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$part_lookup_tool_db->set_charset("utf8");


if ($part_lookup_tool_db->connect_errno) {
    printf("Connect failed: %s\n", $part_lookup_tool_db->connect_error);
    exit();
}

error_reporting(E_ALL);
ini_set('display_errors', 1);

$html = '';
$html .= '<li class="result">';
$html .= '<h1 class="btn2"><center> oemprojString</center></h1>';
$html .= '<h2><b style="background:#e4ee40; color:#000;">&nbsp; Part#: partidString &nbsp;</b></h2>';
$html .= '<h4><b>Stock Item Description:</b> partdescString</h4>';
$html .= '<h4><b>Stock Type:</b> stocktypeString</h4>';
$html .= '<h4><b>Vendor:</b> vendorString</h4>';
$html .= '</br>';
$html .= '<a target="_blank" href="imageString">';
$html .= '<center><img class="part" src="imageString" width="50%" height="50%"></center></a>';
$html .= '<h6>CLICK IMAGE TO ENLARGE</h6>';
$html .= '</br>';
$html .= '<h4><b>Notes:</b> notesString</h4>';
$html .= '</li>';


$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $part_lookup_tool_db->real_escape_string($search_string);


if (strlen($search_string) >= 3 && $search_string !== ' ') {
        $query = 'SELECT * FROM parts WHERE partid LIKE "%'.$search_string.'%" OR partdesc LIKE "%'.$search_string.'%" OR stocktype LIKE "%'.$search_string.'%" OR vendor LIKE "%'.$search_string.'%" OR notes LIKE "%'.$search_string.'%" OR oemproj LIKE "%'.$search_string.'%"';


        $result = $part_lookup_tool_db->query($query);
        while($results = $result->fetch_array()) {
                $result_array[] = $results;
        }

        if (isset($result_array)) {
                foreach ($result_array as $result) {


$display_partid = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['partid']);
$display_partdesc = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['partdesc']);
$display_image = preg_replace("/".$search_string."/i", "".$search_string."", $result['image']);
$display_stocktype = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['stocktype']);
$display_vendor = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['vendor']);
$display_notes = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['notes']);
$display_oemproj = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['oemproj']);


                        $output = str_replace('partdescString', $display_partdesc, $html);
                        $output = str_replace('partidString', $display_partid, $output);
                        $output = str_replace('imageString', $display_image, $output);
                        $output = str_replace('stocktypeString', $display_stocktype, $output);
                        $output = str_replace('vendorString', $display_vendor, $output);
                        $output = str_replace('notesString', $display_notes, $output);
                        $output = str_replace('oemprojString', $display_oemproj, $output);

                        echo($output);
                }
        }else{

                // ** I would rather just show that there were no results found overall **
                $output = str_replace('partdescString', '<font color="red">No Data Found.</font>', $html);
                $output = str_replace('partidString', '<font color="red">No Data Found.</font>', $output);
                $output = str_replace('imageString', 'http://i.imgur.com/default', $output);
                $output = str_replace('stocktypeString', '<font color="red">No Data Found.</font>', $output);
                $output = str_replace('vendorString', '<font color="red">No Data Found.</font>', $output);
                $output = str_replace('notesString', '<font color="red">No Data Found.</font>', $output);
                $output = str_replace('oemprojString', '<font color="red">No Data Found.</font>', $output);

                echo($output);
        }

        if (isset($result_array)) {
                foreach ($result_array as $result) {
                        $id = $result['id'];
                        //now query the children table for child parts
                        $stmt = $part_lookup_tool_db->prepare("SELECT * FROM `children` WHERE `parentpartid` = :partid")
                        $stmt->bindParam(':partid', $id, PDO::PARAM_INT);
                        $stmt->execute;
                        if($children = $stmt->fetchAll() { ?>
                                <center>
                                        <table class='kitTable' width='98%' border='1' cellpadding='2' cellspacing='0'>
                                                <tr>
                                                        <td width='70%' class='kitHeading'><?php echo $result['kitHeading'];?></td>
                                                        <td width='15%' class='kitHeading'><?php echo $result['partHeading'];?></td>
                                                        <td width='15%' class='kitHeading'><?php echo $result['qtyHeading'];?></td>
                                                </tr>
                                                <?php
                                                $i = 1;
                                                foreach ($children as $child) { ?>
                                                <tr>
                                                        <td>Kit part <?php echo $i ;?></td>
                                                        <td class='trCenter'><?php echo $child['partid'];?></td>
                                                        <td class='trCenter'><?php echo $child['quantity'];?></td>
                                                </tr>
                                                <?php
                                                        $i++;
                                                } ?>
                                        </table>
                                </center>
                                <?php
                        }            
                }
        }

}
?>

2 个答案:

答案 0 :(得分:1)

我确实发现了一些错误......

  

元素“table”上不允许使用属性“bordercolor”....你需要使用CSS。

答案 1 :(得分:0)

您的表格未正确规范化。你真的需要两张桌子。

table `Parts`

partid
function
name
stocktype
image
vendor
notes
oemproj

table `children`

partid (joined to partid in parts table)
parentpartid (this is the parts table partid of the kit)
quantity 

假设所有这些部分已存在于另一个表中,children中不需要任何其他数据。现在,您可以在套件中使用1到1亿个子零件,而不会使零件表膨胀。

现在你的代码更像是这样:

if (isset($result_array)) {
    foreach ($result_array as $result) {
        $id = $result['id'];
        //now query the children table for child parts (this assumes $con is your db connection
        $stmt = $con->prepare("SELECT * FROM `Children` WHERE `parentpartid` = :partid");
        $stmt->bindParam(':partid', $id, PDO::PARAM_INT);
        $stmt->execute;
        if($children = $stmt->fetchAll()) { ?>
            <center>
                <table class='kitTable' width='98%' border='1' cellpadding='2' cellspacing='0' bordercolor='556587'>
                    <tr>
                        <td width='70%' class='kitHeading'><?php echo $result['kitHeading'];?></td>
                        <td width='15%' class='kitHeading'><?php echo $result['partHeading'];?></td>
                        <td width='15%' class='kitHeading'><?php echo $result['qtyHeading'];?></td>
                    </tr>
                    <?php
                    $i = 1;
                    foreach ($children as $child) { ?>
                    <tr>
                        <td>Kit part <?php echo $i ;?></td>
                        <td class='trCenter'><?php echo $child['partid'];?></td>
                        <td class='trCenter'><?php echo $child['quantity'];?></td>
                    </tr>
                    <?php
                        $i++;
                    } ?>
                </table>
            </center>
            <?php
        }             
    }
}

这应该指向正确的方向。你可能需要做一些调整才能得到你想要的东西。如果要输出有关子部件的更多详细信息,可以运行更复杂的查询,并通过加入partid从部件表中获取部件名称等。这可能会解决您当前的问题,但更重要的是,它将通过标准化设计为您提供更多的未来灵活性。