skip to Main Content

The php code.

    public function GetProduct($id, $bc = '')
{
    $productname = WHMCSProductProduct::getProductName($id);
    $description = WHMCSProductProduct::getProductDescription($id);
    $details = WHMCSProductProduct::find($id);
    $pricing = $details->pricing();
    if ($bc == '') {
        $price = $pricing->first();
    } else {
        $price = $pricing->byCycle($bc);
        if (is_null($price)) {
            $price = $pricing->first();
        }
    }
    return array(
        'name' => $productname,
        'description' => $description,
        'price' => $price,
    );
}

Output when i am using {$description} variable in tpl file.

Disk Space: 1000 MB
Bandwidth: 10,000 MB
Email Accounts: 25
Subdomains: 100
MySql DataBase: UNLIMITED

This is test information for products

Screenshot from phpMyAdmin

I want create multiple variables from this output to get below result so i can use like below 3 variables in tpl file.

$feature =  Disk Space
$value = 100 MB 

etc..

and also 
$feature.description = This is test information for products.

If this is an unclear question, let me know and I will attempt to clear it up.

Thanks

2

Answers


  1. I’m sure this can be done cleaner but it’ll get the job done:

    $description = "Disk Space: 1000 MB
    Bandwidth: 10,000 MB
    Email Accounts: 25
    Subdomains: 100
    MySql DataBase: UNLIMITED
    
    This is test information for products,
    with a multiline description";
    
    $descArr = explode("rn", $description);
    $lineCount = 1;
    $desc = "";
    foreach ($descArr as $line)
    {
        if ($lineCount <= 5)
        {
            list($key, $val) = explode(":", $line);
            $val = trim($val);
            echo "key: [" . $key . "] n";
            echo "val: [" . $val . "] nn";
        }
        else
        {
            $desc .= $line . "n";
        }
        $lineCount++;
    }
    
    echo "desc:" . $desc . "n";
    

    Output:

    key: [Disk Space]
    val: [1000 MB]
    
    key: [Bandwidth]
    val: [10,000 MB]
    
    key: [Email Accounts]
    val: [25]
    
    key: [Subdomains]
    val: [100]
    
    key: [MySql DataBase]
    val: [UNLIMITED]
    
    desc:
    This is test information for products,
    with a multiline description
    

    I just echod out the pairs of key/value. You can store them into variables if you want of course.


    If you want the key/val pair stored in variables:

    $description = "Disk Space: 1000 MB
    Bandwidth: 10,000 MB
    Email Accounts: 25
    Subdomains: 100
    MySql DataBase: UNLIMITED
    
    This is test information for products,
    with a multiline description";
    
    $descArr = explode("rn", $description);
    
    $keys = $vals = [];
    $lineCount = 0;
    $desc = "";
    
    foreach ($descArr as $line)
    {
        if ($lineCount < 5)
        {
            list($key, $val) = explode(":", $line);
            $val = trim($val);
            $keys[$lineCount] = $key;
            $vals[$lineCount] = $val;
        }
        else
        {
            $desc .= $line . "n";
        }
        $lineCount++;
    }
    
    var_dump($keys); 
    var_dump($vals); 
    echo "desc:" . $desc . "n";
    

    Output:

    array(5) {
      [0]=>
      string(10) "Disk Space"
      [1]=>
      string(9) "Bandwidth"
      [2]=>
      string(14) "Email Accounts"
      [3]=>
      string(10) "Subdomains"
      [4]=>
      string(14) "MySql DataBase"
    }
    array(5) {
      [0]=>
      string(7) "1000 MB"
      [1]=>
      string(9) "10,000 MB"
      [2]=>
      string(2) "25"
      [3]=>
      string(3) "100"
      [4]=>
      string(9) "UNLIMITED"
    }
    desc:
    This is test information for products,
    with a multiline description
    

    $keys & $vals are parallel arrays. This all is based on the format of five lines at the top of $description with key:value being the format.

    Login or Signup to reply.
  2. if the result string is same as in your question you can simply separate them by new line and then explode by :. here is code.

    $str = 'Disk Space: 1000 MB
    Bandwidth: 10,000 MB
    Email Accounts: 25
    Subdomains: 100
    MySql DataBase: UNLIMITED';
    
    $arr = preg_split('/rn|r|n/', $str);
    $new = array();
    foreach($arr as $key=>$a){
        $temp = explode(':', $a);
        $new[$key]['feature'] = trim($temp[0]);
        $new[$key]['value'] = trim($temp[1]);
    }
    echo '<pre>';
    print_r($arr);
    echo '<br>';
    print_r($new);
    

    Output:

    Array
        (
        [0] => Disk Space: 1000 MB
        [1] => Bandwidth: 10,000 MB
        [2] => Email Accounts: 25
        [3] => Subdomains: 100
        [4] => MySql DataBase: UNLIMITED
    )
    
    Array
        (
        [0] => Array
        (
            [feature] => Disk Space
            [value] => 1000 MB
        )
    
        [1] => Array
        (
            [feature] => Bandwidth
            [value] => 10,000 MB
        )
    
        [2] => Array
        (
            [feature] => Email Accounts
            [value] => 25
        )
    
        [3] => Array
        (
            [feature] => Subdomains
            [value] => 100
        )
    
        [4] => Array
        (
            [feature] => MySql DataBase
            [value] => UNLIMITED
        )
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search