About adding + after each added item

Hello,

I couldn’t find any idea how to do it

$quattro can have one or two or three or four or five values
i want to add + after each value, but it won’t be at the beginning and the end
Sample.
4IF+MDU+RF,
8IF+MDU
RF
12IF+MDU+RF
16IF+RF
MDU+RF
Priority order
The first should be “xIF”, the next should be “MDU”, the end should be “RF”

$ifnumber = 0;
$mdu = "";
$rf = "";

$quatro[] = 4; // there will be variables with different names
$quatro[] = 4; // there will be variables with different names
//$quatro[] = 4; // there will be variables with different names
$quatro[] = 4; // there will be variables with different names
//$quatro[] = "MDU";
$quatro[] = "RF";

foreach($quatro AS $value){
    if(is_numeric($value)){
        $ifnumber += $value;
    }elseif($value == "MDU"){
        $mdu = "MDU";
    }elseif($value == "RF"){
        $rf = "RF";
    }
}

if(is_numeric($ifnumber)){
    $ifnumber = $ifnumber."IF";
}

Thank you

Not sure what you are asking, but, this might work:

$results = "";
foreach($quatro AS $value) {
    If($value="MDU" OR "RF") {
        $results .= $value . "+";
    } else {
        $results .= $value . "IF+";
    }
}
$results = substr($results, 0, strlen($results)-1);    // Remove trailing +

It just keeps MDU or RF or the number with IF and pluses between each…

Result: MDU+MDU+MDU+MDU+MDU+MDU
Each item must be added one
The sample output will be as in the example below
Only if 4, MDU and RF are active: 4IF+MDU+RF,
Only if 4+4=8 and MDU is active: 8IF+MDU
If only RF is active: RF
Only if 4+4+4=12, MDU and RF are active: 12IF+MDU+RF
Only if 4+4+4+4=16 and RF is active: 16IF+RF
If only MDU and RF are active: MDU+RF

$ifnumber = 0;
$mdu = "";
$rf = "";

//$quatro[] = 4;
//$quatro[] = 4;
$quatro[] = 4;
$quatro[] = 4;
$quatro[] = "MDU";
//$quatro[] = "RF";

foreach($quatro AS $value){
    if(is_numeric($value)){
        $ifnumber += $value;
    }elseif($value == "MDU"){
        $mdu = "MDU+";
    }elseif($value == "RF"){
        $rf = "RF+";
    }
}
$results = "";
if(is_numeric($ifnumber)){
    $ifnumber = $ifnumber."IF+";
}
$results = $ifnumber.$mdu.$rf;
$results = substr($results, 0, strlen($results)-1);    // Remove trailing +

echo $results;

In my code, there is a missing equal sign!

    If($value="MDU" OR "RF") {

Should be:

    If($value=="MDU" OR "RF") {
1 Like

I didn’t realize it was missing equals :grinning:

Output: 4+4+MDU
If there is more than one “4” number, these numbers need to be sum.

$ifnumber = 0;
$mdu = "";
$rf = "";

//$quatro[] = 4;
//$quatro[] = 4;
$quatro[] = 4;
$quatro[] = 4;
$quatro[] = "MDU";
//$quatro[] = "RF";

$results = "";
foreach($quatro AS $value) {
    If($value == "MDU" OR "RF") {
        $results .= $value . "+";
    } else {
        $results .= $value . "IF+";
    }
}
$results = substr($results, 0, strlen($results)-1);    // Remove trailing +

echo "Output: ".$results;

What I’m basically trying to do is this:

<input type="checkbox" name="quattro[]" value="4">
<input type="checkbox" name="quattro[]" value="4">
<input type="checkbox" name="quattro[]" value="4">
<input type="checkbox" name="quattro[]" value="4">
<input type="checkbox" name="quattro[]" value="MDU">
<input type="checkbox" name="quattro[]" value="RF">

These operations will be from the admin panel.
For optical input features
It will write the values of the selected checkboxes to the database by adding “+” between them.
When displaying product it will show like 8IF+MDU+RF
I will also use it in transactions according to product features.

It will be possible to edit these values saved in the database.
That is, in the edited items fix option, checkbox will be selected and other checkboxes will not be selected.

Not: Normally there is no 13IF because I entered it manually
Ekran görüntüsü 2021-09-05 111900

Put quotes around the 4’s… \

$quatro[] = "4";

For some reason it will not mix numeric with IF text. Odd bug in the PHP system.

OR, just skip all of this silly code and do it this way:

<?PHP
$quatro[] = "4IF";
$quatro[] .= "4IF";
$quatro[] .= "8IF";
$quatro[] .= "MDU";
$quatro[] .= "RF";
$results = "";
foreach($quatro AS $value) {
    $results .= $value . "+";
}
$results = substr($results, 0, strlen($results)-1);
echo $results;
?>

Let’s use input type checkbox instead of variables anymore

You set the values of the checkboxes. Therefore, just set those values correctly and it would be a better design because it would be easier to turn into the result string.

Including your code, I got the result as I wanted with the code below

if(isset($_POST['quattro'])){
foreach($_POST['quattro'] AS $value){
    if(is_numeric($value)){
        $ifnumber += $value;
    }elseif($value == "MDU"){
        $mdu = "MDU+";
    }elseif($value == "RF"){
        $rf = "RF+";
    }
}
$results = "";
if(is_numeric($ifnumber)){
    $ifnumber = $ifnumber."IF+";
}
$results = $ifnumber.$mdu.$rf;
$results = substr($results, 0, strlen($results)-1);    // Remove trailing +
echo $results;
}

Now I have the problem of restoring it to the editor page
The question is:
Adding checkbox for those not in properties variable
Total number of checkboxes is 6
4 - 4
1 - RF
1 - MDU

$features = “8IF+MDU+RF”;
$explode = explode(’+’, $features);

foreach($explode AS $isim){

    if($isim == "MDU"){
        echo '<input type="checkbox" name="quattro[]" value="MDU" checked> MDU';
    }elseif($isim == "RF"){
        echo '<input type="checkbox" name="quattro[]" value="RF" checked> RF';
    }else{
        $ifsayi = substr($features, 0, strlen($features)-2);
        if($ifsayi == 4){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        }elseif($ifsayi == 8){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        }elseif($ifsayi == 12){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        }elseif($ifsayi == 16){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        }
    }
}

For the numeric values, why are you not using a drop-down instead of check box?
Would it not be easier to just have a drop-down with 4 / 8 / 12 / 16 in it instead of all those checkboxes?
That would make it much easier. If you keep this version, I would not pull out the first two characters.
It will cause issues. ( “8IF” would show as “8I” not “8”. ) Instead change the if’s to if ($isim==“8if”) “12IF”, etc. Faster code that way.

This never occurred to me :grinning:
Normally there are 4 polarities for each satellite 4 represents this
I chose checkbox for ease of understanding.
Satellite 1 => 4 Polarity
Satellite 2 => 4 Polarity
Satellite 3 => 4 Polarity
Satellite 4 => 4 Polarity
MDU satellite means 4 polarities combined.
RF means the frequency used for cable TV broadcasting.
Indicates which inputs and outputs optical devices support
Example: 8IF+MDU indicates that it supports two regular satellites + MDU

I got the result with the code below for editing, (amateurish ;)) )

 $features = "8IF+MDU+RF";
    $explode = explode('+', $features);

    foreach($explode AS $value){

        if($value == '4IF'){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        }
        
        if($value == '8IF'){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        }
        
        if($value == '12IF'){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4"> Quattro 1';
        }
        
        if($value == '16IF'){
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        echo '<input type="checkbox" name="quattro[]" value="4" checked> Quattro 1';
        }
    }

    if (in_array("MDU", $explode)){
        echo '<input type="checkbox" name="quattro[]" value="MDU" checked> MDU';
    }else{
        echo '<input type="checkbox" name="quattro[]" value="MDU"> MDU';
    }

    if (in_array("RF", $explode)){
        echo '<input type="checkbox" name="quattro[]" value="RF" checked> RF';
    }else{
        echo '<input type="checkbox" name="quattro[]" value="RF"> RF';
    }

It’s not amateurish if it works! Right? Sounds like you are solving it nicely.

1 Like

I encountered a problem
How do I show 4 unselected checkboxes if there is no “xIF”

if($ifsayi == 4){

I changed it as below

if($value == '4IF'){

I mentioned that part earlier because the 4 makes it a integer not a string.

( Leaving for a few hours! Talk to you later! )

I got the result I wanted with the code below

    $ifedit = array("4IF","8IF","12IF","16IF");
    $ifvarmi = array_intersect($ifedit, $explode);

    if (empty($ifvarmi)) {
	// 4 checkboxes
	}

Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service