Jump to content

PHP looping issue


vbcoach
Go to solution Solved by ginerjm,

Recommended Posts

Hello all.  I am having an issue displaying data in a form that IF EXISTS (not blank) will cause that option to be selected.  The looping itself works.  The variable called into the form works.  Just can't seem to get the form to SELECT the correct variable.

<select name="size" id="size">
              
  <?php  while ($lrow = mssqlfetchassoc($tres))  
	 { 
             ?>
              
      <option value="<?php echo $lrow['ts_id'];?>">
       <?php if($lrow['tsdescription'] == $info['size']) echo 'selected';?>
        <?php echo "$lrow[tsdescription]";?> </option>      
                    
           <?php  
		 }  
 		 ?>
    </select>

So this is displaying the team captain's TShirt size from a database (mssql).  In the database the field is called tsdescription.  For sake of argument, let's say the tshirt description is "Blue 2XLarge".  This variable exists and is passed into the form for updating by the team captain, if necessary.

 

The loop works, as it loops through all the tshirt options (107 in total) and works as a pull-down menu.  Somehow my IF statement doesn't work.  What I am looking for is for PHP to "find" the existing tshirt description 'tsdescription' in the loop statement, and if found, cause it to be "Selected".  

 

The $info are the variables for the form when the captain's information page is loaded.  So far, by itself, the $info['size'] returns the correct tshirt.  "by itself" the dropdown works.  Thus, something must be amiss in my IF statement.

 

Can anyone help direct me to what I am missing to make this work?  Thanks in advance.

 

-J

Link to comment
Share on other sites

I would suggest separating your logic (PHP) from the presentation (HTML) as much as possible. The code you have makes it difficult to debug because it switches back and forth so much. Looking at what you have, it appears the the "selected" options would end up having 'selected' put into the LABEL between the opening and closing tags as opposed to putting the selected parameter into the opening tag. So, if you are not seeing 'selected' in the drop-down list, it is because none of the DESCRIPTIONS match $info['size']. I would assume you would be saving the option VALUE and not the DESCRIPTION. So, you may also be comparing $info['size'] to the wrong value.

 

I prefer to create a function for creation my option tags that I can use for all the select fields. Here is a quick and dirty solution. This assumes you are storing the tshirt size ID for selected values and not the description (as you should be).

 

 

<?php
 
//Put all of this code at the top of your script - before you output any HTML
function createOptions($optionsAry, $selectedValue)
{
    $options = '';
    foreach($optionsAry as $optionValue => $optionLabel)
    {
        $selected = ($optionValue==$selectedValue) ? ' selected="selected"' : '';
        $options .= "<option value='{$optionValue}'{$selected}>{$optionLabel}</option>\n";
    }
    return $options;
}
 
//Convert db results into array and creation otpions HTML content
$tshirtOptionsAry = array();
while ($row = mssqlfetchassoc($tres))
{
    $tshirtOptionsAry[$row['ts_id']] = $row['tsdescription'];
}
$tshirtOptionsHtml = createOptions($tshirtOptionsAry, $info['size']);
 
?>
 
<!-- HTML content AFTER the logic is processed -->
<select name="size" id="size">
<?php echo ; ?>
</select>
Link to comment
Share on other sites

Hello Psycho, thanks for your very quick response.  I know my code is older, but I inherited it.  As I try to wrap my head around what you wrote, one thing that was apparent as I entered the code was the HTML portion seems to be mission some type of variable statement.  The <select name="size" id="size">

<?php echo ; ?>
</select>

is not actually echoing anything.  What's missing here?

Link to comment
Share on other sites

Oh, I might add that what I am looking for overall here is that if the team captain select a tshirt at the time of registration, I would like it to be "SELECTED" and displayed here.  If the field is blank, then I would like for all the tshirt options be be available to be chosen.  Hope that helps?

Link to comment
Share on other sites

<?php echo ; ?>

is not actually echoing anything.  What's missing here?

 

How about: That what should be echoed (hint: there's a variable containing HTML markup).

 

 

 

Hope that helps?

 

We know what you want. I'm not sure if you're reading the replies, though.

Link to comment
Share on other sites

I am reading the replies.  You are writing to me code that I am unfamiliar with.  When you ask me to place this information in my code to see if it works, that's what I do.  If something is missing from your example, it doesn't help me fix the issue, or learn the how and why you did it this way.  So very humbly, what is missing here?

Link to comment
Share on other sites

I haven't written any code. I've told you what's wrong with your code, but you've appearently decided to rewrite it anyway. That's fine, but then you should have a basic understanding of what that code you've copypasted does. If you don't even know what to echo, maybe you should go back to the code you do understand.

Link to comment
Share on other sites

  • Solution

Have you attempted to do any reading/learning of php so that you might glean some knowledge of your own?
 
This is how it s/b written.

$select_var = "<select name='size' id='size'>";
while ($lrow = mssqlfetchassoc($tres))
{
   if ($row['tsdescription'] == $info['size'])
       $selected = 'selected';
   else
       $selected = '';
  $select_var .= "<option value='" . $lrow['ts_id'] . "'  $selected>" .  $row['description'] ."</option>";
}
$select_var .= "</select>";



Always try and stay in php mode until you finally output your entire html doc.  Here you build the necessary dropdown into a var.  Down lower in your script where you assemble the bulk of you static and dynamic html, insert this var where it needs to be and your page will display it properly.  Don't keep spitting out pieces of html and then doing some php and then spit out some html.  It's confusing and hard to follow and even harder to maintain later on.

Edited by ginerjm
Link to comment
Share on other sites

Anyone else able to help?

 

You've been told exactly how to fix your code. On top of that, you've been given alternative code. If that's still not enough, I'm starting to doubt your willingness to work on the problem.

 

This is no We-write-your-code service. After 7 years, you should know that.

Edited by Jacques1
Link to comment
Share on other sites

The problem is that you aren't looking for help. You want others to do your job, for free.

 

While you're pretty good at begging for code and getting people to spoonfeed you, that's also the reason why you don't make any progress whatsoever. I hear you complain a lot about shitty jobs, bad code you've inherited, all kinds of limitations etc. Have you ever wondered why nobody lets you do more interesting tasks? I'll give you a hint: When somebody can't echo a variable after 7 years of programming and needs Internet forums to do that for him, then people are going to question not just his competence but also his attitude.

 

So maybe it's time to grow up and take responsibility instead of blaming everybody else and acting like a spoiled child.

Edited by Jacques1
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.