Home > Net >  Want to show checkbox for each line
Want to show checkbox for each line

Time:08-05

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

CodePudding user response:

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

}

CodePudding user response:

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".

CodePudding user response:

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
);
  • Related