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 $dPost
content 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