Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

function to display dynamic data in dropdown

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

Avatar of Crazy Horse
Crazy Horse
Flag of South Africa image

ASKER

@ 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

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.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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! :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
@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.
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!