Solved

function to display dynamic data in dropdown

Posted on 2016-11-07
8
48 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 109

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 109

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 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

790 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