Solved

function to display dynamic data in dropdown

Posted on 2016-11-07
8
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 110

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
 
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 110

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

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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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