dsjojts9734 2015-01-03 21:29
浏览 46

如何将'string'转换为$ VariableName不用于调试

I'm trying to make an array to insert the data of that array in a table(2) of the database, let me introduce you to the problem.

The thing is I want this array (since now called $myArray) be constructed from another for the mix of another two or three arrays, which are $arBase, $arPost and $dPost.

$arBase = array(
            'title',
            'Prelude',
            'Text',
            'nombr1',
            'nombr2',
            'nombr3',
            'nombr4',
            'regin',
            'ctgry',
            'RutIm'
                );

 $arPost = array(
    $_POST['Titulo2'],
    $_POST['Preludio2'],
    $_POST['Comentario2'],
    $_POST['NOMBR1(2)'],
    $_POST['NOMBR2(2)'],
    $_POST['NOMBR3(2)'],
    $_POST['NOMBR4(2)'],
    $_POST['regin(2)'],
    $_POST['ctgry2'],
    $_POST['RutIm']
                );

    $smr = $smt->fetch(); //fetch of table(1) different to the query of table(2) showed ahead                   

 $dPost = array(
        $smr['Title'],
        $smr['Prelude'],
        $smr['Text'],
        $smr['NOMBR1'],
        $smr['NOMBR2'],
        $smr['NOMBR3'],
        $smr['NOMBR4'],
        $smr['REGIN'],
        $smr['CTGRY'],
        $smr['RutIm']
                );

 $miArray = array();


 echo '<br /><br />ArBase: ';
 var_dump($arBase);
 echo '<br /><br />ArPost: ';
 var_dump($arPost);
 echo '<br /><br />dPost: ';
 var_dump($dPost);

This is actually going to print this:

ArBase: array(10) { 
[0]=> string(5) "title" 
[1]=> string(7) "Prelude" 
[2]=> string(4) "Text" 
[3]=> string(6) "nombr1" 
[4]=> string(6) "nombr2" 
[5]=> string(6) "nombr3" 
[6]=> string(6) "nombr4" 
[7]=> string(5) "regin" 
[8]=> string(5) "ctgry" 
[9]=> string(5) "RutIm" }

ArPost: array(10) { 
[0]=> string(15) "llegar y Llevar" // This was the only filled in the $_POST[''] in the form
[1]=> string(0) "" 
[2]=> string(0) "" 
[3]=> string(0) "" 
[4]=> string(0) "" 
[5]=> string(0) "" 
[6]=> string(0) "" 
[7]=> string(0) "" 
[8]=> string(0) "" 
[9]=> NULL }

dPost: array(10) { // this ones come from another table(1) in the database
[0]=> string(20) "Vas A VIVIV CALABERA" 
[1]=> string(85) "tener con vos me miro fijo y volvi" 
[2]=> string(37) "ella me dijo 4 " 
[3]=> string(3) "The" 
[4]=> string(7) "Name" 
[5]=> string(6) "of" 
[6]=> string(8) "Somebody" 
[7]=> string(13) "Metropoli" 
[8]=> string(5) "Ctgry" 
[9]=> string(23) "images/padlock2.gif.jpg" }

The idea is simple, I want to make $myArray be build selectively, this means that, if data was sent through $_POST, then the value of that $_POST will be the data of that specific value in the $myArray, but, if this was not set (and because of that, is "" or have just " ") then it make a query in the table(1) and add the specific content of that field enter in the corresponding value of $myArray.

Please have in mind for the future than table(1) and table(2) are almost clones but are not the same table.

Until here, all is fine, but then I encountered a problem:

//This is the only table category difference between the two tables
$date =  $smr['Date']; //the fetch for the Date value of the table(1)
        $timestamp = date('Y-m-d H:i:s', strtotime($date)); //Here is converted in Timestamp to be inserted on table(2)

 // Say hi to table(2)
 $qls = "INSERT INTO `u739462586_dtren`.`chcknw` 
 (Title, Prelude, Text, Time, `NOMBR1`, `NOMBR2`, `NOMBR3`, `NOMBR4`, `REGIN`, `CTGRY`, `RutIm`) 
 VALUES (:Title, :Prelude, :Text, :Time, :NOMBR1, :NOMBR2, :NOMBR3, :NOMBR4, :REGIN, :CTGRY, :RutIm)";

 $stmt = $con->prepare($qls);
 $stmt->bindParam(':Title', $title, PDO::PARAM_STR);
 $stmt->bindParam(':Prelude', $Prelude, PDO::PARAM_STR);
 $stmt->bindParam(':Text', $Text, PDO::PARAM_STR);
 $stmt->bindParam(':Time', $timestamp, PDO::PARAM_STR);
 $stmt->bindParam(':NOMBR1', $nombr1, PDO::PARAM_STR);
 $stmt->bindParam(':NOMBR2', $nombr2, PDO::PARAM_STR);
 $stmt->bindParam(':NOMBR3', $nombr3, PDO::PARAM_STR);
 $stmt->bindParam(':NOMBR4', $nombr4, PDO::PARAM_STR);
 $stmt->bindParam(':REGIN', $regin, PDO::PARAM_STR);
 $stmt->bindParam(':CTGRY', $ctgry, PDO::PARAM_STR);
 $stmt->bindParam(':RutIm', $RutIm, PDO::PARAM_STR);
 $rslt = $stmt->execute();

The variables used to bind inside the second table(2) have just the same names than the strings inside the $arBase variable, because I'm trying to make something like:

// of course this code is not gonna work, but is usefull because 'ilustrate' what i'm trying to do 
foreach ($myArray[]) {
        if (isset($arPost[]) && $arPost[] != "") {
                $miArray[] = '$'.$arBase[] => $arPost[]; //a bad attempt to transform the string content of $arBase in a variable name
            } else {
                $miArray[] = '$'.$arBase[] => $dPost[];
            }
        }

I want make something like ($myArray as $arBase => $arPost/$dPost). Of course this is a bad attempt of pseudocode, but it shows the idea, $arBase's 'strings' converted in $VariablesNames so, that way they can been assigned with the $arPost content or the $dPostcontent depending of if (isset($arPost[]) && $arPost[] != "") or not.

Taking in consideration the examples just before, after all the process var_dump($myArray); must return something like this:

$myArray: array(10) { 
    $title => string(5) "llegar y Llevar"
    $Prelude => string(7) "tener con vos me miro fijo y volvi"
    $Text => string(4) "ella me dijo 4 "
    $nombr1 => string(6) "The" 
    $nombr2 => string(6) "Name" 
    $nombr3 => string(6) "of"  
    $nombr4 => string(6) "Somebody" 
    $regin => string(5) "Metropoli" 
    $ctgry => string(5) "Ctgry" 
    $RutIm => string(5) "images/padlock2.gif.jpg" }

Any idea how to accomplish this?

Please feel free to ask or suggest modifications to the question itself, or to know more about the problem, thanks in advance

  • 写回答

1条回答 默认 最新

  • doujiao1949 2015-01-03 21:40
    关注

    It seems to me you are overcomplicating things: You don't need to read the original values to re-insert everything again.

    Instead, you should build a query dynamically, adding just the keys / values that you need to change. And as you are just sending strings and the type does not seem that important, you can send an array with values to the execute() method instead of binding the individual variables.

    As far as I can see, you also need an sql UPDATE statement instead of an INSERT unless you remove the existing row first, which would make things even more unnecessary complicated. To accomplish that, you would have to add the ID to the post.

    The result would look something like:

    // add the first variable that you always need to set or only add a comma in the loop
    // if the added value is not the first. See note at the bottom
    $qls = "UPDATE `u739462586_dtren`.`chcknw` SET id=id";
    $values = array();
    foreach ($_POST as $key => $value)
    {
      // make sure you only add existing keys, perhaps you have a list
      // available already? Also only add non-empty values
      if (in_array($key, $existing_keys) && !empty($value))
      {
        $qls .= ", `{$key}` = :{$key}";
        $values[":{$key}"] = $value;
      }
    }
    // add a WHERE condition to update the right record
    $qls .= " WHERE id=:id";
    // You should send the correct ID with the post
    $values[":id"] = $_POST['id'];
    $stmt = $con->prepare($qls);
    $rslt = $stmt->execute($values);
    

    Note: I did a bit of an ugly hack to accommodate for the commas when building the query, you could also build an array of strings and implode that but that's an easy change to make yourself.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?