1

I have php file that generate result from user input. The result are not equals. I mean sometimes it generate 3 result and sometimes it generate 5 or 4 or 2 result. First, I define an array variable to store these result:

$resultcount = 0;
$result=array();
while(!feof($fpout3)) 
{
    $row = fgets($fpout3);
    if (!$row) {continue;}
    $element = preg_split("/\t/", $row);        
    $page = $element[0];
    $queryName = $element[1];
    $target_name = $element[7];
    $var_target = $page."\t".$element[1]."\t".$element[2];
    if(count($element) != 1) 
    {
        if($i < 10) 
        {
            echo "<tr align='center'><td align=left><input type='checkbox' id='checkedSeq' name='target[]' value='$var_target'>$target_name</td></tr>";
            fwrite($fp, "$var_target\n");

            // fill $result array
            $resultcount++;
            $result[$resultcount]=$target_name;
            //////////////////////
        }                   
    }
}

I want to send the $result array with url link to save it in the database: I already send other variables with the url and now want to pass the $result array with them:

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
echo "<input type='submit' value='Save Result'>";
echo "</form>";

saveResult.php:

<?php
$searchType = (empty($_GET['searchType'])) ? '' : $_GET['searchType'];
$querySeq = (empty($_GET['querySeq'])) ? '' : $_GET['querySeq'];
$patientIDarray = (empty($_GET['patientIDarray'])) ? '' : $_GET['patientIDarray'];
$QueryDate = date("Y-m-d");
$expect=(empty($_GET['expect'])) ? 10 : $_GET['expect'];
$wordSize = (empty($_GET['wordSize'])) ? '' : $_GET['wordSize'];

$serverName = "Alaa";
$connectionInfo = array( "Database"=>"i2b2blast", "UID"=>"i2b2blast", "PWD"=>"demouser");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Result Saved.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO BlastQueryDim (QuerySeq, DatabaseName, WordSize, ExpectThreshold, QueryDate) VALUES ('$querySeq', '$patientIDarray', '$wordSize', '$expect', '$QueryDate')";

$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}
echo "<script>
alert('Result Saved');
</script>";
?>

My question: How to pass $result array with the url? and how to get each index in the other php "saveResult"?

Thanks,

Edit1:

I type:

    echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
foreach ($result as $resultcount => $target_name)
echo "<input type='hidden' name='result[$resultcount]' value='$target_name'/>";
echo "<input type='submit' value='Save Result'>";
echo "</form>";

in saveResult.php, i type:

$result = empty($_POST['result']) ? array() : $_POST['result'];

and try to echo it and it displays: 'Array' which means that it recieve it as an empty!

Edit 2: I solve the problem by using:

$result = json_encode($_POST['result'], true); 
echo $result;

Now, the output of echo is:

{"1":"gi|1786181|gb|AE000111|ECAE000111","2":"gi|1786250|gb|AE000117|ECAE000117"}

How can I insert the second cell of each row in the database? I want to insert 'gi|1786181|gb|AE000111|ECAE000111' in a row and insert 'gi|1786250|gb|AE000117|ECAE000117' in another row

3 Answers 3

1

To pass the $result array when the form is submitted, just create hidden inputs like this:

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";

foreach ($result as $key => $value) {
    echo "<input type='hidden' name='result[$key]' value='$value'/>";
}

echo "<input type='submit' value='Save Result'>";
echo "</form>";

And in the saveResult.php get the result like this:

$result = empty($_POST['result']) ? array() : $_POST['result'];
Sign up to request clarification or add additional context in comments.

6 Comments

It sounds good. I will try it. What about the insert query? Just put: '$result'?
For insert in database you can store like JSON, use json_encode($result), and when select decode to array using $result = json_decode($json, true). I saw that you use Microsoft SQL Server, I do not know this database, if it does not have a JSON type for column you can use a TEXT type or similar.
Do you mean I can't use: "insert into TABLE (WordSize, ExpectThreshold, QueryDate, result) VALUES ('$wordSize', '$expect', '$QueryDate', '$result')"? I want to save each result in a row.
$result is a array, to save a array in a row you can convert the array to a json string, $result_json = json_encode($result), so you can use: insert into TABLE (WordSize, ExpectThreshold, QueryDate, result) VALUES ('$wordSize', '$expect', '$QueryDate', '$result_json')
No i want to save each result in a separate row
|
0

Note, if you go "echo $_POST['result']", you will always return "Array". You need to do is "print_r", to dump that array...

<?php

if(isset($_POST['result'])) {

    $sql = null;

    foreach ($_POST['result'] as $resultcount => $target_name) {
        echo $resultcount . " -> " .$target_name;
        //$sql .= "INSERT INTO table (resultcount, target) VALUES (...);";
    }

    if($sql) {
        sqlsrv_query(....);
    }

}

I will tip you again: Careful with those sql, maybe implements PDO to bind values.

3 Comments

OMG. it is working like that. Many thanks to you. I will try inserting now. if it does not work i will type here the problem.
Thanks, nice jobs!
Great! Great! Great!
0

You can send a _post as array more useful:

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
// here you is the "solution"
foreach($result as $id => $name){
    echo "<input type='hidden' name='result[".$id."]' value='".$name."'>";
}
echo "<input type='submit' value='Save Result'>";
echo "</form>";

Then in saveResult.php, you have now a $_POST['result'] as an array.

Tip: Be careful with your sql statement to avoid injections.

Edit

You now is receiving a array, now if you can retrieve that info, you just use json_encode or add a foreach again in saveResult.php:

foreach($result as $resultcount => $target_name){
    // Here, can ho your statement or sql
}

4 Comments

Add the brackets after the foreach. In saveResult.php, try print_r($result);
where to add the brackets exactly?
Before your input hidden.
it recieve it as empty. I solve it with using: $result = json_encode($_POST['result'], true); see edited post pleas. I have another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.