iam using php in developing our website which is our user has to check banana symptoms that he observe on his plantation,out of that checked symptoms our system will show the possible disease based on what he checked, but i have a problem, if i checked 1 and 4 checkbox it will show the same output, i want that if i checked that both checkbox it will give one output only this is my code: pls help me!! thank you. .

<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("banana",$con) or die("WALEY");
?> <?php
$sql = "select * from page";
$qury = mysql_query($sql);
while($row = mysql_fetch_array($qury))
if(isset($_GET["checkbox"])){
$f= $_GET["checkbox"];
$c = count($f);
echo "<ul><u><b>The Diseases found is: </b></u></br></ul>";
for($i = 0 ; $i<$c;$i++){
if($f[$i]>=6 && $f[$i] <=10){
echo "<li><a href = '$row[0]'><b>Moko Disease</b><a></li><br>";
}
if($f[$i]>=11 && $f[$i]<=16){
echo "<li><a href = '$row[1]'><b>Panama Disease</b><a></li><br>";
}
if($f[$i]>=1 && $f[$i]<=5){
echo "<li><a href = '$row[2]'><b>Bunchy-top Disease</b><a></li><br>";
}
}
}else{
echo "<br><a href = 'checklist.php'><b>Please check your symptoms</b><a>";
}
?>

Recommended Answers

All 4 Replies

Member Avatar for diafol

OK, quick question, what if the maximum number of checkboxes? If less than 32 - and always will be less than 32, we can use a bitwise trick. Example here: http://demos.diafol.org/bitwise-checkboxes.php

BTW you should post your form html. Otherwise we're guessing as to how you've implemented it.

commented: great approach +13
Member Avatar for diafol

Thanks cereal. However, I think I got the OP's requirements wrong. I did this for a non-bitwise version:

<?php
$output = '';
$bananaDiseases = [
    1=>'Bract mosaic',
    'Bunchy top',
    'Mosaic',
    'Streak',
    'Banana mild mosaic',
    'Nematode root rot',
    'Root-knot',
    'Root-lesion',
    'Anthracnose',
    'Black cross',
    'Black leaf streak',
    'Black root rot',
    'Brown blotch',
    'Brown spot',
    'Ceratocystis fruit rot',
    'Cigar-end',
    'Cladosporium speckle',
    'Corm dry rot',
    'Cordana leaf spot',
    'Crown rot',
    'Cylindrocladium root rot',
    'Damping-off',
    'Deightoniella fruit speckle',
    'Diamond spot',
    'Eyespot',
    'Fruit freckle',
    'Fruit rot',
    'Fungal root-rot',
    'Fungal scald',
    'Leaf speckle',
    'Leaf spot',
    'Main stalk rot'
];

if(isset($_GET['diseases']))
{
    $listDiseases = [];
    $rawDiseases = (array) $_GET['diseases'];
    foreach($_GET['diseases'] as $v)
        if(isset($bananaDiseases[$v])) $listDiseases[] = $bananaDiseases[$v];
    $output = (!empty($listDiseases)) ?
        '<h3>Diseases</h3><ul><li>' . implode('</li><li>', $listDiseases) . '</li></ul>' :
        '<p>No diseases</p>';
}

$checkBoxes = '';
foreach($bananaDiseases as $k=>$v)
{
    $check = (isset($_GET['diseases']) && in_array($k,$_GET['diseases'])) ? ' checked' : '';
    $checkBoxes .= "<label><input type='checkbox' name='diseases[]' value='$k'$check /> $v</label>";
}

?>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Page Title</title>
    <style>
        label{display:block;}
    </style>
  </head>
<body>
  <form>
        <?=$checkBoxes?>
      <input type="submit" value="Check" />
      <button id="clear-form">Clear Form</button>
  </form>
  <?=$output?>
    <script>
        document.getElementById('clear-form').addEventListener("click", function(){
            var checkboxes = document.getElementsByTagName('input');
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = false;
                }
            }
        });
    </script>
</body>
</html>

But again, I got it wrong, as the OP seems to want just one output not many :( Will get to it :)

Member Avatar for diafol

So perhaps like this:

<?php
$checkArray = '';
$output = 'No Diseases';
$diseaseArray = ['Moko Disease','Panama Disease','Bunchy-top Disease'];
$foundDiseases = [];

if(isset($_GET['checkbox']))
{
    $checks = (array) $_GET['checkbox'];
    foreach($checks as $c)
    {
        if ($c >= 1 && $c <= 5) {
            $checkArray[] = 2;
        } elseif ($c >= 6 && $c <= 10) {
            $checkArray[] = 0;
        } elseif ($c >= 11 && $c <= 16) {
            $checkArray[] = 1;
        }
    }
    $result = array_unique($checkArray);
    if(!empty($result))
    {
        foreach($result as $r)
        {
            $foundDiseases[] = $diseaseArray[$r];
        }
        $output = '<h3>Diseases Found:</h3><p>' . implode('</p><p>', $foundDiseases) . '</p>';
    }
}
?>



<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Page Title</title>
    <style>
        label{display:block;}
    </style>
  </head>
<body>
  <form>
      <input type="checkbox" name="checkbox[]" value="1" />
      <input type="checkbox" name="checkbox[]" value="2" />
      <input type="checkbox" name="checkbox[]" value="3" />
      <input type="checkbox" name="checkbox[]" value="4" />
      <input type="checkbox" name="checkbox[]" value="5" />
      <input type="checkbox" name="checkbox[]" value="6" />
      <input type="checkbox" name="checkbox[]" value="7" />
      <input type="checkbox" name="checkbox[]" value="8" />
      <input type="checkbox" name="checkbox[]" value="9" />
      <input type="checkbox" name="checkbox[]" value="10" />
      <input type="checkbox" name="checkbox[]" value="11" />
      <input type="checkbox" name="checkbox[]" value="12" />
      <input type="checkbox" name="checkbox[]" value="13" />
      <input type="checkbox" name="checkbox[]" value="14" />
      <input type="checkbox" name="checkbox[]" value="15" />
      <input type="checkbox" name="checkbox[]" value="16" />
      <input type="submit" value="Check" />
      <!-- YOUR CHECKBOXES ETC -->
  </form>
  <?=$output?>
 </body>
</html>

Thank you for sharing your ideas..:) i will try this code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.