Solved

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

Posted on 2014-01-30
37
209 Views
Last Modified: 2014-04-29
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
Comment
Question by:LB1234
  • 10
  • 9
  • 8
  • +2
37 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 207 total points
ID: 39821072
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 168 total points
ID: 39821255
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
 
LVL 1

Author Comment

by:LB1234
ID: 39821263
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 168 total points
ID: 39821279
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 207 total points
ID: 39821286
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39821289
@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
 
LVL 1

Author Comment

by:LB1234
ID: 39821305
ok let me read that and repost this question according to SSCCE.  Sorry guys. :(
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 42 total points
ID: 39821315
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
 
LVL 1

Author Comment

by:LB1234
ID: 39821319
I mean not rePOST the question but restate it.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39821328
:-)

We will be here!
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 168 total points
ID: 39821339
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39821370
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
 
LVL 1

Author Comment

by:LB1234
ID: 39821432
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
 
LVL 1

Author Comment

by:LB1234
ID: 39821437
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 168 total points
ID: 39821474
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
 
LVL 58

Expert Comment

by:Gary
ID: 39821475
The field done never changes in your loop - it is always the same.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39821619
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
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39821634
Ray,

The word selected is being added to every line because it's hard coded in the sprintf() statement
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:LB1234
ID: 39821646
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39821649
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 207 total points
ID: 39821659
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
 
LVL 58

Expert Comment

by:Gary
ID: 39821716
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
 
LVL 1

Author Comment

by:LB1234
ID: 39822174
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 207 total points
ID: 39822203
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
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 83 total points
ID: 39822226
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
 
LVL 58

Expert Comment

by:Gary
ID: 39822245
I still don't understand the relationship of DONE to CATEGORY?
0
 
LVL 1

Author Comment

by:LB1234
ID: 39822253
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
 
LVL 58

Expert Comment

by:Gary
ID: 39822272
What happens now?
0
 
LVL 1

Author Comment

by:LB1234
ID: 39822275
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
 
LVL 33

Expert Comment

by:Slick812
ID: 39822306
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 207 total points
ID: 39822308
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
 
LVL 33

Expert Comment

by:Slick812
ID: 39822335
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
 
LVL 1

Author Comment

by:LB1234
ID: 39822378
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39822774
OK, if you won't post the test data, I'm out.  Good luck, ~Ray
0
 
LVL 33

Expert Comment

by:Slick812
ID: 39823032
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 83 total points
ID: 39823041
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39860368
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now