List menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

List menu

Post 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?
matthewl
Forum Newbie
Posts: 13
Joined: Sat May 03, 2008 5:28 am

Re: List menu

Post 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.
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: List menu

Post by DeFacto »

Thanks for reply matthewl, i got your idea, but result is the same.
matthewl
Forum Newbie
Posts: 13
Joined: Sat May 03, 2008 5:28 am

Re: List menu

Post 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>
 
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: List menu

Post by DeFacto »

Thanks a lot matthewl, it works :drunk:
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: List menu

Post 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
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: List menu

Post by DeFacto »

Yes, sure, i would like to know more.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: List menu

Post 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
Spartan101
Forum Newbie
Posts: 12
Joined: Mon Feb 25, 2008 4:56 pm
Location: Manchester (UK)

Re: List menu

Post 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/
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: List menu

Post by DeFacto »

Thanks guys, i will check that.
Post Reply