Jump to content
Cyjm1120

How to save three variables from different queries

Recommended Posts

Hi everyone,
 
I would like to know if:

 

 
I have three different forms to create three different mysql codes.
Now my code will generate the three mysql codes correctly but each time I tried to generate the second mysql code using another form, the previous will get wiped and therefore I can only have one mysql code working at the moment. 
 
(I apologize in advanced if my issue is not clearly stated.)

 

Attached is the code that I have right now. I am trying to get all three mysql codes (Searchsql, Filtersql, Panelsql) correct so I can pass all three mysql queries to a function where it can combine all three queries into one array using the array_intersect function.

 

Please kindly let me know your opinion on the problem and I would really appreciate the help.

$object = new Three;
class Three
{
    public $Panel, $Search, $Filter;
    function save_Three()
    {
        while($Prow = sqli_fetch_array($Panel, MYSQLI_BOTH))
        {
            $PanelName = $Prow ['name'];
        }
        while($Srow = sqli_fetch_array($Search, MYSQLI_BOTH))
        {
            $SearchName = $Srow ['name'];
        }
        while($Frow = sqli_fetch_array($Filter, MYSQLI_BOTH))
        {
            $FilterName = $Frow ['name'];
        }
        $Combined = array_intersect($Panel, $Search, $Filter);
        foreach ($Combined as $item)
            print $item;
    }
}

$Panelsql = "SELECT DISTINCT aID, name from test.article";

if (isset($_GET['pid']))
{
    $pID= $_GET['pid'];
    echo "$pID";

    $Panelsql = "SELECT DISTINCT aID, name from test.a_m, test.platform, test.message, test.article where aid in 
    (SELECT DISTINCT assocAID from test.a_m, test.platform, test.message where assocMID in
    (SELECT mID from test.message, test.platform where pID=$pID and pID = assocPID))";

}
else if (isset($_GET['mid']))
{
    $mID= $_GET['mid'];

    
    $Panelsql = "SELECT DISTINCT aID, name from a_m, message, article where aid in 
    (SELECT DISTINCT assocAID from a_m, message where mID=$mID and mID = assocMID)";
}
else
{   
    $Panelsql = "SELECT DISTINCT aID, name from article";
}

$object->Panel = $Panelsql;

$aRegions = $_POST['RegionSelected'];
$Filtersql = 'SELECT * FROM test.article';
if(isset($aRegions))
{
    $Filtersql .= ' WHERE 1 AND ('; 
    foreach ($aRegions as $word) 
    if($word==$aRegions[count($aRegions)-1])
    {
        $Filtersql .= " Region = '". $word ."')";
    }
    else
    {
        $Filtersql .= " Region = '". $word ."' OR";
    }
}
//Population filtering function
$aPopulations = $_POST['PopulationSelected'];
if(isset($aPopulations))
{
    if(!isset($aRegions)){$Filtersql .= ' WHERE 1 AND ('; }
    else
    {
        $Filtersql .= ' AND (';
    } 
    foreach ($aPopulations as $word) 
    if($word==$aPopulations[count($aPopulations)-1])
    {
        $Filtersql .= " Population = '". $word ."')";
    }
    else
    {
        $Filtersql .= " Population = '". $word ."' OR";
    }       
            
}

$object->Filter = $Filtersql;


$Searchsql="SELECT * FROM test.article";

if (isset($_POST['search'])){
    
    $st= ($_POST['search_box']);
    $Searchsql .= " WHERE name LIKE '%{$st}%' OR abstract LIKE '%{$st}%' OR Summary LIKE '%{$st}%' OR Keyword LIKE '%{$st}%'
    OR population LIKE '%{$st}%'OR region LIKE '%{$st}%'";
}
$object->Search = $Searchsql;


Share this post


Link to post
Share on other sites

Just taking a shot at what I think you are doing. 

 

Form 1 is displayed and submitted and you grab the first value.  Then I assume that you generate a new form and the same process happens and you grab the second value, but lose the first. 

 

1 - you could save the first value as a hidden form field in the second form and then retrieve both when the second form is submitted.

2 - OR you could save each form value as a $_SESSION variable and when all 3 forms are done you will have 3 session vars to use at the end.

Share this post


Link to post
Share on other sites

Thank you so much for your advice. I would certainly learn about those two options and try to apply them into my code.

Share this post


Link to post
Share on other sites

The first option - a hidden field - is nothing more than adding one of these to your next form before output:

<input type='hidden' name='field1' value='$field1'>

Then when your second form is subtmitted and you receive the POST vars, you will have a $_POST['field1'] in it.  Then you send that back out with the field you are getting in your second form, both to the third form. 

Edited by ginerjm

Share this post


Link to post
Share on other sites

Hi, I used the $_SESSION variable to store all three of my variables and everything works fine except when I refresh the page, the variables are still there.

 

I would like to know if there is a way to use the session_unset/destroy function so that every time the page is refreshed, the SESSION variables will reset.

 

Thanks!

Share this post


Link to post
Share on other sites

The whole point of using SESSION variables is so that the values persist from page to page.

Share this post


Link to post
Share on other sites

In other words - if you are TRULY using $_SESSION vars then they are NOT on your page so you are NOT seeing them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.