www.webdeveloper.com
Results 1 to 13 of 13

Thread: array question (I think!)

  1. #1
    Join Date
    May 2009
    Posts
    124

    array question (I think!)

    Let's see if I can explain what I am trying to do here.

    I am creating a (for fun) diagnosis tool for my website. The idea is to have users fill out a checkbox list of things like vomiting, diarrhea, increased thirst, etc.

    Then, that goes to a php file to be processed. What I want to do is something like this:

    If vomiting is checked then add one point to each disease condition that includes vomiting such as pancreatitis, gastritis, etc.

    Then, at the end, I want to add up each of the totals and display the top 5.

    I'm pretty sure I need to store these as arrays, but I just can't get my head around how to do it!

    Thanks, any help is appreciated!

    Dr. Marie

  2. #2
    Join Date
    Apr 2010
    Posts
    213
    If you'll forgive a slightly off topic observation, that's a very worthwhile idea! You might think about writing it in a MySQL based CMS. The actual MySQL query is very few lines of code (two or three) and the database is then easily expanded to encompass a vast array of symptoms and diseases. It could even be enhanced to receive feedback and refine its diagnostics accordingly.

    Just a thought,

    Chipzzz

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    Yes, you will need an array of checkboxes to send to your php script.

    In your html form you will need something like this:

    Code:
     
    <input type="checkbox" name="chkSymptoms[]" value="vomiting" />
    <input type="checkbox" name="chkSymptoms[]" value="diarrhea" />
    .
    .
    .
    When you click the submit button an array named chkSymptoms with user selected values is sent to the server.

    Your php code would look something like this:

    PHP Code:
    //get the form data
    $symptoms $_POST['chkSymptoms'];  //store the checked symptoms in an array called $symptoms
     
    foreach($symptoms as $value) {
        if(
    $value == 'vomiting') {
            
    //do something here
        
    }
     
        if(
    $value == 'diarrhea') {
            
    //do something else here
        
    }
    }
     
    //the rest of your processing 
    You could use switch() but I'm trying to keep it fairly simple for now.
    Last edited by tirna; 04-17-2010 at 08:19 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    If you go for a hard-coded, array-based solution, it could be something like:
    PHP Code:
    $diseases = array(
       
    'pneumonia' => 0,
       
    'mumps' => 0,
       
    'tonsillitis' => 0
    );
    if(
    $highTemperature)
    {
       
    $diseases['pneumonia']++;
       
    $diseases['mumps']++;
       
    $diseases['tonsillitis']++;
    }
    if(
    $swollenThroat)
    {
       
    $diseases['mumps']++;
       
    $diseases['tonsillitis']++;
    }
    if(
    $vomiting)
    {
       
    $diseases['mumps']++;
    }
    arsort($diseases); // sort highest values to beginning of array
    $bestGuess key($diseases); // get the key of the first array element 
    You could even add weighted amounts for each question, e.g.:
    PHP Code:
    if($vomiting)
    {
       
    $diseases['mumps'] += 2// add 2 for significant symptom

    (I'm not doctor and am not claiming vomiting is a significant mumps symptom, just making an example. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2009
    Posts
    124
    Ah, thanks guys!

    Tirna...your way is pretty much how I was doing it, but I needed the foreach line.

    You guys rock!

    And Nog...You'd probably make a great doctor...a doctor has to be able to explain things to people and you do that well!

    Nog, do you remember when I first came online a year ago and you helped me figure out how to learn php, mysql, etc? My site has been up and running for a while now and is really doing well!

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    Quote Originally Posted by DrMarie View Post
    Ah, thanks guys!

    Tirna...your way is pretty much how I was doing it, but I needed the foreach line.

    You guys rock!

    And Nog...You'd probably make a great doctor...a doctor has to be able to explain things to people and you do that well!

    Nog, do you remember when I first came online a year ago and you helped me figure out how to learn php, mysql, etc? My site has been up and running for a while now and is really doing well!
    I can't say I specifically remember that, but I recognized your user name, so I suppose there's something hidden in the musty file cabinets of my memory.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2009
    Posts
    124
    OK...I'm getting there. I've included my (very rudimentary) code here:

    PHP Code:
    <?php
    $symptom
    []=$_POST['symptom'];


    $num=count($symptom);


    $disease = array(
       
    'pancreatitis' => 0,
       
    'gastritis' => 0,
       
    'kidney disease' => 0,
       
    'liver disease' => 0,
       
    'diabetes' => 0
    );
    if (
    $num==0)
      echo 
    "Error";
    else {
      foreach(
    $symptom as $value) {
        if (
    $value=="vomiting") {
           
    $disease['pancreatitis']+=2$disease['gastritis']+=2$disease['kidney disease']+=1$disease['liver disease']+=1;
        }
        if (
    $value=="polydypsia") {
           
    $disease['kidney disease']+=2$disease['liver disease']+=1$disease['diabetes']+=2;
        }
       
     
      }


    arsort($disease);
    print 
    "Top matches:\r\n";

    $top key($disease);
    print_r ($top);

    }
    ?>
    So, when I print $top I just get the number 1 choice. How do I print out the top 5?

  8. #8
    Join Date
    Mar 2010
    Posts
    2,803
    One way of doing it would be :

    PHP Code:
    <?php

    $rows2display 
    5;

    for(
    $i=0$i<$rows2display$i=$i+1) {
        echo 
    'Disease: '.key($disease[i]).' = '.$disease[i].' points.<br />';
    }

    ?>
    Last edited by tirna; 04-18-2010 at 08:32 PM.

  9. #9
    Join Date
    May 2009
    Posts
    124
    Ah, ok...thank you!

    Bless you tirna for helping me out!

  10. #10
    Join Date
    May 2009
    Posts
    124
    I'm getting errors:

    PHP Code:
    Warningkey() [function.key]: Passed variable is not an array or object in /home/drmarie/public_html/tool.php on line 37
    Disease
    : = points.

    Warningkey() [function.key]: Passed variable is not an array or object in /home/drmarie/public_html/tool.php on line 37
    Disease
    : = points.

    Warningkey() [function.key]: Passed variable is not an array or object in /home/drmarie/public_html/tool.php on line 37
    Disease
    : = points.

    Warningkey() [function.key]: Passed variable is not an array or object in /home/drmarie/public_html/tool.php on line 37
    Disease
    : = points.

    Warningkey() [function.key]: Passed variable is not an array or object in /home/drmarie/public_html/tool.php on line 37
    Disease
    : = points
    I got a similar error when I tried to echo disease[1]. I think it has something to do with the fact that it is an associative array?

  11. #11
    Join Date
    Mar 2010
    Posts
    2,803
    Sorry - I forgot it was an associative array and completely stuffed it up

    This should work but there is probably an easier way:

    PHP Code:
    <?php
    $rows2display 
    5;
    $count 0;
    foreach(
    $disease as $key => $value) {
       if(
    $count $rows2display) {
        echo 
    'Disease: '.$key.' = '.$value.' points.<br />';
      }
      
    $count $count 1
    }
    ?>

  12. #12
    Join Date
    May 2009
    Posts
    124
    Once again, thanks!

  13. #13
    Join Date
    Mar 2010
    Posts
    2,803
    no problem happy to help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles