Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding records to a result set

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
now that I finally have my sessions working (thanks to Lrnmore)I've been able to properly remove records from a result set that have been stored in a session...

Next I would like to be able to add one result at a time to the result set.
Example:
I begin with an empty <iframe> (except for headers)

below the <iframe> are buttons, each containing a name.

when the button is pressed a query is run (i.e. SELECT * FROM movies WHERE name="$_SESSION['name'];)

the results (for that person) are displayed in the <iframe> and added to the result set.

when another button is pressed all the records for that name are added to the previous results...and so on...

Right now I am able to output the entire contents of the table to the <iframe>, however I'm not sure as to how I can add the results of each query to a SESSION result set.

So I guess the question is...

Can I add rows to a result set (and the <ifram>) based on a mysql query using SESSION variables? If so whatwould be the best way.

Thanks

atsea
 
If you want to accumulate the results of multiple query results you just add the returned data rows to a session variable. It's just accumulating the rows - preferably returned as associative arrays - in a session var that is a multidimensional array.

Code:
$query = "SELECT * FROM movies WHERE name='".$_SESSION['name']."'";
$result = mysql_query($query) OR die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
    # whatever display code here
    # then add the row to the session
    $_SESSION['results'][] = $row;
}

That will accumulate all rows in the session var $_SESSION['results'].
Now with the <iframe> you have to keep in mind that the script that's run in the iframe has to share the same session ID was the parent window to make the session variables accessible.

Hope I understood your problem correctly. If not, just let me know.
 
Thats exaclty what I want to do, however I'm not sure the best way to implement it...

Initially I thought of putting it in a seperate file (lets say add.php) this file would run when a button is clicked (<input class="dir_but" type="submit" value="Stanley Kubrick" onClick="add.php">
This would return all the results (with director="Stanly Kubrick") to
<iframe src="php/db_select.php" width="100%" height="190px" marginheight="0" marginwidth="0" frameborder="0"></iframe>

Not sure how to have the results update in the <iframe> (although I figure it will have to do with the "target=''" property).

Now Im wondering (instead of a seperate file) if I should contain everything in one file...In this case I need to figure out how to pass the variable to .php file from the button click (Note: my page has a .html extension).

Here is what I have so far (without any add function):
this code will print the entire contents of the database to an html table. In addition a button is printed in the first column, when that button is pressed (isset) the ID is sent to an IF statement where it is (unset) removed from the result set.
Code:
<?php
session_start(); 
//header("Cache-control: private"); // IE 6 Fix. 
error_reporting(E_ALL);   
 

// check whether to pull the data from the database
if(!isset($_SESSION['rs'])) { 

    include 'db_connect_movies.php'; 
    $sql_select = "SELECT ID, Genre, Title, Director, Year FROM $db_table"; 
    $rs = mysql_query($sql_select); 
    $numrows = mysql_num_rows($rs); 
  
    for($i=0; $i < $numrows; $i++) { 
        $_SESSION['rs'][$i] = mysql_fetch_assoc($rs);
    } 
} 

if(isset($_GET['id'])) { 
    for($i=0; $i < count($_SESSION['rs']); $i++) { 
        if($_GET['id'] == $_SESSION['rs'][$i]['ID']) { 
            unset($_SESSION['rs'][$i]); 
        } 
    }
    $_SESSION['rs'] = array_values($_SESSION['rs']); 
}

//creating the table /w headers
echo "<html><body>";  
echo "<table border='1' cellspacing='0'><tr><td><img src='../images/close.jpg' /></td><td>Genre</td><td>Title</td><td>Director</td><td>Year</td></tr>";  
      
//row for each record
for($i=0; $i < count($_SESSION['rs']); $i++) { 
        echo "<tr><td><a href='" . basename($_SERVER['PHP_SELF']) . "?id=" . $_SESSION['rs'][$i]['ID'] . "'><img src='../images/close.jpg' border='0' /></a></td><td>" . $_SESSION['rs'][$i]['Genre'] . "</td><td>" . $_SESSION['rs'][$i]['Title'] . "</td><td>" . $_SESSION['rs'][$i]['Director'] . "</td><td>" . $_SESSION['rs'][$i]['Year'] . "</td></tr>";  
} 
      
echo "</table>";  
echo "</body></html>";  

//free memory
mysql_free_result($rs);  

//close the db
mysql_close();

?>

any suggestions for the best method to accomplish this?

atsea
 
Let me ask you this:
What is the final functionality you are looking for? It is not clear to me what you want to achieve.

If you want to sort results, filter results out etc. it seems more efficient to tweak the SQL each time and retrieve a subset of the entire data. Storing what is in the database in a session variable to later remove things from it might not be the best way to go. Database requests are quick, and with a littel work on the SQL very specific result sets can be retrieved.

If you let us know what you want to do, it will be easier to give concrete examples.
 
The final functionality I'm looking for is...

I have a database that stores information regarding my favorite movies. (i.e. title, director, genre, etc.)

I have a webpage that is basically an interface to the database. (In this case it is a page that has various ways to search the data)

The specific method of searching I am working on (the final functionality) is a series of buttons that contain the name of the directors in my database. when that button is clicked all the movies for that director are displayed (in an <iframe>).

(ex. I click on "Stanly Kubrick", and all his movies are displayed. In other words all the records containing "Stanly Kubrick" in the director column are added to a result set and the <iframe>)

then if the user decides to click on another director, that director and his/her movies will be displayed as well.

(ex. now I click on "Oliver Stone" and all his movies are displayed along with "Stanly Kubrick's". In other words all records that contain the value "Oliver Stone" in the director column are added to the result set of the previous query).

Now, with all the movies from the selected directors displayed the user can go through the list and remove records from the table (and result set) leaving only certain movies behind.
(Note: the remove function is complete, as shown in my previous post)

Not sure if that clears anything up...let me know.

atsea
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top