Jump to content

Need help with a bit of logic


dc_jt

Recommended Posts

Ive got address 1-5 fields listed on separate lines (they have to be like this) but I need to make sure there are no gaps in the lines. Therefore, if there is no address 1, this shows address 2, if there is no address 2 this shows address 3 etc. I cant work out the logic, I tried this but it doesnt work if there is no address 2 AND no address 3. Any ideas?

 

if($add1 == ""){

$add1 = $add2;
$add2 = $add3;
$add3 = $add4;
$add4 = $add5;
$add5 = '';
}

if($add2 == ""){
$add2 = $add3;
$add3 = $add4;
$add4 = $add5;
$add5 = '';
}

if($add3 == ""){
$add3 = $add4;
$add4 = $add5;
$add5 = '';
}

if($add4 == ""){
$add4 = $add5;
$add5 = '';
}

Link to comment
https://forums.phpfreaks.com/topic/179355-need-help-with-a-bit-of-logic/
Share on other sites

Thanks for the quick reply. The thing is I cant just separate them with a line break. I am using placeholders on a pdf document so I need to set address 1 to a value, address 2 to a value etc so there is no gap. I.e

 

Address 1

 

 

Address 4

Address 5

 

This is how it is coming out now because for example, address 2 and 3 are not set a value. It doesnt matter if address 5 doesnt have a value because it has moved upto address 5 becausse there will be nothing below that. Does that make sense?

 

Thanks

The last line was simply to show that the gaps are filled.

 

array[0], array[1].... etc will each hold a line of the address, minus any blank lines. If only four lines have values, then there will only be elements up till array[3]. Use this however you like to fill in your PDF placeholders.

 

$line1 = isset($array[0]) ? $array[0] : "";
$line2 = isset($array[1]) ? $array[1] : "";
$line3 = isset($array[2]) ? $array[2] : "";
... etc etc 

Nope still havent got it. This is my code:

 

$add1 = "address1";
$add2 = "address2";
$add3 = "address3";
$add4 = "address4";
$add5 = "address5";

$address = array($add1, $add2, $add3, $add4, $add5);
$array = array_diff($address, array(""));

$add1 = isset($array[0]) ? $array[0] : "";
$add2 = isset($array[1]) ? $array[1] : "";
$add3 = isset($array[2]) ? $array[2] : "";
$add4 = isset($array[3]) ? $array[3] : "";
$add5 = isset($array[4]) ? $array[4] : "";

print_r($array);
print("<br />");print("<br />");
print($add1);
print("<br />");
print($add2);
print("<br />");
print($add3);
print("<br />");
print($add4);
print("<br />");
print($add5);
print("<br />");

Array ( [0] => address1 [3] => address3[4] => address4)

address1 


address3
address4

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.