Jump to content


Photo

How to save three variables from different queries

php mysql multiple queries function variables

  • Please log in to reply
8 replies to this topic

#1 Cyjm1120

Cyjm1120
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 07 November 2014 - 09:47 PM

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;




#2 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,081 posts
  • LocationVoorheesville NY

Posted 07 November 2014 - 09:55 PM

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.


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#3 Cyjm1120

Cyjm1120
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 08 November 2014 - 12:10 AM

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



#4 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,081 posts
  • LocationVoorheesville NY

Posted 08 November 2014 - 12:43 AM

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, 08 November 2014 - 12:43 AM.

JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#5 Cyjm1120

Cyjm1120
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 November 2014 - 07:41 PM

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!



#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,973 posts

Posted 10 November 2014 - 07:55 PM

:facewall:


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Cyjm1120

Cyjm1120
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 November 2014 - 08:47 PM

:sweat:  :sweat:  :sweat:  :confused:



#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,973 posts

Posted 10 November 2014 - 09:13 PM

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,081 posts
  • LocationVoorheesville NY

Posted 10 November 2014 - 09:27 PM

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


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users