Jump to content

How to reuse data from SQL in a class.


guymclarenza

Recommended Posts

I want to reuse the data from getFaq for different levels of a website. 

$fcid is the variable. 
 

Creating the full page in the getFaq function could work but limits the function to one set of results,

Do I need to create a new function if I want to create another page with the data and a new call to the database?

It seems to me there must be a way to store the array from $fcid  and call it from another page to get a subset of the data. 

I added the getFP class as an attempt to do just that but I am getting no results. 

If I place the code in the getFP class in the getFAQ function the script works as envisioned. 

I want to create a getFAQpage, a getFAQheader and a getFAQsubset based on  categories. 

The data is all there in $fcid but I have no idea on how to access it from outside the class. 

Dbh is the PDO connection. 

 

Class GetData extends Dbh {
	
			
		public function getFaq($site) {
			$sql = "SELECT faq_cats.faqc_name, faqs.faq_question, faqs.faq_answer FROM faq_cats INNER JOIN faqs ON faq_cats.faqc_id = faqs.faq_cat WHERE faq_cats.faqc_site = ? ORDER BY faq_cats.faqc_name";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$site]);
			$fcid = $stmt->fetchAll();
			return $fcid;	
		}

	}

	Class getFP extends GetData {

		
		public function getFaqPage([$fcid]) {
			
			$fcid = $this->fcid;
			$fhead = null;		
				foreach ($fcid as $faq) {
					$fname = $faq["faqc_name"];			
					$fquest = $faq["faq_question"];
					$fans = $faq["faq_answer"];
					
						if ($fhead != $fname) {
							echo "<h2>".$fname."</h2>";
						}
	
					echo $fquest."<br />".$fans."<br /><br />";
					$fhead = $fname;
				}
		}
	

	}
	
	
	$faqob = new getFP(); 
	$faqob->getFaqPage([$fcid]);

 

Link to comment
Share on other sites

This does not work 

public function getFaq($site) {
			$sql = "SELECT faq_cats.faqc_name, faqs.faq_question, faqs.faq_answer FROM faq_cats INNER JOIN faqs ON faq_cats.faqc_id = faqs.faq_cat WHERE faq_cats.faqc_site = ? ORDER BY faq_cats.faqc_name";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$site]);
			$fcid = $stmt->fetchAll();
			
			$fhead = null;		
				foreach ($fcid as $faq) {
					$fname = $faq["faqc_name"];			
					$fquest = $faq["faq_question"];
					$fans = $faq["faq_answer"];
					$fcont = null;
						if ($fhead != $fname) {
							$fcont .= "<h2>".$fname."</h2>";
						}
	
					$fcont .= $fquest."<br />".$fans."<br /><br />";
					$fhead = $fname;

					
				}
			
		}

	}

	$fcont;	
	$faqob = new getData(); 
	$faqob->getFaq($site);

	echo $fcont;	
	

This does
 

Class GetData extends Dbh {
	
			
		public function getFaq($site) {
			$sql = "SELECT faq_cats.faqc_name, faqs.faq_question, faqs.faq_answer FROM faq_cats INNER JOIN faqs ON faq_cats.faqc_id = faqs.faq_cat WHERE faq_cats.faqc_site = ? ORDER BY faq_cats.faqc_name";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$site]);
			$fcid = $stmt->fetchAll();
			
			$fhead = null;		
				foreach ($fcid as $faq) {
					$fname = $faq["faqc_name"];			
					$fquest = $faq["faq_question"];
					$fans = $faq["faq_answer"];
					$fcont = null;
						if ($fhead != $fname) {
							$fcont .= "<h2>".$fname."</h2>";
						}
	
					$fcont .= $fquest."<br />".$fans."<br /><br />";
					$fhead = $fname;
						
					echo $fcont;
					
				}
			
		}

	}

	$fcont;	
	$faqob = new getData(); 
	$faqob->getFaq($site);

Can anyone tell me why and how to make the previous one work.

Link to comment
Share on other sites

public function getFaq($site) {
			$sql = "SELECT faq_cats.faqc_name, faqs.faq_id, faqs.faq_question, faqs.faq_answer FROM faq_cats INNER JOIN faqs ON faq_cats.faqc_id = faqs.faq_cat WHERE faq_cats.faqc_site = ? ORDER BY faq_cats.faqc_name";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$site]);
			$fcid = $stmt->fetchAll();
			
			$fhead = null;
			$findex = null;
			$fcont = null;
			echo "<h1>FAQ Index</h1>";

			foreach ($fcid as $faq) {
				$fname = $faq["faqc_name"];			
				$fquest = $faq["faq_question"];
				$fans = $faq["faq_answer"];
				$faid = $faq["faq_id"];

					if ($fhead != $fname) {
						$findex .= "<h2>".$fname."</h2>";
					}
					$findex .= "<a href=\"#".$faid."\">".$fquest."</a><br />";
					$fhead = $fname;
				}

				

				foreach ($fcid as $faq) {
					$fname = $faq["faqc_name"];			
					$fquest = $faq["faq_question"];
					$fans = $faq["faq_answer"];
					$faid = $faq["faq_id"];
					
					$fcont .= "<a name=\"".$faid."\"></a>".$fquest."<br />".$fans."<br /><br />";
					
				}

				echo $findex;
				echo "<br /><br />";
				echo $fcont;
			
		}

	}

	$faqob = new getData(); 
	$faqob->getFaq($site);

Is there a shorter or more concise way of doing this?

The result looks like this

 

FAQ Index

DNA Testing FAQs

What should I do with unexpected DNA results?
How can I learn more about DNA and how to use it?
What should I consider before testing my DNA?
What is DNA?

Genealogy FAQs

What is the difference between my family tree and my genetic tree?
What types of DNA are tested for ancestry purposes?

Paternity Test FAQs

How do I receive my results?
How long do I have to wait for my paternity test results?
How long will the paternity test take?
How is the paternity test done?
If the mother is deceased or does not want to come, what can we do?
Does the mother have to be present?


What should I do with unexpected DNA results?
If you receive unexpected results, such as the identity of parents, grandparents, great-grandparents, or other previously unknown relatives, be sensitive and considerate of others involved who may be affected by the results. Approach family members with c

How can I learn more about DNA and how to use it?
Consider these resources in learning more about DNA and how you can use it effectively in your own family history activities: ISOGG Wiki—The International Society of Genetic Genealogists has created and maintains a wiki (community written encyclopedia)

What should I consider before testing my DNA?
DNA test results may reveal unexpected information about your family history, such as adoptions, unexpected paternity, etc. The affected party may be yourself or a close DNA relative—perhaps someone you know nothing about. If this new information may be u

What is DNA?
DNA, or deoxyribonucleic acid, is the genetic code that defines each person’s biological characteristics. DNA is chains of four individual nucleotide links, the sequence of which encode our genetic information. These strings of DNA are coiled into package

What is the difference between my family tree and my genetic tree?
Your family tree is what has been documented over time through family history records and often reflects relationships, some of which may not be biological. Your genetic tree consists of the relatives with whom you share DNA, which you mutually inherited

What types of DNA are tested for ancestry purposes?
DNA can provide information in different ways to help you identify your related family members and ancestors, depending on which part of the DNA molecule is used. Typically, four types of DNA are used to discover more about your ancestry: atDNA Autoso

How do I receive my results?
You have options to choose from that you indicate on the form

How long do I have to wait for my paternity test results?
Approximately 10-15 working days

How long will the paternity test take?
The test itself takes approximately 150 minutes

How is the paternity test done?
a Finger prick of the parties involved to collect a small blood sample

If the mother is deceased or does not want to come, what can we do?
If the mother is still alive but does not want to be present for the test, an affidavit is required explaining why she is not present. The affidavit must be brought with the other parties. If the mother is deceased, then the death certificate of the mo

Does the mother have to be present?
If the child is a minor (younger than 18 years), the mother needs to be present to sign consent for testing for the minor child. If the child is older than 18 years, the mother need not be present.

 

Link to comment
Share on other sites

On 2/24/2021 at 4:32 AM, guymclarenza said:

Can anyone tell me why and how to make the previous one work.

The first code block doesn't work because of variable scope in PHP. The $fcont inside the class is not the same $fcont as the one outside the class. There are several ways to fix the issue, and given where you appear to be in learning PHP your solution in the second code block is fine albeit not ideal. Another solution is to build the string in the class method and return it to the calling code, which then prints the result. The best and most advanced option is to use a templating language like Twig or Blade, though that's probably going to be a topic for the future.

Link to comment
Share on other sites

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.