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
    22,335
    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. )
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,335
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.21292 seconds
  • Memory Usage 3,015KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (7)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (13)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (13)postbit
  • (13)postbit_onlinestatus
  • (13)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates