hi,

i need a way to allow my users to replace placeholders with data from my recordset in mysql.

situation is:

  1. the create a letter and have keywords defined from my recordset columns such as {Name}, {DOB}, {Email} and so on.
  2. currently i can generate the ltter but i need a way to replace all columns like above with the values from my recordset query on page.

issue is i cannot do it manually for each column as one there is far too many and two there maybe additional fields added and it will be used for other tables with big tables.

a developer had given me a piece of code that seemd to work before which is preg_replace? but this is not happening now and need a way to mass change all columns like above with the correct values from query.

many thanks

if (isset($_POST['letterEdit'])&& ($_POST['letterEdit']!='')){
        $letterBody = $_POST['letterEdit'];
        preg_match_all('/{([a-zA-Z]|[0-9]|_)*}*/i',$letterBody,$words);
        $alabala = $words[0]; 
        $patterns = $words[0];
        $i = 1;
        $select = '';
        $alabala = array_unique($alabala);
        $counted = count($alabala);
        foreach($alabala as $value){ 
        //echo "i = $i ,counted = $counted <br/>";       
          $value = trim($value, '{,}');
          if($value != 'todaysDate'){
            if($i === $counted){
              $select .= $value;
            }else{
              $select .= $value.', ';
            }
          }else{
            if($i === $counted){
              $select .= " DATE_FORMAT(NOW() ,'%W, %D of %M %Y, %h:%i:%S %p') AS `todaysDate` ";
            }else{
              $select .= "DATE_FORMAT(NOW() ,'%W, %D of %M %Y, %h:%i:%S %p') AS `todaysDate`,";
            }
          }
          $i++;  
        }
        // echo $select;
    }
    require_once('Connections/cbank.php');
    //print_r($_POST);
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }

      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }


    $colname_complaint_rs = "-1";
    if (isset($_POST['complaitId'])) {
      $colname_complaint_rs = $_POST['complaitId'];
    }
    mysql_select_db($database_cbank, $cbank);
    $query_complaint_rs = "SELECT $select FROM clientscomp WHERE complaitId = ".GetSQLValueString($colname_complaint_rs, "int");
    //echo $query_complaint_rs;
    $complaint_rs = mysql_query($query_complaint_rs, $cbank) or die(mysql_error());
    $row_complaint_rs = mysql_fetch_assoc($complaint_rs);
    $totalRows_complaint_rs = mysql_num_rows($complaint_rs);
    //echo "<pre>";print_r($alabala);echo "</pre>"; 
    //echo "<pre>";print_r($row_complaint_rs);echo "</pre>"; 
    $letterBodyNew = preg_replace('/}/','',preg_replace('/{/','',preg_replace($alabala,$row_complaint_rs,$letterBody)));

    $insertSQL = sprintf("INSERT INTO lettersent (Letters_LetterID, SentDate, Title, Content, clients_ClientID) VALUES (%s, NOW(), %s, %s, %s)",
                       GetSQLValueString($_POST['letterId'], "int"),
                       GetSQLValueString($_POST['letterTitle'], "text"),
                       GetSQLValueString($letterBodyNew, "text"),
                       GetSQLValueString($_POST['complaitId'], "int"));

    mysql_select_db($database_cbank, $cbank);
    $Result1 = mysql_query($insertSQL, $cbank) or die(mysql_error());   
<?php 
echo $letterBodyNew; 
?>

Based on $row_complaint_rs I'd use a loop to create 2 arrays containing the placeholders and the matching values, and pass those to preg_replace.

Hi there. Can. You expand please as not sure how i would impliment this to swap the query values.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.