b3ck
b3ck

Reputation: 97

php mysql multiple if statements

So I have this result array for which I need to run an IF statement and then display data depending on if it says yes or no, I got that part down, now I need to add another IF statement inside of the other one.. here is an example of what I am trying to do, I know it's wrong so please help.

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 "";
        }               
    }
}

Here is my 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) "" 
    } 
}

Here is all of my PHP code with "Anonymous Man's" answer included:

<?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
                        }            
                }
        }

}
?>

Upvotes: 0

Views: 147

Answers (2)

Anonymous Man
Anonymous Man

Reputation: 3056

Your table is not properly normalized. You really need two tables.

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 

You don't need any other data in children assuming all of those parts already exist in the other table. Now you can have from 1 to a zillion child parts in a kit without bloating the parts table.

Now your code would be more like this:

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
        }             
    }
}

This should point you in the right direction. You might need to make some tweaks to get what you want. If you wanted to output more detail about the child parts you could run a more complex query and get the part name etc out of the parts table by joining on partid. This will likely solve your current problem, but more importantly, it will give you a lot more future flexibility with a normalized design.

Upvotes: 0

Arian Faurtosh
Arian Faurtosh

Reputation: 18491

I did find something wrong...

Attribute "bordercolor" is not allowed on element "table".... you need to use CSS.

Upvotes: 1

Related Questions