Need help getting started - PHP, MySQL, Item/Part Configurator

  • (20 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »

296 Replies - 10511 Views - Last Post: 19 March 2009 - 01:32 PM Rate Topic: -----

#31 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 06 February 2009 - 01:02 PM

But that may give the user the impression anything they select in sequence is possible. I'd look at that and say "Great! I can get a 1/8" brass philips head screw" when in reality 1/8" only comes in stainless.

Is what I want to do even possible? I know you are suggesting an easier alternative, but I'd like to stick with the original idea that was discussed here internally.
Was This Post Helpful? 0
  • +
  • -

#32 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 06 February 2009 - 01:10 PM

I'm sure it is possible, but it seems to me that you're going to either need to use AJAX or multiple submits in php. Either way, every time the user makes a selection you need to narrow down the choices, and that means multiple queries before you show the user anything useful to them. That would annoy me as a user: I like to click as few submit buttons as possible!

I think it's easier to present them with all options and let them see what comes up. People are used to seeing that kind of thing anyway. To top it off, if the choice they were looking for isn't in the database, my way lets them see potential alternatives.
Was This Post Helpful? 0
  • +
  • -

#33 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 06 February 2009 - 01:19 PM

View Postnoboost4you, on 6 Feb, 2009 - 12:02 PM, said:

But that may give the user the impression anything they select in sequence is possible. I'd look at that and say "Great! I can get a 1/8" brass philips head screw" when in reality 1/8" only comes in stainless.

I think that if you use something like this in the arrays of choices:
$length = array ("Any", "1/8","1/4","1/2");
$material = array("Any", "Galvanized", "Brass","Nylon","Stainless");
$drivestyle = array("Any", "Slotted","Philips", "Hex");


The word "Any" appearing as the first option would imply that you're searching through the database for closest matches. Other housekeeping chores like labeling the menus and some body text can help to remove any confusion.
Was This Post Helpful? 0
  • +
  • -

#34 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 06 February 2009 - 01:30 PM

View PostCTphpnwb, on 6 Feb, 2009 - 12:19 PM, said:

View Postnoboost4you, on 6 Feb, 2009 - 12:02 PM, said:

But that may give the user the impression anything they select in sequence is possible. I'd look at that and say "Great! I can get a 1/8" brass philips head screw" when in reality 1/8" only comes in stainless.

I think that if you use something like this in the arrays of choices:
$length = array ("Any", "1/8","1/4","1/2");
$material = array("Any", "Galvanized", "Brass","Nylon","Stainless");
$drivestyle = array("Any", "Slotted","Philips", "Hex");


The word "Any" appearing as the first option would imply that you're searching through the database for closest matches. Other housekeeping chores like labeling the menus and some body text can help to remove any confusion.


Ah, ok. It definitely wouldn't hurt to start with something like this just to get our feet wet. Thanks for all your help. Have a good weekend. I'll be back Monday to continue this saga.
Was This Post Helpful? 0
  • +
  • -

#35 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 06 February 2009 - 07:34 PM

Ok, well I'm going to have to spend some time this weekend on it! I'm a little embarrassed looking at my code. :crazy:

I should have seen that it was repetitive, and that almost always means that it should be shorter. Here's a shorter version:
<?php
//require("generic_class.php");
session_start();
$choice = $_SESSION['choice'];

if(count($choice) == 0) {
	$length = array ("Any length", "1/8","1/4","1/2");
	$material = array("Any material", "Galvanized", "Brass","Nylon","Stainless");
	$drivestyle = array("Any drivestyle", "Slotted","Philips", "Hex");
	$choice = array("Length" => $length, "Material" => $material,"Drivestyle" => $drivestyle);
}

foreach($choice as $key => $value) {
	echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
	foreach($value as $item) {
		echo '<select name= "'.$key.'">';
		if ($item == $_POST[$key]) {
			$selected = "selected";
		} else {
			unset($selected);
		}
		echo '<option value="'.$item.'" '.$selected.' >'.$item.'</option>';
	}
	echo '</select>'; 
}
echo '<input type="submit" /></form>';

$_SESSION['choice'] = $choice;

?>

Was This Post Helpful? 0
  • +
  • -

#36 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 07 February 2009 - 12:06 PM

Ok, if you try this I think you'll see what I'm talking about. It doesn't give the option to select parts from the results, which aren't formatted to look pretty, but that should be easy to add.

I've created two tables. First is options with these columns:
Length	Material	Drivestyle
1/8	Galvanized	Slotted
1/4	Brass	Philips
1/2	Nylon	Hex
	Stainless	 


Then I have parts with these columns:


	Partnum	Length	Material	Drivestyle
			12	1/8	Galvanized	Slotted
			14	1/2	Galvanized	Hex
			233	1/4	Galvanized	Philips
			434	1/2	Brass	Philips
			112	1/2	Brass	Hex
			4321	1/4	Brass	Slotted
			9931	1/4	Nylon	Hex
			9982	1/8	Stainless	Hex
			1221122	1/2	Galvanized	Slotted
			12000	1/2	Galvanized	Philips


Here is my code:
<?php
require("generic.php");
class options extends general {
	var $opttable = array();

	function getchoices() {
		$query = "SELECT * FROM ".$this->table[$this->tablenum];
		$dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error());
		while($row = mysql_fetch_array($dbdata)) {
			foreach($row as $keyname => $value) {
				if(!is_numeric($keyname)) {
					if (is_null($this->opttable[$keyname])) {
						$this->opttable[$keyname] = array("Any");
					}
					if(!is_null($value) && $value != "") {
						array_push($this->opttable[$keyname],$value);
					}
				}
			}
		}
	}	
	
	function showparts($q) {
		$query = "SELECT * FROM ".$this->table[$this->tablenum]." ".$q;
		//echo $query."<br>";
		$dbdata = mysql_query($query,$this->dblink) or die('Error reading from table: '.$this->table[$this->tablenum]." is ".mysql_error());
		$head = "<b>";
		$hd = true;
		while($this->row = mysql_fetch_array($dbdata)) {
			if($hd) {
				foreach($this->row as $key => $value) {
					if(!is_numeric($key)) {
						$head .= $key." -- ";
					}
				}
				$head .= "</b><br>";
				echo $head;
				$hd = false;
			}
			$this->display();
		}
	}
	
}

session_start();
//session_destroy();
	$choice = $_SESSION['choice'];
if(count($choice) == 0) {
	$length = array ("Any");
	$material = array("Any");
	$drivestyle = array("Any");
	$opt = new options;
	$opt->tablenum = 3;
	$opt->getchoices();
	$choice = $opt->opttable;
	echo "<br>NEW<br>";
}
$myparts = new options;
$myparts->tablenum = 4;


$thequery = "WHERE ";
foreach($choice as $key => $value) {
	echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
	foreach($value as $item) {
		echo '<select name= "'.$key.'">';
		if ($item == $_POST[$key]) {
			$selected = "selected";
			if($item != "Any") {
				$thequery .= $key." = '".$item."' AND ";
			}
		} else {
			unset($selected);
		}
		echo '<option value="'.$item.'" '.$selected.' >'.$item.'</option>';
	}
	echo '</select>'; 
}
echo '<input type="submit" /></form>';
$thequery = substr($thequery,0, -5);
if(count($_POST) > 0) {
	$myparts->showparts($thequery);
}

$_SESSION['choice'] = $choice;
$_SESSION['myparts'] = $myparts;

?>


and generic.php, which has some extra functions because I 'borrowed' it from other projects:
<?php 
class general {
	var	$id;
	var $keyname= "id";
	var	$row;
	public	$table = array("newtable","Another_table","A3rdTable","options","parts");
	var	$dblink;
	var	$servername='localhost';
	var	$dbusername='root';
	var	$dbpassword='root';
	var	$dbname='newtest';

	function __construct() {
		$this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL');
		mysql_select_db($this->dbname,$this->dblink) or  ('Could not open database '.mysql_error());
	}
		
	function read_table($tablenum) {
		$query = "SELECT * FROM ".$this->table[$tablenum]." where ".$this->keyname." = '".$this->id."'";
		$dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error());
		$this->row = mysql_fetch_array($dbdata);
	}
	
	function update_table($tablenum) {
		$query = "update ".$this->table[$tablenum]." set ";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$query .= $key." = '".$value."', ";
			}
		}
		$query = substr($query,0,-2)." where ".$this->keyname." ='".$this->id."'";
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

function addrecord($tablenum) {
	$query = "insert into ".$this->table[$tablenum]." (";
	$end_query = ") values (";
	foreach($this->row as $key => $value) {
		if(!is_numeric($key) && $key != "id") { 
			$query .= $key.", ";
			$end_query .= "'".$value."', ";
			}
		}
	$end_query = substr($end_query,0,-2).")";	
	$query = substr($query,0,-2).$end_query;
	mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

function display() {
	$line = "";
	foreach($this->row as $key => $value) {
		if(!is_numeric($key) && $key != "id") { 
			$line .= $value." -- ";
			}
		}
		echo $line."<br>";
	}

function droprecord($tablenum,$dropid) {
	$query = "Delete from ".$this->table[$tablenum]." where ".$this->keyname." = '".$dropid."'";
	echo $query;
	mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

}	

?>

Was This Post Helpful? 0
  • +
  • -

#37 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 08:57 AM

This is the output after I changed generic.php to our DB, username, and password.

Each field had one selection in the pull down menu (what you see). I'm guessing that's not how you pictured it?

Posted Image

This post has been edited by noboost4you: 09 February 2009 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

#38 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 09:36 AM

You need to add a table called: options to your database. That will contain all of the possible values for each field in your table with the data. So you end up with two tables defined in your database (In my system it's named: newtest)
public $table = array("newtable","Another_table","A3rdTable","options","YOUR_TABLE_NAME");

var $dbname='newtest';

I have other tables listed because I reuse the same code for testing other things. That's why I have these lines in the main code:

$opt->tablenum = 3;

and

$myparts->tablenum = 4;

They tell the object which table to access.
Was This Post Helpful? 0
  • +
  • -

#39 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 11:18 AM

I added the 'options' and 'parts' tables like you had them in a previous post.

Check my last post. I have edited it to show the screenshot.
Was This Post Helpful? 0
  • +
  • -

#40 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 11:34 AM

What browser are you using? Not IE, I hope!
The html produced by my code gives me this:
<br>NEW<br><form action="/selections.php" method="post"><select name= "Length"><option value="Any" selected >Any</option><select name= "Length"><option value="1/8"  >1/8</option><select name= "Length"><option value="1/4"  >1/4</option><select name= "Length"><option value="1/2"  >1/2</option></select><form action="/selections.php" method="post"><select name= "Material"><option value="Any" selected >Any</option><select name= "Material"><option value="Galvanized"  >Galvanized</option><select name= "Material"><option value="Brass"  >Brass</option><select name= "Material"><option value="Nylon"  >Nylon</option><select name= "Material"><option value="Stainless"  >Stainless</option></select><form action="/selections.php" method="post"><select name= "Drivestyle"><option value="Any" selected >Any</option><select name= "Drivestyle"><option value="Slotted"  >Slotted</option><select name= "Drivestyle"><option value="Philips"  >Philips</option><select name= "Drivestyle"><option value="Hex"  >Hex</option></select><input type="submit" /></form>


That works in my browser.
Was This Post Helpful? 0
  • +
  • -

#41 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 12:06 PM

Newest version of Firefox.
Was This Post Helpful? 0
  • +
  • -

#42 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 12:11 PM

I see. I got the same problem in Firefox. Works in Safari though. I'll have to see what's going on with the html.
Was This Post Helpful? 0
  • +
  • -

#43 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 12:19 PM

Ok, got it.
Switch these to lines from:
foreach($value as $item) {
echo '<select name= "'.$key.'">';

to:

echo '<select name= "'.$key.'">';
foreach($value as $item) {


Safari doesn't care, but Firefox doesn't like multiple select names in the options.
Was This Post Helpful? 0
  • +
  • -

#44 noboost4you  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 01:06 PM

View PostCTphpnwb, on 9 Feb, 2009 - 11:19 AM, said:

Ok, got it.
Switch these to lines from:
foreach($value as $item) {
echo '<select name= "'.$key.'">';

to:

echo '<select name= "'.$key.'">';
foreach($value as $item) {


Safari doesn't care, but Firefox doesn't like multiple select names in the options.


Ah, that's better. How did you figure out switching the two lines would cause that problem?
Was This Post Helpful? 0
  • +
  • -

#45 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Need help getting started - PHP, MySQL, Item/Part Configurator

Posted 09 February 2009 - 01:10 PM

I ran the html through this validator:
http://validator.w3.org/


So do you see what I was talking about? By letting the user pick the parameters and searching, it's likely that there will be more than one item that fits their needs that they can choose from.
Was This Post Helpful? 0
  • +
  • -

  • (20 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »