Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

This PHP code is not working as expected, troubleshooting assist please

If have a table called category_name which has a long list of categories for the dropdown below.  The code below is not showing the selection from the database, but it is displaying the dropdown just fine.  Any ideas?  When I var_dump $row["category"] i do get values.  Any ideas?  Thanks.

<select>
<?php 
while ($category = mysqli_fetch_assoc($result2)) {
	
		$selected = (empty($row["done"])) ? "" : $row["category"];
		
		$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $selected, $category['category_name'], $category['category_name']);
	
}
echo $category_options 

?>



</select>

Open in new window

0
LB1234
Asked:
LB1234
  • 10
  • 9
  • 8
  • +2
12 Solutions
 
GaryCommented:
I'm guessing this

$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $selected, $category['category_name'], $category['category_name']);

should be

$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>",  $category['category_name'], $selected, $category['category_name']);
0
 
Chris StanyonCommented:
As well as what Gary's pointed out, what does row['category'] actually contains, because you're setting the value of $selected to it. Unless it's the word 'selected' then your HTML will be wrong, even after you've put the sprintf arguments in the right order.

Try this:

$selected = (empty($row["done"])) ? '' : 'selected';
		
$category_options .=  sprintf("<option value = '%s' %s>%s</option>", $category['category_name'], $selected, $category['category_name']);

Open in new window

0
 
LB1234Author Commented:
That's a step in the right direction Cathal, but now the drop down is simply displaying the last item in the category result set, ignoring the selected variable.  If I var_dump $row["category"], I do get what I'm expecting.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Chris StanyonCommented:
Read my previous comment. Because of your sprintf() statement, every OPTION will contain the 'selected' attribute, so the last one will always be selected.

You only include the selected attribute on the element you want selected!
0
 
GaryCommented:
Well this
$selected = (empty($row["done"])) ? "" : $row["category"];

Is being set every time for every option, your code has no logic to set it only when needed from what you have given us..
As Chris has asked what are they? What is done? I assume $row["category"] = "selected"?
Normally you have logic that compares the value that you want selected to the current value in the loop - if the same then set selected.
0
 
Ray PaseurCommented:
@LB1234: One of the best things you can do with a question like this is show us the data.  We rarely need to see code that we already know doesn't work.  One of the best tools for visualizing the data is PHP var_dump().

Why the data?  Because if we have that, we can create the SSCCE and test it before we post the code here at EE!  You'll get better results than if we are forced to guess about the data and therefore post untested code.
0
 
LB1234Author Commented:
ok let me read that and repost this question according to SSCCE.  Sorry guys. :(
0
 
Ray PaseurCommented:
No apology necessary, nor solicited.  We just want to lead you in the right direction!

Example of how to show us the data.  What is the value for $row?  Where did this get set?  Also, note the use of error_reporting(E_ALL).  You want that all of the time!

<select>
<?php
error_reporting(E_ALL); 
while ($category = mysqli_fetch_assoc($result2))
{
    // SHOW WHAT IS BEING RETRIEVED FROM THE DATA BASE QUERY
    var_dump($category);
    
    // WHAT IS THE VALUE OF $row??
    $selected = (empty($row["done"])) ? "" : $row["category"];
    
    CONCATENATE SOMETHING
    $category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $selected, $category['category_name'], $category['category_name']);
}
echo $category_options 
?>
</select>

Open in new window

0
 
LB1234Author Commented:
I mean not rePOST the question but restate it.
0
 
Ray PaseurCommented:
:-)

We will be here!
0
 
Chris StanyonCommented:
Ray - the point I was making with LB1234's code is that every option will have the selected attribute, as does yours. For this to work you only have the selected attribute on the option you want selected.

This is wrong (the last element will always be selected!):

<option value="1" selected="">1</option>
<option value="2" selected="">2</option>
<option value="3" selected="something">3</option>
<option value="4" selected="">4</option>

This is right:

<option value="1" >1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
0
 
Ray PaseurCommented:
Chris: I just copied  LB1234's code and added var_dump() so we can see the data.  I don't really know what the variables contain.  But I certainly agree that with selected options you'll only get the last one, unless you've used <select multiple>.

We would probably also want to give a name= attribute to the <select> tag.  Without that, nothing will be present in the request, no matter what kind of selections the client makes.
0
 
LB1234Author Commented:
Guys, in the ternary operator it doesn't say $selected = 'selected = " "'

but just ' '  so I don't understand why the word selected is being added to every line.

Seems to me $select equals empty or 'selected'


I don't understand.
0
 
LB1234Author Commented:
I do however get that if every line is selected it'll show the last one.  I don't understand why the ternary isn't providing a blank where selected would be, in accordance with whether or not the field "done" has a 1 or a 0 in in it (done represents a checkbox).
0
 
Chris StanyonCommented:
The reason selected is being added to every option is because of your sprintf() statement:

$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $selected, $category['category_name'], $category['category_name']);

So, taking this to it's conclusion, if your $row[category] is empty, you will still end up with:

<option value = 'something' selected = ''>some value</option>

Open in new window

Which brings me right back to my first comment - you will have the selected attribute on EVERY option = whether it has a value or not is irrelevant
0
 
GaryCommented:
The field done never changes in your loop - it is always the same.
0
 
Ray PaseurCommented:
don't understand why the word selected is being added to every line
That's the dictionary picture example of why we need to see your data! The ternary operator is being applied to a variable named $row, but $row is undefined in the script, so we don't know what's in there.
0
 
Chris StanyonCommented:
Ray,

The word selected is being added to every line because it's hard coded in the sprintf() statement
0
 
LB1234Author Commented:
It is still happening with revised code:

<select>
<?php 
while ($category = mysqli_fetch_assoc($result2)) {
	
	$selected = (empty($row["done"])) ? '' : 'selected';
		
	$category_options .=  sprintf("<option value = '%s' %s>%s</option>", $category['category_name'], $selected, $category['category_name']);
	
}
	echo $category_options;

?>



</select>

Open in new window

0
 
Ray PaseurCommented:
I'll ask this one last time, then if you can't give us an answer, I'll sign off on the question.

Where is the data?

What is the value of the array $row?

0
 
GaryCommented:
I'll say it again
$selected = (empty($row["done"])) ? '' : 'selected';

$row['done'] is either empty or not - it will never change in the loop because the value is static.
How does a value of empty or 1 have any bearing on a category name?
0
 
GaryCommented:
Change your code to this and let us know what is displayed

<?php 
echo $row["done"]."-".$row['category'];
while ($category = mysqli_fetch_assoc($result2)) {
		$selected = (empty($row["done"])) ? "" : $row["category"];
		
		$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $category['category_name'], $selected, $category['category_name']);
	
}
?>
<select>
<?php echo $category_options;?>
</select> 

Open in new window

0
 
LB1234Author Commented:
Cathal, here's what's displayed.  Definitely progress.  It's pulling information from the category row regardless of whether or not done is checked.  The first image is the echo to the browser and the second is the PHPMYadmin shot of the table itself.

broswer output
phpmyadmin table
0
 
GaryCommented:
I'm guessing this is what you should be doing

<?php 
while ($category = mysqli_fetch_assoc($result2)) {
		$selected = ($category['category_name']==$row["category"]?"selected":"");
		$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $category['category_name'], $selected, $category['category_name']);
	
}
?>
<select>
<?php echo $category_options;?>
</select> 

Open in new window

0
 
Slick812Commented:
greetings LB1234, you have an easy to do Loop error, that many who try loops (while loop in this case) do not catch, so you say things like -
"I don't understand why the ternary isn't providing a blank where selected would be"

in your while loop as -
while ($category = mysqli_fetch_assoc($result2)) {

the mysqli_fetch_assoc will ADVANCE the $category  ROW to the Next ROW until all are returned, which is what you want, , HOWEVER the $category and "while mysqli_fetch_assoc( ) loop" have nothing to do with the $row array and it is NOT ADVANCED to the next $row, SO it is important that you see that the $row is ADVANCED to the next $row in another loop, probably as -
while ($row = mysqli_fetch_assoc($result)) {

It may be difficult to design a double advance loop for $row and $category,  that would ADVANCE BOTH array to the NEXT row, this would depend on their "Relationships" to each other, if the $row and $category have the same amount of while looping, it may be a success, however as Ray has tried to tell you, we can not see the full picture of what need to be fixed, without better info. . .. Can you at least show us the loop that gets the $row may be as -
while ($row = mysqli_fetch_assoc($result)) {
and the associated code?
0
 
GaryCommented:
I still don't understand the relationship of DONE to CATEGORY?
0
 
LB1234Author Commented:
Ok here it is:

<?php session_start();  ?> 
<?php include ("header.php");?>
<?php include ("nav.php");?>
<?php include ("menu.php");?>




<form method="post">

<table class="gridtable" width="88%" border="0" cellspacing="0" cellpadding="2">
 
   <tr>
        <th scope="col">Date</th> 
        <th scope="col">Amount</a></th>
        <th scope="col">Vendor</th>
        <th scope="col">Description</th>
        <th scope="col">Bind</th>
        <th scope="col">Category</th>
        <th scope="col">Notes</th>
        <th scope="col">Bind</th>
        <th scope="col">Last Updated</th>
        <th scope="col">Updated By</th>
        <th scope="col">Done</th>
  </tr>
 
 
 <?php

//pulls  all table data



		

// pulls options for category drop down

$category_options = "";

$category_query = "SELECT * FROM category";

$result2 = mysqli_query($connection, $category_query);




$query = "SELECT * FROM transactions LIMIT 10";

$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result)) {
	$id = $row["transactions_id"];
	
?>


  <tr>
<!-- Date-->   				<td><?php echo date("n-d-y", strtotime($row["date"]));?></td>
<!-- Amount-->   			<td><?php echo "$" . number_format($row["amount"], 2)?></td>
<!-- Vendor-->   			<td><?php echo $row["vendor"]?></td>
<!-- Description-->   		<td><?php echo $row["description"]?></td>
<!-- First Bind-->    		<td class="center"><input type="checkbox"></td>




<!-- Category Drop Down-->  <td class="center">

<?php 
while ($category = mysqli_fetch_assoc($result2)) {
		$selected = ($category['category_name']==$row["category"]?"selected":"");
		$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $category['category_name'], $selected, $category['category_name']);
	
}
?>
<select>
<?php echo $category_options;?>
</select> 

</td>




<!-- Notes-->   			<td><textarea><?php echo $row ["notes"] ?></textarea></td>
<!-- Second Bind-->			<td class="center"><input type="checkbox"></td>
<!-- Last Updated-->  		<td class="center"><?php echo date("n-d-y")?></td>
<!-- Updated By-->			<td>temp</td>
<!-- Done-->      			<td class="center"><input 

<?php $checked = (empty($row["done"])) ? "" : "checked";

echo $checked;
?>



type="checkbox"></td>
  </tr>


<?php

}
?>

</table><br>

<input name="submit" type="submit" id="submit" align="right">

</form>


<?php

var_dump( $row["done"]);

?>

<?php include ("footer.php");?>

Open in new window

0
 
GaryCommented:
What happens now?
0
 
LB1234Author Commented:
Cathal it's one big form, and one when someone clicks "done" to I want the form to retain those values so the next time they log in, they're see what was finished and nothing else.  I want the values they entered to be saved and displayed in the dropdown so they can see what they've entered so far.  If done is checked, then the dropdown value should show what they entered last time.
0
 
Slick812Commented:
Can you say why you use a "SELECT" if you want this -
"If done is checked, then the dropdown value should show what they entered last time"
and what is supposed to happen if the if the use the dropdown to chose another value? It is just suppose to allow them to do several dropdown values settings, I do not see how this can work, sorry?
0
 
GaryCommented:
Ok, starting to make sense now
Try this. I am assuming that the category names that you get from here
$category_query = "SELECT * FROM category";
are exactly the same as in the DONE table?

<?php 
while ($category = mysqli_fetch_assoc($result2)) {
		$selected = ($category['category_name']==$row["category"] && $row["done"]==1?"selected":"");
		$category_options .=  sprintf("<option value = '%s' selected = '%s'>%s</option>", $category['category_name'], $selected, $category['category_name']);
	
}
?>
<select>
<?php echo $category_options;?>
</select>

Open in new window

0
 
Slick812Commented:
if you have the DONE Box checked on the right, you do not need the repetitive Category dropdown? The Done BOX will show the last Done won't it? If it is set from the database read, but there seems to be a DONE column in both?
0
 
LB1234Author Commented:
Gents, thanks for your help.  Heading home for the day.  I'll reply tomorrow.  Sorry about the terrible annotations and explanations.  I posted this when i was mentally worn out and incredibly frustrated with this issue.  I always post this stuff only after I've tried my best for a long while to solve it on my own.
0
 
Ray PaseurCommented:
OK, if you won't post the test data, I'm out.  Good luck, ~Ray
0
 
Slick812Commented:
Not sure I see the full picture, but trying to get several "done" column rows from a single $row, to use with the loop for $category['category_name'] is not going to happen, again I do not see a select box as a good choice for this kind of display for a single value of "what they entered last time", and using the same repetitive dropdown an even poorer choice, but I may not understand. I looks to me as the db query of-
        $query = "SELECT * FROM transactions LIMIT 10";
has all of the data-info needed to show the correct output, without the category SELECT, but I have to guess a little.
anyway this would be my version, I use this CSS  style="background:#888;"  to grey the background for ALL completed (done) rows
and on the non-done rows I add this to the Category <td>  " UNFINISHED, Must Complete". .
code for the <tr> section below
<tr<?php if($row["done"]) echo ' style="background:#888;"'?> >
<!-- Date-->  <td><?php echo date("n-d-y", strtotime($row["date"]));?></td>
<!-- Amount-->  <td><?php echo "$" . number_format($row["amount"], 2)?></td>
<!-- Vendor-->  <td><?php echo $row["vendor"]?></td>
<!-- Description--> <td><?php echo $row["description"]?></td>
<!-- First Bind-->  <td class="center"><input type="checkbox"></td>
<!-- Category-->  <td><?php echo $row["category"]?><?php if(!$row["done"]) echo " UNFINISHED, Must Complete";?></td>
<!-- Notes-->  <td><textarea><?php echo $row ["notes"] ?></textarea></td>
<!-- Second Bind--><td class="center"><input type="checkbox"></td>
<!-- Last Updated--><td class="center"><?php echo date("n-d-y")?></td>
<!-- Updated By--> <td>temp</td>
<!-- Done-->  <td class="center"><input type="checkbox"<?php if($row["done"]) echo " checked";?>></td>
</tr>

Open in new window

but I am guessing that there is a $row["category"]
I hope this give you some idea, but I really think you need a different GUI than the dropdowns
0
 
Slick812Commented:
if I did not get your meanings, and you need the Category table SELECT, and the many dropdowns, then you night consider the query -
$category_query = "SELECT * FROM category WHERE done = 0";

and then you do not need the option select, as the dropdown will only show ALL of the unfinished "not-done" catagories
0
 
Ray PaseurCommented:
I'm still not sure I understand exactly what the central issue was - there were many peripheral issues that seemed to distract and we never got any test data, so it was nigh impossible to show any useful (tested) code examples.  

Just guessing, but if the central issue was "I want my clients input to be remembered so they don't have to fill out the form in one sitting," then the design pattern is a shopping cart (where you would never want to lose the client input).  You might want to post a question about that separately.

Best of luck with the project, and don't be reluctant to post an exploratory question -- you don't get any extra credit for banging your head against a wall when there is a community of experts who can help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 9
  • 8
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now