Jump to content

Recommended Posts

My boss has a new survey for her site using php. The coder used arrays and told her to 'simply' fill in Topic A and Issue A...

This works great! Until she insists on using a period at the end of the sentence. That stops the topic from being read. I am sure there is a simple, silly solution but I am too new to this language and have spent hours trying to find it by Google.

 

Here is the problem code

 

<?php
//The array of questions. Replace "Issue A" and "Topics A 1",
//etc with your own values
$questions = array( 
			array(issue => "Rising health care costs are one of the biggest concerns for me",
						  topics => array( "•	How much plans are allowed to charge for premiums, deductibles, and other out of pocket expenses",
						  					"•	Proposals that allow the government to control health care costs for individuals and employers",
						  					"•	Plans that allow individuals to join purchasing pools to increase their buying power for insurance, medicines and other health care needs")
			),
						  
			array(issue => "I want to be able to choose my own doctor",
			topics => array( "•	Plans that allow you to stay with your current doctor",
						  	 "•	Proposals that give you the freedom to change doctors",
						  	 "•	Plans that let you see any doctor you choose, not only doctors on a list of preferred providers")
			),
			array(issue => "Everyone should pay something for their health care",
			topics => array( "•	Candidates who propose plans that include sliding scales",
						  	"•	Plans that include subsidies to help individuals and families at a variety of income levels purchase affordable health care",
						  	"•	Proposals that address issues of cost for people at all income levels")
			),
			array(issue => "I think the government should help ensure that all Americans have access to health care they can afford",
			topics => array( "•	Proposals that give the government the power to limit unfair rate increases",
			"•	How the government can eliminate the policy of pre-existing conditions",
			"•	Plans that allow the government to play a watchdog role over skyrocketing drug costs")
			),
			array(issue => "I would like to select from a range of government and private health care plans to find one that best meets my health care needs",
			topics => array( "•	Candidates who advocate for competition between government and private plans",
			"•	Proposals that guarantee a minimum level of comprehensive benefits from government and private health care plans",
			"•	Proposals that allow you to choose between government and private plans")
			)
				  );

 

Here is the code

 

 

<?php
	//if there's no POST vars, display the survey
	if(! $_POST) {
		$answers = array(	array( 'very-strongly', 'Very Strongly Agree'),
							array( 'strongly', 'Strongly'),
							array( 'moderately ', 'Moderately '),
							array( 'not-much', 'Not Much'),
							array( 'not-at-all', 'Not At All')
						);

		$out .= "<form class='survey' method='POST'>";
		foreach($questions as $q){
			$out .= "<div class='question'>";
			$out .= $q["issue"] . ".<br/><br/> ";
			foreach( $answers as $a ) {
			$out .= "</div>";
				$out .= "<div class='choice'>";
				$out .= "<input type='radio' name =' " . $q["issue"] . "' value='" . $a[0] . "'/>" . " " . $a[1] . " "." ";
			}

			$out .= "<p/>";

			$out .= "</div>";

		}
		//email section
		$out .= "Email (optional):<input type='text' name='email'>";
		$out .= "<p/>";
		$out .= "<input type='submit' value='Submit'/>";
		$out .= "</form>";
		echo $out;

	}else{
		$most_important = array();
		$medium_important = array();
		$low_important = array();
		$not_important = array();

		//db connect info
		$host = 'db47int.dotsterhost.com';
		$user = 'u1001628_herndon';
		$pass = 'seadragon';
		$database = 'db1001628_HAreg';
		$table = 'survey_responses';

		mysql_connect($host, $user, $pass);
		mysql_select_db($database);

		//create a "user id" representing this response group
		$get_user = "select Max(user) from $table";
		$q = mysql_query($get_user);
		$user_id = mysql_result($q, 0) + 1;

		//order the questions into response groups
		foreach($_POST as $issue => $response) {
			if( $response == 'very-strongly' or $response == 'strongly') {
				$most_important[] = return_question($issue);
			} elseif( $response == 'moderately ') {
				$medium_important[] = return_question($issue);
			} elseif( $response == 'not-much') {
				$low_important[] = return_question($issue);
			} else {
				$not_important[] = return_question($issue);
			}

			//save responses.
			$time = date('Y-M-D g:i A');
			$email = $_POST['email'];
			$ip=@$REMOTE_ADDR; 
			$q = "INSERT INTO `$table` VALUES( NULL, '$user_id', '$issue', '$response', '$time', '$ip', '$email')";
			mysql_query($q);
			//echo mysql_error();

		}


		//display the responses
		if( count($most_important) != 0) {
			$out .= "<strong>What concerns are most important to you?</strong>";
			$out .= "<br/>";
			foreach($most_important as $m) {	
				$out .= "<br/>";				
				$out .= $m['issue'] . " ";
				$out .= "<br/>";
			}
			$out .= "<br/>";
			$out .= "<strong>Because of this, you'll want to pay attention to issues like:</strong>";
			$out .= "<br/><br/>";

			foreach($most_important as $m) {
				foreach( $m["topics"] as $t) {
					$out .= $t . "<br/>";
				}
			}
		}

		$out .= "<br/>";

		if( count($medium_important) != 0) {
			$out .= "<strong>Also of concern are issues like:</strong>";
			$out .= "<br/>";
			foreach($medium_important as $m) {
				$out .= "<br/>";	
				$out .= $m['issue'] . " ";
				$out .= "<br/>";	
			}
			$out .= "<br/>";
			$out .= "<strong>So, you'll want to pay attention to:</strong>";
			$out .= "<br/><br/>";	

			foreach($medium_important as $m) {
				foreach( $m["topics"] as $t) {
					$out .= $t . "<br/>";
				}
			}
		}
		$out .= "<br/>";
		if(count($medium_important) == 0 and count($most_important) == 0) {
			if( count($low_important) != 0) {

				$out .= "<strong>No single issue stands out for you, but here are some things you might want to keep in mind</strong>";
				$out .= "<br/><br/>";

				foreach($low_important as $l) {
					foreach( $l['topics'] as $t) {
						$out .= $t . "<br/>";
					}
				}
			}else {
				$out .= "No single issue stands out for you.";
			}
		}

		echo $out;

	} ?>

Link to comment
https://forums.phpfreaks.com/topic/83697-solved-survey-array-using-sentence/
Share on other sites

I added a few lines. Is that anything like what you want>

 

<?php
	//if there's no POST vars, display the survey
	if(! $_POST) {
		$answers = array(	array( 'very-strongly', 'Very Strongly Agree'),
							array( 'strongly', 'Strongly'),
							array( 'moderately ', 'Moderately '),
							array( 'not-much', 'Not Much'),
							array( 'not-at-all', 'Not At All')
						);

		$out .= "<form class='survey' method='POST'>";
		foreach($questions as $q){
			$out .= "<div class='question'>";
			$out .= $q["issue"] . ".<br/><br/> ";
                                        
                                        $out .= "<ul>\n";                                        // added
                                        foreach ($q['topics'] as $t)                             // added
                                        {                                                        // added
                                            list(,$topic) = explode("\t", $t);                   // added
                                            $out .= "<li>$topic</li>";                           // added
                                        }                                                        // added
                                        $out .= "</ul>\n";                                       // added
                                        
			foreach( $answers as $a ) {
			$out .= "</div>";
				$out .= "<div class='choice'>";
				$out .= "<input type='radio' name =' " . $q["issue"] . "' value='" . $a[0] . "'/>" . " " . $a[1] . " "." ";
			}

			$out .= "<p/>";

			$out .= "</div>";

		}
		//email section
		$out .= "Email (optional):<input type='text' name='email'>";
		$out .= "<p/>";
		$out .= "<input type='submit' value='Submit'/>";
		$out .= "</form>";
		echo $out;

	}else{
		$most_important = array();
		$medium_important = array();
		$low_important = array();
		$not_important = array();

		//db connect info
		$host = 'db47int.dotsterhost.com';
		$user = 'u1001628_herndon';
		$pass = 'seadragon';
		$database = 'db1001628_HAreg';
		$table = 'survey_responses';

		mysql_connect($host, $user, $pass);
		mysql_select_db($database);

		//create a "user id" representing this response group
		$get_user = "select Max(user) from $table";
		$q = mysql_query($get_user);
		$user_id = mysql_result($q, 0) + 1;

		//order the questions into response groups
		foreach($_POST as $issue => $response) {
			if( $response == 'very-strongly' or $response == 'strongly') {
				$most_important[] = return_question($issue);
			} elseif( $response == 'moderately ') {
				$medium_important[] = return_question($issue);
			} elseif( $response == 'not-much') {
				$low_important[] = return_question($issue);
			} else {
				$not_important[] = return_question($issue);
			}

			//save responses.
			$time = date('Y-M-D g:i A');
			$email = $_POST['email'];
			$ip=@$REMOTE_ADDR; 
			$q = "INSERT INTO `$table` VALUES( NULL, '$user_id', '$issue', '$response', '$time', '$ip', '$email')";
			mysql_query($q);
			//echo mysql_error();

		}


		//display the responses
		if( count($most_important) != 0) {
			$out .= "<strong>What concerns are most important to you?</strong>";
			$out .= "<br/>";
			foreach($most_important as $m) {	
				$out .= "<br/>";				
				$out .= $m['issue'] . " ";
				$out .= "<br/>";
			}
			$out .= "<br/>";
			$out .= "<strong>Because of this, you'll want to pay attention to issues like:</strong>";
			$out .= "<br/><br/>";

			foreach($most_important as $m) {
				foreach( $m["topics"] as $t) {
					$out .= $t . "<br/>";
				}
			}
		}

		$out .= "<br/>";

		if( count($medium_important) != 0) {
			$out .= "<strong>Also of concern are issues like:</strong>";
			$out .= "<br/>";
			foreach($medium_important as $m) {
				$out .= "<br/>";	
				$out .= $m['issue'] . " ";
				$out .= "<br/>";	
			}
			$out .= "<br/>";
			$out .= "<strong>So, you'll want to pay attention to:</strong>";
			$out .= "<br/><br/>";	

			foreach($medium_important as $m) {
				foreach( $m["topics"] as $t) {
					$out .= $t . "<br/>";
				}
			}
		}
		$out .= "<br/>";
		if(count($medium_important) == 0 and count($most_important) == 0) {
			if( count($low_important) != 0) {

				$out .= "<strong>No single issue stands out for you, but here are some things you might want to keep in mind</strong>";
				$out .= "<br/><br/>";

				foreach($low_important as $l) {
					foreach( $l['topics'] as $t) {
						$out .= $t . "<br/>";
					}
				}
			}else {
				$out .= "No single issue stands out for you.";
			}
		}

		echo $out;

	} ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.