I'm looking for a way to concatenate a string being processed through a for loop.

Basically, I'm passing a variable into an array (using explode). This array can have anywhere from 1 to inf items in it. Currently, I'm passing the array through a for loop and echoing the data from there.

Data comes in form "2,4,8,6,", so an array $explodeResults[$l] is created. I loop based on another variable which knows how many numbers were included in this variable. The loop echos a simple statement "Dice $n was of value $explodeResults[$l]" and then repeats for as many as needed.

I want the information to be displayed like this:

Dice 1 was 2<br>
Dice 2 was 4<br>
Dice 3 was 8<br>
Dice 4 was 6

But have that all concatenated into another string value. The other string value would be like "You rolled 4d8 with a modifier of +0 and the total was 20"

The end result should be:

You rolled 4d8 with a modifier of +0 and the total was 20.

Dice 1 was 2
Dice 2 was 4
Dice 3 was 8
Dice 4 was 6

Is there a way to do what I'm asking. If further clarification is needed, please let me know. Thank you.

    there is definately a way post the code that you've done so far and I'll see if I can help ya out.

      Here. Some of it won't be relevant to the issue at hand, but I'll post it anyways.

      <?php
      	// Purpose: Get the passed variables from dice script
      	//          and parse them to readable data, then post
      	//		to the thread.
      
      mysql_connect("localhost", "dbuser", "dbpass");
      mysql_select_db("superwad_invision");
      
      $n		= $_GET['n'];
      $dx		= $_GET['dx'];
      $mod		= $_GET['mod'];
      $pm		= $_GET['plusmin'];
      $total		= $_GET['total'];
      $results                   = $_GET['results'];
      $ipbcookie	= $_COOKIE['invisionmember_id'];
      $postTime	= date("U");
      
      // Place the results into an array for easier display.
      
      $explodeResults = explode(",", $results);
      
      // Set modifier variable
      
      if ($pm == '0')
      {
      	$plmi = "+";
      }
      elseif ($pm == '1')
      {
      	$plmi = "-";
      }
      
      // Is the user a guest?
      
      if ($ipbcookie == '0')
      {
      	$guest = '1';
      }
      else
      {
      	$guest = '0';
      }
      
      // Get username from cookie information if not a guest
      
      if ($guest == '0')
      {
      	$result = mysql_query("SELECT name from ibf_members WHERE id = $ipbcookie");
      	$row = mysql_fetch_row($result);
      	$userName = $row[0];
      
      $postString = "$userName rolled ". $n ."d". $dx . " with a modifier of ". $plmi ."". $mod. " and the total was $total.";
      
      for ($l = 0; $l < $n; $l++)
      {
      	$res = $l + 1;
      	echo ("Dice $res was a $explodeResults[$l]<br>");
      }
      
      $result1 = mysql_query("SELECT posts FROM ibf_forums WHERE id = '16'");
      $row1 = mysql_fetch_row($result1);
      $posts1 = $row1[0];
      $updatePosts1 = $posts1 + 1;
      
      mysql_query("UPDATE `ibf_forums` SET `posts` = '$updatePosts1', `last_post` = '$postTime', `last_poster_id` = '10', `last_poster_name` = 'DiceBot', `last_title` = 'Rolling Thread', `last_id` = '31' WHERE `id` = '16'");
      
      $result2 = mysql_query("SELECT posts,ip_address FROM ibf_members WHERE id = '10'");
      $row2 = mysql_fetch_row($result2);
      $posts2 = $row2[0];
      $updatePosts2 = $posts2 + 1;
      
      mysql_query("UPDATE `ibf_members` SET `posts` = '$updatePosts2', `ip_address` = '' WHERE `id` = '10'");
      
      $result3 = mysql_query("SELECT posts FROM ibf_topics WHERE tid = '31'");
      $row3 =	mysql_fetch_row($result3);
      $posts3 = $row3[0];
      $updatePosts3 = $posts3 + 1;
      
      mysql_query("UPDATE `ibf_topics` SET `posts` = '$updatePosts3', `last_poster_id` = '10', `last_post` = '$postTime', `last_poster_name` = 'DiceBot' WHERE `tid` = '31'");
      mysql_query("INSERT INTO `ibf_posts` (`pid`, `append_edit`, `edit_time`, `author_id`, `author_name`, `use_sig`, `use_emo`, `ip_address`, `post_date`, `icon_id`, `post`, `queued`, `topic_id`, `forum_id`, `attach_id`, `attach_hits`, `attach_type`, `attach_file`, `post_title`, `new_topic`, `edit_name`) VALUES ('', '0', NULL, '10', 'DiceBot', '1', '1', '127.0.0.1', '$postTime', '0', '$postString', '0', '31', '16', '', '0', '', '', NULL, '0', NULL)");
      }
      else
      {
      	echo ("Sorry, guests cannot use this feature.  Please login in order to roll the dice.");
      }
      
      ?>

        Hey there. Just wondering if you've made any progress on this.

          So, if I understand, these lines produce the output you want, but you like it all contained in one string variable?

          $postString = "$userName rolled ". $n ."d". $dx . " with a modifier of ". $plmi ."". $mod. " and the total was $total.";
          for ($l = 0; $l < $n; $l++)
              {
                  $res = $l + 1;
                  echo ("Dice $res was a $explodeResults[$l]<br>");
          } 

          If that's the case, try this:

          $postString = "$userName rolled ". $n ."d". $dx . " with a modifier of ". $plmi ."". $mod. " and the total was $total.";
          $newString=$postString."<br />";
          for ($l = 0; $l < $n; $l++)
              {
                  $res = $l + 1;
                  $newString+="Dice $res was a $explodeResults[$l]<br>";
          }
          
          echo $newString; 

          or you could not bother with $newString and just use $postString+="Dice $res...

            Hmm...

            I tried that code, but for some reason $newString contains a "0" and nothing more...

              DUH!!! {brain fart}

              $postString = "$userName rolled ". $n ."d". $dx . " with a modifier of ". $plmi ."". $mod. " and the total was $total."; 
              $newString=$postString."<br />"; 
              for ($l = 0; $l < $n; $l++) 
                  { 
                      $res = $l + 1; 
                      $newString.="Dice $res was a $explodeResults[$l]<br>";  // NOTE THE CHANGE HERE!!!
              } 
              
              echo $newString;
              

                You sire, are awesome!

                  Write a Reply...