Solved

function to display dynamic data in dropdown

Posted on 2016-11-07
8
28 Views
Last Modified: 2016-11-09
I am trying to use a function to display dynamic data in a dropdown. This sort of works but instead of one dropdown it has about 20, each with one value. Each one does have a different value so at least I know it is pulling all the records. It just isn't displaying properly.

function cat_dropdown($link) {
	
	$stmt = $link->prepare("SELECT `cat_id`, `category_name` FROM `categories`");
	$stmt->execute();
	$result = $stmt->get_result();
	$numRows = $result->num_rows;
	if($numRows > 0){
		while($row = $result->fetch_assoc()) {
			
			$cat_id = htmlentities($row['cat_id']);
			$category_name = htmlentities($row['category_name']);
			$cat = <<<CAT
			
			          <div class="col-sm-3">
              <div class="form-group">
                <select class="form-control" id="departments" name="departments">
                  <option value="0" selected="selected">
                    Categories
                  </option>
                  <option value="$cat_id">
                    $category_name
                  </option>
                </select>
              </div>
            </div>
			  
CAT;
echo $cat;
		}
	}
	
	$stmt->close();
}

Open in new window

0
Comment
Question by:Black Sulfur
  • 4
  • 3
8 Comments
 

Author Comment

by:Black Sulfur
ID: 41877767
@ Ray, in case you look at this, you said I shouldn't do something like this:

$email = $_POST['email'];

Open in new window


I hope this doesn't equate to the same thing but I wouldn't think so because it isn't a super global? It's a row from the database?

$category_name = htmlentities($row['category_name']);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41878057
a row from the database...
Yes, and it's not the same as copying a variable (Superglobal or not) -- it's calling a function that transforms a variable into another variable with a new value.  That's a big difference and a good one!  A new value should generally have a new name unless the new value is utilized and disposed of very "locally" in the same block of code

It would be helpful to see the data you're using to test this.  Maybe a mockup of the database would give us some insights into why we're not getting the expected output.  At first blush, it looks right to me, but I'm watching Monday Night Football with the other eye and drinking beer, so I'm probably not very reliable tonight.  Maybe another expert will weigh in, or I can try to look at it again in the morning.
0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
ID: 41878124
Try:
function cat_dropdown($link) {
	
	$stmt = $link->prepare("SELECT `cat_id`, `category_name` FROM `categories`");
	$stmt->execute();
	$result = $stmt->get_result();
	$numRows = $result->num_rows;
	if($numRows > 0){
			echo <<<CAT_TOP
			
			          <div class="col-sm-3">
              <div class="form-group">
                <select class="form-control" id="departments" name="departments">
                  <option value="0" selected="selected">
                    Categories
                  </option>
CAT_TOP;

		while($row = $result->fetch_assoc()) {
			
			$cat_id = htmlentities($row['cat_id']);
			$category_name = htmlentities($row['category_name']);
                  echo '
                  <option value="$cat_id">
                    $category_name
                  </option> ';
		}

			echo <<<CAT_BOTTOM
                </select>
              </div>
            </div>
			  
CAT_BOTTOM;
	}
	
	$stmt->close();
}

Open in new window

0
 

Author Comment

by:Black Sulfur
ID: 41878148
I only get to work on learning and coding after a full day's work in a completely different field so a lot of times I am quite tired when I get to it and I usually end up working until late at night and when I go to bed all the code is still running through my head. I think I must even dream about it because at 3am I woke up with a Eureka moment realizing that I couldn't do it the way I did it to display products from the database because I am not trying to duplicate the actual physical drop down itself, but the contents that go into it! Silly me. I will try fix it on my own before looking at the solution posted here and if I still can't get it I will let you guys know either way.

Thanks!

P.S. I want to get to the point where I can be watching football and drinking beer! :)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 41878561
In this comment, which otherwise looks mostly right to me, the apostrophes may be misapplied in the echo statement (line 22).  This would be a good place for another HEREDOC block because variable substitution is needed to get the $cat_id and $category_name information into the HTML option tags.  Fuller explanation here:
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html
0
 

Author Comment

by:Black Sulfur
ID: 41880054
Okay, so I got it working. All I did was change the heredoc to only:

 </option>
<option value="$cat_id">
 $category_name
 </option>

Open in new window


because like I mentioned above, I had wrapped the entire drop down itself into the loop, instead of just the actual records themselves.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41880343
@Black Sulfur: It's wise to test the proposed solutions before you accept them.  Not everyone tests the solutions they post here at E-E; that part is up to the Author.
0
 

Author Comment

by:Black Sulfur
ID: 41880980
Apologies Ray, I was in a hurry to close out so the question didn't stay open too long. I will take more care next time!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now