Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

function to display dynamic data in dropdown

Posted on 2016-11-07
8
Medium Priority
?
82 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
 
LVL 1

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 111

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 1600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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
 
LVL 1

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 111

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
 
LVL 1

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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