View Full Version : Array & Dropdown menus
WebWeaver63
03-02-2005, 05:36 AM
I am trying to write a function that looks at the values of 2 table & brings back the data in 2 seperate dropdown with Titles(name) and the appropiate value selected for a particular user. The tables are:
Table 1
-------------------------------------------------------------------------------------------------------
fid | 2 3
name | Gender Religion
value | Male::Female Anabaptists::Baptist Church::Catholicism::Congregational:
size | 0 0
need | 3 3
pos | 1 1
public | 1 1
----------------------------------------------------------------------------------------------
Table 2
------------------------------------------
vid | 5 6
uid | 6 6
fid | 2 3
value| Male Catholicism
------------------------------------------
so for example for uid=6 I would want it to bring back
Gender
<select>
<option selected>Male</option>
<option>Female</option>
</select>
Religion
<select>
<option >Anabaptists</option>
<option >Baptist Church</option>
<option selected>Catholicism</option>
<option >Congregational</option>
<option >Coptic Church</option>
<option >Disciples </option>
</select>
the code I'm trying (unsucessfully I might add) to use is:
// Pick Gender
function rel_select($user_id, $select_name = 'gender')
{
global $db, $user_prefix;
//recup info user ----------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field");
while ($sqlvalue = $db->sql_fetchrow($result)) {
list($value) = $db->sql_fetchrow( $db->sql_query("SELECT value FROM ".$user_prefix."_cnbya_value WHERE fid ='".$sqlvalue[fid]."' AND uid ='".$user_id."'"));
$me[$sqlvalue[name]] = $value;
}
//--------------------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field WHERE need <> '0' ORDER BY pos");
while ($sqlvalue = $db->sql_fetchrow($result)) {
$t = $sqlvalue[fid];
$value2 = explode("::", $sqlvalue[value]);
if (substr($sqlvalue[name],0,1)=='_') eval($name_exit = $sqlvalue[name]) else $name_exit = $sqlvalue[name];
$rel_select ='<tr><td>'.$name_exit.'<b></b></td><td>';
$rel_select .='<select name='.nfield[$t].'>\n';
for ($i = 0; $i<count($value2); $i++) {
if ($userinfo[$sqlvalue[name]] == $value2[$i]) $sel = 'selected'; else $sel = '';
$rel_select .='<option value='.$value2[$i]. .$sel.'>'.$value2[$i].'</option>\n';
}
$rel_select .='</select>';
$rel_select .='</td></tr>\n';
}
return $rel_select;
}
Can any one help me?? I'm a newbie to php
Viper007Bond
03-02-2005, 06:50 AM
I don't want to try to make sense of your table setup nor your code, so I've started over with both. :p
The religion table.
CREATE TABLE religions (
religionid int(5) NOT NULL auto_increment,
religionname varchar(255) NOT NULL default '',
PRIMARY KEY (religionid)
) TYPE=MyISAM;
INSERT INTO religions VALUES (1, 'Anabaptists');
INSERT INTO religions VALUES (2, 'Baptist Church');
INSERT INTO religions VALUES (3, 'Catholicism');
INSERT INTO religions VALUES (4, 'Congregational');
That looks like this in human readable format:
religionid | religionname
-----------+---------------
1 | Anabaptists
2 | Baptist Church
3 | Catholicism
4 | Congregational
And here's our user table:
CREATE TABLE users (
userid int(10) NOT NULL auto_increment,
gender enum('Unknown','Male','Female') NOT NULL default 'Unknown',
religion int(5) NOT NULL default '0',
PRIMARY KEY (userid)
) TYPE=MyISAM;
INSERT INTO users VALUES (1, 'Male', 3);
INSERT INTO users VALUES (2, 'Female', 2);
INSERT INTO users VALUES (3, 'Male', 1);
INSERT INTO users VALUES (4, 'Male', 2);
INSERT INTO users VALUES (5, 'Female', 4);
That looks like this in human readable format:
userid | gender | religion
-------+--------+---------
1 | Male | 3
2 | Female | 2
3 | Male | 1
4 | Male | 2
5 | Female | 4
Using an ID rather than the name again keeps the religion names in 1 place instead of 2.
Now, for the PHP to grab it with. THIS IS TOTALLY UNTESTED, so it may not work...
<?php
$userid = 1; // This is set by logging in or whatever
/* -- YOU CONNECT TO THE DATABASE HERE -- */
// Let's grab the user data
$userquery = mysql_query("SELECT gender, religion FROM users WHERE userid='" . $userid . "' LIMIT 1");
$user = mysql_fetch_array($queryusers);
echo "Your gender is: <select>\n";
echo "<option";
if ($user['gender'] == "Male") echo " selected";
echo ">Male</option>\n";
echo "<option";
if ($user['gender'] == "Female") echo " selected";
echo ">Female</option>\n";
echo "</select>\n\n"
// Grab the different religions
$religionquery = mysql_query("SELECT religionid, religionname FROM religions");
echo "Your religion is: <select>\n";
// Echo out each religion name
while ($religion = mysql_fetch_array($religionquery)) {
echo "<option";
if ($user['religion'] == $religion['religionid']) echo " selected";
echo ">" . $religion['religionname'] . "</option>\n";
}
echo "</select>"
?>
WebWeaver63
03-02-2005, 04:20 PM
Thank you for replying, sorry maybe I didn't explain the tables that well here's a dump form the 2 tables in question:
# --------------------------------------------------------
#
# Table structure for table 'field'
#
CREATE TABLE field (
fid int(10) NOT NULL auto_increment,
name varchar(255) DEFAULT 'field' NOT NULL,
value varchar(255),
size int(3),
need int(1) DEFAULT '1' NOT NULL,
pos int(3),
public int(1) DEFAULT '1' NOT NULL,
PRIMARY KEY (fid),
KEY fid (fid)
);
#
# Dumping data for table 'field'
#
INSERT INTO field VALUES ('2', 'Gender', 'Male::Female', '0', '3', '1', '1');
INSERT INTO field VALUES ('3', 'Religion', 'Anabaptists::Baptist Church::Catholicism::Congregational::Coptic Church:: Disciples', '0', '3', '2', '1');
# --------------------------------------------------------
#
# Table structure for table 'value'
#
CREATE TABLE value (
vid int(10) NOT NULL auto_increment,
uid int(10) DEFAULT '0' NOT NULL,
fid int(10) DEFAULT '0' NOT NULL,
value varchar(255),
PRIMARY KEY (vid),
KEY vid (vid)
);
#
# Dumping data for table 'value'
#
INSERT INTO value VALUES ('5', '6', '2', 'Male');
INSERT INTO value VALUES ('6', '6', '3', 'Disciples');
INSERT INTO value VALUES ('7', '3', '2', 'Male');
INSERT INTO value VALUES ('8', '3', '3', 'Catholicism');
INSERT INTO value VALUES ('9', '2', '2', 'Male');
INSERT INTO value VALUES ('10', '2', '3', 'Catholicism');
INSERT INTO value VALUES ('11', '4', '2', 'Male');
INSERT INTO value VALUES ('12', '4', '3', 'Catholicism');
INSERT INTO value VALUES ('13', '5', '2', 'Male');
INSERT INTO value VALUES ('14', '5', '3', 'Baptist Church');
Am I'm trying to give the user a dropdown box to edit their values, so for example for uid=6 I would want it to bring back
Gender
<select>
<option selected>Male</option>
<option>Female</option>
</select>
Religion
<select>
<option >Anabaptists</option>
<option >Baptist Church</option>
<option selected>Catholicism</option>
<option >Congregational</option>
<option >Coptic Church</option>
<option >Disciples </option>
</select>
the code I'm trying (unsucessfully I might add) to use is:
// Pick Gender
function rel_select($user_id)
{
global $db, $user_prefix;
//recup info user ----------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field");
while ($sqlvalue = $db->sql_fetchrow($result)) {
list($value) = $db->sql_fetchrow( $db->sql_query("SELECT value FROM ".$user_prefix."_cnbya_value WHERE fid ='".$sqlvalue[fid]."' AND uid ='".$user_id."'"));
$me[$sqlvalue[name]] = $value;
}
//--------------------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field WHERE need <> '0' ORDER BY pos");
while ($sqlvalue = $db->sql_fetchrow($result)) {
$t = $sqlvalue[fid];
$value2 = explode("::", $sqlvalue[value]);
if (substr($sqlvalue[name],0,1)=='_') eval($name_exit = $sqlvalue[name]) else $name_exit = $sqlvalue[name];
$rel_select ='<tr><td>'.$name_exit.'<b></b></td><td>';
$rel_select .='<select name='.nfield[$t].'>\n';
for ($i = 0; $i<count($value2); $i++) {
if ($userinfo[$sqlvalue[name]] == $value2[$i]) $sel = 'selected'; else $sel = '';
$rel_select .='<option value='.$value2[$i]. .$sel.'>'.$value2[$i].'</option>\n';
}
$rel_select .='</select>';
$rel_select .='</td></tr>\n';
}
return $rel_select;
}
I hope that explains it better, I could really use the help. Again Thank you.
WebWeaver63
03-02-2005, 06:29 PM
Thank you for your assistance but i figured it out. I created 2 function one to get the religion & to get the gender.
// Pick Religion
function rel_select($user_id)
{
global $db, $user_prefix;
//recup info user ----------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field");
while ($sqlvalue = $db->sql_fetchrow($result)) {
list($value) = $db->sql_fetchrow( $db->sql_query("SELECT value FROM ".$user_prefix."_cnbya_value WHERE fid ='".$sqlvalue[fid]."' AND uid ='".$user_id."'"));
$userinfo[$sqlvalue[name]] = $value;
}
//--------------------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field WHERE need <> '0' ORDER BY pos");
while ($sqlvalue = $db->sql_fetchrow($result)) {
$t = $sqlvalue[fid];
$value2 = explode("::", $sqlvalue[value]);
if (substr($sqlvalue[name],0,1)=='_') eval( "\$name_exit = $sqlvalue[name];"); else $name_exit = $sqlvalue[name];
$rel_select ='<select name=nfield['.$t.']>';
for ($i = 0; $i<count($value2); $i++) {
if ($userinfo[$sqlvalue[name]] == $value2[$i]) $sel = 'selected'; else $sel = '';
$rel_select .='<option value='.$value2[$i]. " " .$sel.'>'.$value2[$i].'</option>';
}
$rel_select .='</select>';
}
return $rel_select;
}
// Pick Gender
function gen_select($user_id)
{
global $db, $user_prefix;
//recup info user ----------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field");
while ($sqlvalue = $db->sql_fetchrow($result)) {
list($value) = $db->sql_fetchrow( $db->sql_query("SELECT value FROM ".$user_prefix."_cnbya_value WHERE fid ='2' AND uid ='".$user_id."'"));
$userinfo[$sqlvalue[name]] = $value;
}
//--------------------------------------------
$result = $db->sql_query("SELECT * FROM ".$user_prefix."_cnbya_field WHERE need <> '0' AND fid ='2' ORDER BY pos");
while ($sqlvalue = $db->sql_fetchrow($result)) {
$t = $sqlvalue[fid];
$value2 = explode("::", $sqlvalue[value]);
if (substr($sqlvalue[name],0,1)=='_') eval( "\$name_exit = $sqlvalue[name];"); else $name_exit = $sqlvalue[name];
$gen_select ='<select name=nfield['.$t.']>\n';
for ($i = 0; $i<count($value2); $i++) {
if ($userinfo[$sqlvalue[name]] == $value2[$i]) $sel = 'selected'; else $sel = '';
$gen_select .='<option value='.$value2[$i]. " " .$sel.'>'.$value2[$i].'</option>\n';
}
$gen_select .='</select>';
}
return $gen_select;
}
Viper007Bond
03-02-2005, 09:39 PM
I still say you're going about it in a crazy and non-clean way, but if you're happy with it and it works, then good luck. :)
iDxMan
03-02-2005, 11:58 PM
I'll echo Viper's DB structure comments as well. (being as constructive as I can)
It doesn't seem to be a very relational setup at all.. I especially don't understand the goal of using a delimited data structure in the `field' table and there seems to be quite a bit of data duplication.
Well, I guess I sortof see the idea behind it. Looks like you're basically using the `field' table as a single storage area for the various values that a field could contain.
Going with a normalized structure you aren't duplicating data and you're storing far less data that will help in the long run. No telling what snafu's you're going to run into with this structure.
-r
WebWeaver63
03-03-2005, 08:48 PM
not particularly happy w/ it but it works. the tbl structure was designed for phpNuke NOT by me. I'm jus trying to make it work best I can.
Mr. Popularity
03-04-2005, 12:04 AM
Here's a nice menu for you to use. I wrote it myself. It's PHP using the Smarty Template Engine. enjoy!
<?php
/**
* Smarty Javascript/CSS Menu
*
*/
class JCSS_Menu {
var $class = "menu";
var $classHover;
var $activeClass;
var $activeClassHover;
function JCSS_Menu(){
$this->__construct();
}
function __construct(){
$this->classHover &= $this->class;
$this->activeClass &= $this->class;
$this->activeClassHover &= $this->classHover;
}
function _jscript($cssClass, $display = false){
return "for ( var i = 0; i < this.childNodes.length; i++) { if (this.childNodes[i].nodeType == 1 && this.childNodes[i].nodeName == 'UL') { this.childNodes[i].style.display = '".(($display)? "block": "none")."'; this.childNodes[i].className = '".$cssClass."'; } }";
}
function _li($on,$off = NULL, $sub = false){
if (!$off) $off = $on;
$out = "<li class=\"".$on."\" onMouseOver=\" ";
if ($sub) $out .= $this->_jscript($off,true);
$out .= " this.className = '".$off."';\" onMouseOut=\" ";
if ($sub) $out .= $this->_jscript($on,false);
$out .= " this.className = '".$on."'; \" >\n";
return $out;
}
function _a($name,$href,$options = NULL){
return "<a href=\"$href\" $options>$name</a>\n";
}
function _parseClasses($list){
$classAry = explode(",",$list);
if (is_Array($classAry)) {
/*
* In order to prevent classnames from cascading incorrectly, setting these variables must be in this sequence!
*/
if ($classAry[3]) $this->activeClassHover = $classAry[3];
if ($classAry[2]) $this->activeClass = $classAry[2];
if ($classAry[1]) $this->classHover = $classAry[1];
if ($classAry[0]) $this->class = $classAry[0];
} else {
$this->class = $classAry;
}
}
/**
* Type: method<Br>
* Name: register<br>
* Purpose: to register the pre-created JCSS_Menu object as a smarty
* object.
*
* @param string| the name of the smarty object to be used in the template
* @param Smarty
*
*/
function register($name,&$smarty_obj){
$smarty_obj->register_object($name,$this,NULL,true,array("element_block"));
}
function menu($params, &$smarty_obj){
//echo "<pre>"; var_dump($params); echo "</pre>";
if($params['classes']) $this->_parseClasses($params['classes']);
if (is_array($params['options'])) {
$out .= "<UL class=\"".$this->class."\" style=\"display: ".(($params['hidden'])? "none" : "block").";\" ".(($params['id'])? "id=\"".$params['id']."\"": "").">\n";
foreach ($params['options'] as $item){
if (!is_array($item)) continue;
if ($item['sub'] && is_array($item['sub'])) {
$out .= $this->_li((($item['active'] == true)? $this->activeClass : $this->class),(($item['active'] == true)? $this->activeClassHover : $this->classHover),true)."<nobr>".(($item['resource'])? $this->_a($item['name'],$item['resource']) : $item['name'])."</nobr>";
$out .= $this->menu(array("options" => $item['sub'],
"hidden" => true),$smarty_obj);
} else {
$out .= $this->_li((($item['active'] == true)? $this->activeClass : $this->class),(($item['active'] == true)? $this->activeClassHover : $this->classHover))."<nobr>".(($item['resource'])? $this->_a($item['name'],$item['resource']) : $item['name'])."</nobr>";
}
$out .= "</LI>\n";
}
$out .= "</UL>\n";
} else {
if(!is_array($this->masterList)) $smarty_obj->trigger_error("Error: no menu data.");
$params['options'] = $this->masterList;
return $this->menu($params,$smarty_obj);
}
return $out;
}
function element($params,&$smarty_obj){
if (!$params['name']) {
$smarty_obj->trigger_error("A Display name is requires for this element");
}
return $this->newItem($params);
}
function element_block($params, $content, &$smarty_obj, &$repeat){
if($repeat) return;
if($content) $params['name'] =& $content;
if (!$params['name']) {
$smarty_obj->trigger_error("A Display name is requires for this element");
}
return $this->newItem($params);
}
/**
* Type: method<Br>
* Name: newItem<br>
* Purpose: to allow menu element creation via program logic
*
* @param array
* Description: array parameters match the optional menu attributes
* below
*/
function newItem($params){
if ($params['name']) $item['name'] = $params['name'];
else return false;
if ($params['resource'] || $params['href']) {
if ($params['resource']) $item['resource'] = $params['resource'];
else $item['resource'] = $params['href'];
}
if ($params['id']) $item['id'] = $params['id'];
elseif (!$item['resource']) $item['id'] = preg_replace("/\W/","_",$item['name']);
else $item['id'] = preg_replace("/\W/","_",$item['resource']);
if (isset($params['active'])) $item['active'] = true;
else $item['active'] = false;
$parent = trim($params['parent'],"/");
$path = preg_split("/\//",$parent,-1,PREG_SPLIT_NO_EMPTY);
$t =& $this->masterList;
for($i=0;$i<count($path);$i++) {
$part = $path[$i];
if ($i < count($path)){
if(is_array($t[$part])) {
$item['path'] .= "/$part";
$t =& $t[$part]['sub'];
}
}
}
$item['path'] .= "/".$item['id'];
$t[$item['id']] = $item;
}
}
?>
Mr. Popularity
03-04-2005, 12:05 AM
/*
*
* Sample Usage:
*
*<?php // demo.php
*
* require_once 'object.jcss_menu.php';
*
* $smarty = new Smarty;
*
* $demo = &new JCSS_Menu();
* $demo->register("demo",$smarty);
*
* $demo->newItem(array("name" => "File 1",
* "resource" => "?file1",
* "id" => "f1"));
*
* $demo->newItem(array("name" => "File 2",
* "resource" => "?file2",
* "id" => "f2"));
*
* $smarty->display("demo.tpl");
*
* // EOF;
*
*
* // Demo Template: demo.tpl
* <html>
* <head>
* <style type="text/css">
* {literal}
* #demo {
* margin: 0px 0px 0px 0px;
* padding: 0px 0px 0px 0px;
* list-style-type: none;
* list-style-position: inside;
* border-top: 1px solid #ddddff;
* font-family: Arial, Helvetica;
* width: 150px;
* font-weight: 600;
* font-size: 12pt;
* }
*
* #demo UL{
* margin: 0px 0px 0px 0px;
* padding: 0px 0px 0px 0px;
* list-style-type: none;
* list-style-position: inside;
* border-top: 1px solid #ddddff;
* font-family: Arial, Helvetica;
* width: 150px;
* font-weight: 600;
* font-size: 10pt;
* }
*
* #demo UL.menu {
* background-color: #f6f6f6;
* }
*
* #demo UL.active {
* background-color: #e9e9e9;
* }
*
* #demo UL.menuhover {
* position: absolute;
* background-color: #fff6f6;
* left: 50px;
* }
*
* #demo UL.activehover {
* position: absolute;
* background-color: #fcfcfc;
* left: 50px;
* }
*
* #demo A {
* text-decoration: none;
* }
*
* #demo LI {
* margin: 0px 0px 0px 0px;
* padding: 3px 3px 3px 3px;
* border-right: 1px solid #ddddff;
* border-left: 1px solid #ddddff;
* border-bottom: 1px solid #ddddff;
* }
*
* #demo LI.menu {
* background-color: #f6f6ff;
* }
*
* #demo LI.menuhover {
* background-color: #fff6f6;
* }
*
* #demo LI.active {
* background-color: #e9e9e9;
* }
*
* #demo LI.activehover {
* background-color: #fcfcfc;
* }
*
* {/literal}
* </style>
* </head>
* <body>
* {*
*
* Menu elements can be optionally added to the menu from the template
* too, via the "element" method or the "element_block" method, the
* difference between the two being that the displayed data is
* enclosed in a block function for "element_block" and is set via the
* "name" attribute for "element". Other than this difference, both
* methods have the same attributes. The only required attribute is
* "name" for "element". "element_block" has no require attributes.
*
* The additional attributes are the following:
*
* href: the page location this element will link to
*
* resource: currently an alias to href, this will likely change
*
* active: if set to TRUE, it will force this element to use the
* active and activehover css classes.
*
* parent: built to behave similarly to XML's XPath (and to easily
* facilitate adding XML support in the future), menu
* elements can need to declare it's parent as a fully
* qualified path of element ids. Omission of this
* attribute will result in the element being placed at
* the root level of the menu. This attribute is also
* error tolerant and will "roll up" if a parent does not
* exist or has not yet been declared.
*
* id: if left unset, the element id will be created from the
* element href first, then the element name.
*
* sort: not yet implemented. currently, all menu elements will
* displayed in the order declared, elements declared by
* template will be created AFTER all elements created by
* your program logic, and all children must be declared
* AFTER it's parent is declared. Changing this is a
* major TODO item.
*
* *}
*
* {demo->element_block href="?t1" active=true}
* Template 1
* {/demo->element_block}
*
* {demo->element name="Template 2" parent="f1"}
*
* {demo->element_block href="?t3" parent="/f1/Template_2"}
* Template 3
* {/demo->element_block}
*
* {demo->element_block href="?t4" parent="/f1/Template_2" }
* Template 4
* {/demo->element_block}
*
*
* {*
* the "classes" attribute consists of a series of 4 css classes
* in the following order: standard class, standard hover class, active
* class (suitable for indicating the current page), and active hover
* class. The only one required is the first standard class.
*
* The "id" attribute is optional, but facilitates css.
* *}
*
* {demo->menu classes="menu,menuhover,active,activehover" id="demo"}
* </body>
* </html>
*
* // EOF;
*/
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.