stdfr33 stdfr33 - 2 months ago 12
PHP Question

Cakephp persistant line in output

I have the code below in a php form that I created. I cannot figure out how to make it so that the output file has the PartColor: line even if nothing is selected from shoe_sizes or color_value.

Currently it adds all the size lines to the output file even if there is no data but the PartColor: line isn't being added when no choice is made.

foreach ($data['Saleslead']['style_numbers'] as $key => $value) {
$comma_delmited_list .= "---- Start Product ----\n";
$comma_delmited_list .= "\n";

$color_value = $data['Saleslead']['colors'][$key];
$shoe_sizes = $data['Saleslead']['shoe_sizes'][$key];
$part_number = $style_number_complete_list[$data['Saleslead']['style_number_id'][$key]]['name'];
$color_range = $style_number_complete_list[$data['Saleslead']['style_number_id'][$key]]['color_range'];
$comma_delmited_list .= "PartNumber: ".$part_number."\n";
$comma_delmited_list .= "PartColorRange: ".$color_range."\n";

if($shoe_sizes != "" || $color_value != ""){
$texttoshow = "";
if($shoe_sizes != "" && $color_value != ""){
$texttoshow = $shoe_sizes;
}else if ($shoe_sizes != "" && $color_value == ""){
$texttoshow = $shoe_sizes;
}else if ($shoe_sizes == "" && $color_value != ""){
$texttoshow = $color_value;
}
$comma_delmited_list .= "PartColor: ".$texttoshow."\n";
}

$comma_delmited_list .= "cur_UnitPriceUserEntered: ".$data['Saleslead']['unit_price'][$key]."\n";
$comma_delmited_list .= "OrderInstructions: ".$data['Saleslead']['order_instruction'][$key]."\n";
$comma_delmited_list .= "Size01_Req: ".$data['Saleslead']['small'][$key]."\n";
$comma_delmited_list .= "Size02_Req: ".$data['Saleslead']['medium'][$key]."\n";
$comma_delmited_list .= "Size03_Req: ".$data['Saleslead']['large'][$key]."\n";
$comma_delmited_list .= "Size04_Req: ".$data['Saleslead']['xlarge'][$key]."\n";
$comma_delmited_list .= "Size05_Req: ".$data['Saleslead']['2xlarge'][$key]."\n";
$comma_delmited_list .= "Size06_Req: ".(isset($data['Saleslead']['other'][$key])?$data['Saleslead']['other'][$key]:"")."\n";
$comma_delmited_list .= "\n";
$comma_delmited_list .= "---- End Product ----\n";
$comma_delmited_list .= "\n";


Here is what text looks like after the form is submitted.

PartNumber: 00021

PartColorRange: Darks

cur_UnitPriceUserEntered: 21

OrderInstructions:

Size01_Req: 21

Size02_Req:

Size03_Req:

Size04_Req:

Size05_Req:

Size06_Req:

And I need PartColor: beneath PartColorRange:

Thanks for all the help!

UPDATE* Answer by bill below.

if($shoe_sizes != "" || $color_value != ""){
$texttoshow = "";
if($shoe_sizes != "" && $color_value != ""){
$texttoshow = $shoe_sizes;
}else if ($shoe_sizes != "" && $color_value == ""){
$texttoshow = $shoe_sizes;
}else if ($shoe_sizes == "" && $color_value != ""){
$texttoshow = $color_value;
}
$comma_delmited_list .= "PartColor: ".$texttoshow."\n";
} else {
$comma_delmited_list .= "PartColor: \n";
}

Answer

It's because you are only adding the PartColor: string if $shoe_sizes or $color_value aren't empty. You could add an else block after checking if they are empty if you want PartColor: to show regardless.

if($shoe_sizes != "" || $color_value != "") {
    // your code
} else {
     $comma_delmited_list .= "PartColor: \n";
}
Comments