LB1234
asked on
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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.
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.
ASKER
ok let me read that and repost this question according to SSCCE. Sorry guys. :(
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I mean not rePOST the question but restate it.
:-)
We will be here!
We will be here!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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.
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.
ASKER
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).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The field done never changes in your loop - it is always the same.
don't understand why the word selected is being added to every lineThat'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.
Ray,
The word selected is being added to every line because it's hard coded in the sprintf() statement
The word selected is being added to every line because it's hard coded in the sprintf() statement
ASKER
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>
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I still don't understand the relationship of DONE to CATEGORY?
ASKER
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");?>
What happens now?
ASKER
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.
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?
"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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
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.
OK, if you won't post the test data, I'm out. Good luck, ~Ray
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
I hope this give you some idea, but I really think you need a different GUI than the dropdowns
$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>
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!
ASKER