-2

Currently, in our office website, there is a userinput textbox and after inserting, results from database will be shown below. There are 4 results Lot ID, Product, EWSFLOW and Zone.Among them, only zone is different. I want to do that Lot ID, Product and EWSFlow must show at once and if that entered values have 5 different zones, Zone must shown Zone: 1,2,3,4,5. << First problem has been solved. And right now, I tried to add check boxes for each zone and checkbox must shown beside each zone. But currently, checkboxes are showing at the top. Also, count of the checkboxes must be same as Zones. lets say if the inserted value have 5 zones, it has to show 5 checkboxes besides of it (Example: Zone : [checkbox] 1). Checkboxes are showing at top

    echo "<table id='corwafer'>";
$arr = array();

while ($row = mysqli_fetch_assoc($result1)) {

$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;

if (!in_array($key, $arr)){
    echo "<tr>";
    echo "<th >Lot ID:</th>";
    echo "<td >$field1name</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<th>Product:</th>";
    echo "<td>$field2name</td>"; 
    echo "</tr>";

    echo "<tr>";
    echo "<th>EWSFLOW: </th>";
    echo "<td>$field3name</td>"; 
    echo "</tr>";

    array_push($arr, $key);
}

echo "<tr>";
echo "<th>Zone:</th>";
echo "<input type='checkbox' name='chkzone' value='chkzone'>";
echo "<td>$field4name</td>"; 
echo "</tr>";

}

echo "</table>";
AlexKhan
  • 1
  • 4
  • 1
    Don't `echo` your output inside your `while` loop but create the output into a variable that contains your desired output, then `echo` that variable after the loop – brombeer Aug 04 '22 at 06:36
  • 1
    Your query may be insecure/unstable because you are not using a prepared statement. This question relates to [the asker's earlier closed question](https://stackoverflow.com/q/73229953/2943403). – mickmackusa Aug 04 '22 at 06:50
  • [This answer](https://stackoverflow.com/a/41505038/2943403) even shows a sqlfiddle demontrating grouping to ensure unique data across multiple identifying columns – mickmackusa Aug 04 '22 at 07:06
  • @brombeer Why collecting data first? – Wiimm Aug 04 '22 at 07:23
  • @mickmackusa Can I edit this question and still can ask one more question? – AlexKhan Aug 04 '22 at 07:30
  • 1
    @Alex try not to change the desired result now that you have received answers. Changing things now will ruin the existing answers. You need to be very considered and precise about what you need when you first ask your question so that you are respectful of volunteer contributors. – mickmackusa Aug 04 '22 at 09:43

3 Answers3

0

You can change your query and use GROUP BY feature of MySQL. Below is the query. Ignore any spelling mistakes.

$sql = "SELECT lotid, product, ewsflow, GROUP_CONCAT(zone) FROM productdb.tbl_correlationwafer WHERE lotid = ? GROUP BY lotid, product, ewsflow ORDER BY lotid";

$pq = $mysqli->prepare($sql);
$pq->bind_param('i', $productlotid);
$pq->execute();
$result = $pq->get_result();

$data = $result->fetch_all();

GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.

GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".

AKT
  • 56
  • 5
0

You can define an array and put lotid, product and ewsflow into it as merged inside the loop. Then before echoing check if it's already used before :

$arr = array();

while ($row = mysqli_fetch_assoc($result1)) {

$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;

if (!in_array($key, $arr)){
    echo "<tr>";
    echo "<th >Lot ID:</th>";
    echo "<td >$field1name</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<th>Product:</th>";
    echo "<td>$field2name</td>"; 
    echo "</tr>";

    echo "<tr>";
    echo "<th>EWSFLOW: </th>";
    echo "<td>$field3name</td>"; 
    echo "</tr>";

    array_push($arr, $key);
}

echo "<tr>";
echo "<th>Zone:</th>";
echo "<td>$field4name</td>"; 
echo "</tr>";

}
Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
0

You can accomplish the desired output in a much simpler fashion if you were to use group_concat in the SQL query to gather together the various zone columns into a formatted value - then the PHP really needs only process a single row in the recordset and display the desired table format.

The SQL takes advantage of a prepared statement to help mitigate SQL injection - matters not that it is an internal website IMO - always better to be secure!

$sql='SELECT 
            `lotid`,
            `product`,
            `ewsflow`,
            group_concat( distinct `zone` order by `zone` asc separator ", " ) as `zone`
        FROM `productdb`.`tbl_correlationwafer` 
        WHERE `lotid` = ?
        ORDER BY `lotid`';
    
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s', $productlotid );
$stmt->execute();
$stmt->bind_result( $lotid, $product, $ewsflow, $zone );
$stmt->fetch();


printf('
    <table id="corwafer">
        <tr>
            <th>Lot ID:</th>
            <td>%1$s</td>
        </tr>
        <tr>
            <th>Product:</th>
            <td>%2$s</td>
        </tr>
        <tr>
            <th>EWSFLOW:</th>
            <td>%3$s</td>
        </tr>
        <tr>
            <th>Zone:</th>
            <td>%4$s</td>
        </tr>
    </table>',
    $lotid,
    $product,
    $ewsflow,
    $zone
);
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46