Jump to content

Sessions


naykidd

Recommended Posts

Basicaly, the following brings up two dropdown menus of cars

Firstly, you select the make, and hit select

Then the models appear, according to the make.

You then select the relative model, and hit select, generating a list of parts from the model

 

This all goes well, but im trying to save the make from the first form to the second form so i can say "You searched for 'Make' 'Model' "

I've been reading up all morning on Sessions, and i've got it to work setting a variable as a random word and making it appear, but i can't get my code to pick up the first choice as a variable, it will only say the model

 

I understand i need to use something like the following i've written SOMEWHERE in my code..

 

$_SESSION['test0'] = $_POST['chooser'];

 

but wherever i seem to put it, chooser is always blank

however if i change chooser to "lemon" or something, and put the line $lemon = test or something above it, it works fine and says "You have chosen Test "make""

 

<?
///////////////////////////////// CONNECTION
require_once("Connections/connection.php");
session_start(); 


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<link href="../stylesheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style9 {
font-size: 14px
}
.style11 {font-size: 14px}
.style1 {
font-size: 18px
}
.style12 {font-size: 12px}
.style131 {	font-size: 14px;
font-weight: bold;
}

</style>
</head> 
<body>
<table border='0' width 955 align=center>
<tr>
<td> <img src="../Images/banner.jpg" alt="Banner" width="955" height="100" /> </td>
/////////////// SEARCH 1 ***************
$query="SELECT Make FROM makes";

$result = mysql_query ($query);?>
<p> </p>
<p>
  <form enctype='multipart/form-data' METHOD=POST ACTION="pagetest.php">
  <?
echo "<select name='chooser'></option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[Make]>$nt[Make]</option>";

}
echo "</select>";
echo "<input type='submit' value='submit' />";


echo "</form>";

?>
  <br />
  <br />
  <?

////////////////////////// SEARCH 2 ************************

if (isset($_POST['chooser']))

echo "You Selected $chooser <br>
Now choose a Model of $chooser <br><br>";



$query2="SELECT Model FROM makemodel WHERE Make = '$chooser'"; //Select the make associated with the model (E.g. ford brings up Escort, Fiesta etc..

$result2 = mysql_query ($query2);
?>
  <form enctype='multipart/form-data' METHOD=POST ACTION="pagetest.php">
  <?
echo "<select name='mfinder'></option>";

while($nt2=mysql_fetch_array($result2)){
echo "<option value=$nt2[Model]>$nt2[Model]</option>";

}
echo "</select>";
echo "<input type='submit' value='submit' />";


echo "</form>";



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

echo "You Searched for ".$_SESSION['test0']." $chooser $type $mfinder";

$querys = sprintf("SELECT * FROM rossinis"); //selects all data from the database
$results = @mysql_query($querys); //tells the database
$row = mysql_fetch_array($results); //fetches the result
$querys = " ORDER BY `Model`";

$searchSQL = "SELECT ID, Make, Model, Detail, Capacity, Year, Vs, Dia, Thick, Bore, Studs, Part, Price FROM rossinis WHERE "; 

// grab the search types.  
$types = array();  
$types[] = isset($_GET['mfinder'])?"`model` LIKE '%{$mfinder}%'":'';   

$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)  

if (count($types) < 1)  
$types[] = "`Model` LIKE '%{$mfinder}%'"; // use the model as a default search if none are checked  

$andOr = isset($_GET['matchall'])?'AND':'OR';  
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `id`"; // order by id  

$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

$size = 0;
$cnt = 0;

"</Center></td>";
  echo "<tr>";
  echo "<table border='0' width '955' align=center>";
  echo "</tr>";
  echo "<br>";
  
echo "<table border='1' width='955' align=center>";

     	echo "<tr>";
	echo "<td><b>Make</b></td>";
			echo "<td><b>Model</b></td>";
	echo "<td><b>Detail</b></td>";
	echo "<td><b>Capacity</b></td>";
	echo "<td><b>Year</b></td>";
	echo "<td><b>Vented/Solid</b></td>";
	echo "<td><b>Diameter</b></td>";
			echo "<td><b>Thickness</b></td>";
					echo "<td><b>Disk Bore</b></td>";
							echo "<td><b>Studs</b></td>";
									echo "<td><b>Part Number</b></td>";
											echo "<td><b>Price</b></td>";
        echo "</tr>";
while ($row1 = mysql_fetch_array($searchResult)) {
        
	echo "<tr>";
	echo "<td>".$row1['Make']."</td>";
	echo "<td>".$row1['Model']."</td>";
	echo "<td>".$row1['Detail']."</td>";
	echo "<td>".$row1['Capacity']."</td>";
	echo "<td>".$row1['Year']."</td>";
	echo "<td>".$row1['Vs']."</td>";
			echo "<td>".$row1['Dia']."</td>";
					echo "<td>".$row1['Thick']."</td>";
							echo "<td>".$row1['Bore']."</td>";
									echo "<td>".$row1['Studs']."</td>";
											echo "<td>".$row1['Part']."</td>";
													echo "<td>".$row1['Price']."</td>";

	}
    $cnt++;
    echo "</tr>";
echo "</table>"; 
echo "<br><br><br><br><br>"; 


}  

function removeEmpty($var) {  
return (!empty($var));   
} 
session_destroy();  
?>
</p>

Link to comment
Share on other sites

$makes = array();
$models = array();
if (isset($_POST['make'])) {
    //generate models
    $models = array(..);
    if (isset($_POST['model'])) {
        //generate something else or handle it!
    }
}

 

The reason I defined $makes and $models before my if's is because you prolly want to roll it out like or use in a template which wouldn't otherwise work because $models wasn't yet defined:

 

<?php if (!empty($models)): ?>
<select name="models">
    <?php foreach ($models as $model): ?>
    <option name="<?php print $model; ?>"><?php print $model; ?></option>
    <?php endforeach; ?>
</select>
<?php else: ?>
<select name="models" disabled></select>
<?php endif; ?>

Link to comment
Share on other sites

Thanks!

I've tried implementing it into my code but i think i've mistaken where it needs to go, im getting the form again, and now its not allowing me to make any selections, could you help in where id put your code?

 

in mine the make of the car is known as $chooser and the model of the car is known as $mfinder, have you used $make and $model where i would use them then?

 

I've been doing this all day though is nice to be getting somewhere finally!

Link to comment
Share on other sites

I tried to input your words too but seem to have failed, first search works but the second dropdown is always empty :/

 

Many thanks

 

<?
///////////////////////////////// CONNECTION
require_once("Connections/connection.php");
session_start(); 



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<link href="../stylesheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style9 {
font-size: 14px
}
.style11 {font-size: 14px}
.style1 {
font-size: 18px
}
.style12 {font-size: 12px}
.style131 {	font-size: 14px;
font-weight: bold;
}

</style>
</head> 
<body>
<table border='0' width 955 align=center>
<tr>
<td> <img src="../Images/banner.jpg" alt="Banner" width="955" height="100" /> </td>

/////////////// SEARCH 1 ***************
$query="SELECT Make FROM makes";

$result = mysql_query ($query);?>
<p> </p>
<p>
  <form enctype='multipart/form-data' METHOD=POST ACTION="pagetest.php">
  <?
echo "<select name='chooser'></option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[Make]>$nt[Make]</option>";

}
echo "</select>";
echo "<input type='submit' value='submit' />";


echo "</form>";

?>
  <br />
  <br />
  <?

////////////////////////// SEARCH 2 ************************
if (isset($_POST['chooser']))

echo "You Selected $chooser <br>
Now choose a Model of $chooser <br><br>";

$query2="SELECT Model FROM makemodel WHERE Make = '$chooser'"; //Select the make associated with the model (E.g. ford brings up Escort, Fiesta etc..

$models = mysql_query($query2);

if (!empty($chooser)): ?>
<select name="mfinder">
    <?php foreach ($chooser as $models): ?>
    <option name="<?php print $models; ?>"><?php print $models; ?></option>
    <?php endforeach; ?>
</select>
<?php else: ?>
<select name="mfinder" disabled></select>
<?php endif;



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

echo "You Searched for $chooser $mfinder";

$querys = sprintf("SELECT * FROM rossinis"); //selects all data from the database
$results = @mysql_query($querys); //tells the database
$row = mysql_fetch_array($results); //fetches the result
$querys = " ORDER BY `Model`";

$searchSQL = "SELECT ID, Make, Model, Detail, Capacity, Year, Vs, Dia, Thick, Bore, Studs, Part, Price FROM rossinis WHERE "; 

// grab the search types.  
$types = array();  
$types[] = isset($_GET['mfinder'])?"`model` LIKE '%{$mfinder}%'":'';   

$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)  

if (count($types) < 1)  
$types[] = "`Model` LIKE '%{$mfinder}%'"; // use the model as a default search if none are checked  

$andOr = isset($_GET['matchall'])?'AND':'OR';  
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `id`"; // order by id  

$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

$size = 0;
$cnt = 0;

"</Center></td>";
  echo "<tr>";
  echo "<table border='0' width '955' align=center>";
  echo "</tr>";
  echo "<br>";
  
echo "<table border='1' width='955' align=center>";

     	echo "<tr>";
	echo "<td><b>Make</b></td>";
			echo "<td><b>Model</b></td>";
	echo "<td><b>Detail</b></td>";
	echo "<td><b>Capacity</b></td>";
	echo "<td><b>Year</b></td>";
	echo "<td><b>Vented/Solid</b></td>";
	echo "<td><b>Diameter</b></td>";
			echo "<td><b>Thickness</b></td>";
					echo "<td><b>Disk Bore</b></td>";
							echo "<td><b>Studs</b></td>";
									echo "<td><b>Part Number</b></td>";
											echo "<td><b>Price</b></td>";
        echo "</tr>";
while ($row1 = mysql_fetch_array($searchResult)) {
        
	echo "<tr>";
	echo "<td>".$row1['Make']."</td>";
	echo "<td>".$row1['Model']."</td>";
	echo "<td>".$row1['Detail']."</td>";
	echo "<td>".$row1['Capacity']."</td>";
	echo "<td>".$row1['Year']."</td>";
	echo "<td>".$row1['Vs']."</td>";
			echo "<td>".$row1['Dia']."</td>";
					echo "<td>".$row1['Thick']."</td>";
							echo "<td>".$row1['Bore']."</td>";
									echo "<td>".$row1['Studs']."</td>";
											echo "<td>".$row1['Part']."</td>";
													echo "<td>".$row1['Price']."</td>";

	}
    $cnt++;
    echo "</tr>";
echo "</table>"; 
echo "<br><br><br><br><br>"; 


}  

function removeEmpty($var) {  
return (!empty($var));   
} 
session_destroy();  
?>
</p>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.