user3258571 user3258571 - 3 months ago 10
PHP Question

Selected in Option Menu when Items Have Hyphens

I have an option menu that I would like to show selected when reading recorded choice from a table. Items in table have hyphens (e.g. Volvo - 3638, Toyota - 92131). Problem is I am using strcmp to echo selected on stored value. While it stores the value in the table, I cannot echo selected on value when I create an option menu to change the option menu choice from previously stored value. I have tried == to no avail. I'm guessing it is the hyphen. Is there a solution where I can keep the hyphens and echo selected? Here is my option menu:

<input type="text" name="Activity" class="form-control" id="Activity" value="<?php echo htmlentities($row_Recordset1['Activity'], ENT_COMPAT, 'utf-8'); ?>" required <?php
if($row_RecordsetTrans['SUM(`transaction`.quantity)'] >0){
echo 'readonly';
}

?>>
</div>
<div class="form-group">
<label for="Account">Account*</label>

<select class="form-control" name="Account" id="Account" required <?php
if($row_RecordsetTrans['SUM(`transaction`.quantity)'] >0){
echo 'readonly';
}else{
echo 'text';
}

?>>
<option value="" <?php if (!(strcmp("", $row_Recordset1['Account']))) {echo "selected=\"selected\"";} ?>>select account</option>
<?php
do {
?>
<option value="<?php echo $row_Accounts['acctname']?>"<?php if (!(strcmp($row_Accounts['acctname'], $row_Recordset1['Account']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Accounts['acctname']?></option>
<?php
} while ($row_Accounts = mysql_fetch_assoc($Accounts));
$rows = mysql_num_rows($Accounts);
if($rows > 0) {
mysql_data_seek($Accounts, 0);
$row_Accounts = mysql_fetch_assoc($Accounts);
}
?>
</select>

Answer

Turns out my code above was correct. The problem was in my imported data. I was using:

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE accounts FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r' IGNORE 1 LINES

As it turns out, the loaded data was getting a line break in the acctname field. So strcmp would not be able to compare the data.

The fix was to add \n on the Terminated by to remove the line break:

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE accounts FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES