Page 1 of 1

List menu

Posted: Sat May 03, 2008 5:17 am
by DeFacto
Hello,
I'm trying to make list menu that gets values from database. Here is my code

Code: Select all

 
<form style="margin-left:300px" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="name" onChange="this.form.submit()">
<option >----------</option>
<?php
$hostname="localhost";
$mysql_login=" ";
$mysql_password=" ";
$database="menu";
 
if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
  die("Can't connect to database server.");
}else{
    if (!(mysql_select_db("$database",$db))){
      die("Can't connect to database.");
    }
}
$result = mysql_query( "SELECT id FROM pavadinimai" );
while ($get_info = mysql_fetch_row($result)){
print '<option value="<?php foreach ($get_info as $field) ?>>"<?php echo "$field";?></option>';  }
?>
</select>
</form>
 
but i get as much empty fields instead of values as are records in database table. Could anyone help me, please?

Re: List menu

Posted: Sat May 03, 2008 6:01 am
by matthewl

Code: Select all

 
<form style="margin-left:300px" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="name" onChange="this.form.submit()">
<option >----------</option>
<?php
$hostname="localhost";
$mysql_login=" ";
$mysql_password=" ";
$database="menu";
 
if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
  die("Can't connect to database server.");
}else{
    if (!(mysql_select_db("$database",$db))){
      die("Can't connect to database.");
    }
}
$result = mysql_query( "SELECT id FROM pavadinimai" );
while ($get_info = mysql_fetch_row($result)){
print '<option value="'.$get_info['id'].'">'.$get_info['id'].'</option>'; 
}
?>
</select>
</form>
 
Without cleaning up any other code other than whats needed that should fix the problem for you.

The print line contained php tags, even thos php was already running.

If you dont understand what i changed, give me a shout and i will explain a little more.

Re: List menu

Posted: Sat May 03, 2008 6:08 am
by DeFacto
Thanks for reply matthewl, i got your idea, but result is the same.

Re: List menu

Posted: Sat May 03, 2008 6:57 am
by matthewl
sorry my mistake i assumed (wrongly) without reading properly that you where using mysql_fetch_array

Code: Select all

 
 <form style="margin-left:300px" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <select name="name" onChange="this.form.submit()">
 <option >----------</option>
 <?php
 $hostname="localhost";
 $mysql_login=" ";
 $mysql_password=" ";
 $database="menu";
  
 if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
   die("Can't connect to database server.");
 }else{
     if (!(mysql_select_db("$database",$db))){
       die("Can't connect to database.");
     }
 }
 $result = mysql_query( "SELECT id FROM pavadinimai" );
 while ($get_info = mysql_fetch_row($result)){
 print '<option value="'.$get_info[0].'">'.$get_info[0].'</option>';
 }
 ?>
 </select>
 </form>
 

Re: List menu

Posted: Sat May 03, 2008 7:02 am
by DeFacto
Thanks a lot matthewl, it works :drunk:

Re: List menu

Posted: Sun May 04, 2008 12:19 am
by yacahuma
I really hate to see this kind of coding

Dont you think it will be better something like this

Code: Select all

 
 
$s1 = new STCSelect  (array('name' => 'name',  'options' =>$mgr->getNamesKV()));
$s1->draw();
 
 
Let me know if you are interested and I can post a link

Re: List menu

Posted: Sun May 04, 2008 2:51 am
by DeFacto
Yes, sure, i would like to know more.

Re: List menu

Posted: Sun May 04, 2008 8:56 am
by yacahuma
Basically what you want to do is remove all the database stuff from your code. Put it on a different class. I call them managers.

The manager is the one that knows how an object relates to tables in a database. An object could be saved in 1 table or 3 tables. Your code should not need to know that. Thats why you put that logic somewhere else. In this case in the manager. Your manager will be something like this

Code: Select all

 
class CustomerManager
{
   function getNameKV()
  {
    //get data from database and put it into an array  
  }
}
 
Then just use a library to hide all the code from going through the array and selecting the right option. You are going to be doing this so many times that you dont want to repeat this code everywhere. It makes your code harder to read.

here is a link to a library I created . There is nothing fancy about them but they work
http://www.stccorp.net/stclib.html

and some info about the database separation
http://yacahuma.blogspot.com/2007/12/dr ... n-php.html

Re: List menu

Posted: Sun May 04, 2008 3:29 pm
by Spartan101
yacahuma is referring to what's called a design pattern.

You may want to take a look at the Model-View-Control Design Pattern and the way it works. I just quickly googled a page that discusses it here:-

http://www.tonymarston.net/php-mysql/mo ... oller.html

Design Patterns give you layers of abstraction that hide away detail of the underlying database from users, making it easier to modify database structure and the code within your pages are as a result much cleaner. In the example code you posted, if you ever had to change a variable name or some of the database structure you'd have a bit of a nightmare attempting to amend all of the code especially in much larger projects.

It's a more advanced concept but will benefit you much much more if you get to grips with it early.

If you have a lot of database tables you can even use the following code generator to automatically create the code you want:-

http://www.phpobjectgenerator.com/

Re: List menu

Posted: Mon May 05, 2008 5:46 am
by DeFacto
Thanks guys, i will check that.