Dynamic Pulldown Menu question

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
Handler
Forum Newbie
Posts: 9
Joined: Wed Oct 13, 2004 9:02 pm
Location: Albany, NY USA
Contact:

Dynamic Pulldown Menu question

Post by Handler »

nigma | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Greetings!

I have this little piece of code I've been playing with.  I'm learning php/mysql bit by bit.  And to me it seems really drawn out on reading the reocords from the database untill the end.  Is there a more 'compact' method of doing this?

Here's what I have so far (it works).

Code: Select all

<html>
   <head>
   </head>
   <body>
      <form method=post action="index.php">
         <select name="rating">
 <?php
include_once ("./settings.php");
$link = mysql_connect($host,$user,$password);
$result=mysql_db_query($dbname,"select * from months",$link);
while($row = mysql_fetch_array($result)) {
   if ($result > 0) {
      $num4=mysql_num_rows($result);
      if($num4!=0) {
         $n=0;
         while ( $n<$num4 ) {
            $sign=mysql_result($result,$n,"sign");
            $n++;
            if (!$size) $size="0 kb";
            echo "<option selected value='$sign'>$sign</option>\n";
         }
      }
   }
}
?>
         </select>
         <input type="submit" value="next">
      </form>
   </body>
</html>
Thanks for any help,pointers,tips and/or constructive criticism!

Handler
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What working with dynamic menus I tend to find putting all the items into an array and then outputting it the way you want. This is a lot more flexible then simply pulling raw info from your database.
Handler
Forum Newbie
Posts: 9
Joined: Wed Oct 13, 2004 9:02 pm
Location: Albany, NY USA
Contact:

Post by Handler »

Phenom ..

Thanks for your reply. Could you give me perhaps a brief sample of code? Nothing overly to sophisticated .. just a taste. I can research it from there!

Thanks!

Handler
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It depends on your database structure.

I like to categorize which links get put in each category. So I would do something like

Code: Select all

<?

while ($result = mysql_fetch_assoc)
{

$links[$category][] = $sign;

?>
}

Then you can organize them however you want ( in alphebetical order, etc ). OHHH the options are limitless
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What are you trying to do inside your while() loop. I think it can be simplified greatly.

Counter to ~Phenom's opinion, I find it somewhat wasteful to read a query's results into an array, just to dump it out again. Unless that query data will be used in multiple locations I'd think this approach would be wasteful both in overhead, and file length (code complexity). Are there other factors I'm not remembering?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

imho, for a simple script it doesn't really matter, but after a while, the matter: "separate code/data retrieval from markup/output" will rise ;)
Handler
Forum Newbie
Posts: 9
Joined: Wed Oct 13, 2004 9:02 pm
Location: Albany, NY USA
Contact:

Post by Handler »

nigma | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Ok.. I tried something different, from something I found.

Code: Select all

$num=mysql_num_rows($result);
$cur=0;
while($cur<$num)
{
$row=mysql_fetch_array($result);
#echo $row["sign"]."<BR>";
echo "<option selected value=" .$row['sign']. ">" .$row['sign']. "</option>\n";
$cur++;
}
This is a bit more straight forward and makes sense.

Although when I went to research what it actualy does in the mysql doc its not there! Just like the mysql_fetch_assoc function you have. Where do I find these?

Next thing I've been fighting with are the periods (.) in PHP. What exactly are they for and how do they work in a nutshell?!

The database is something I dreamed up to pratice with. The name of the db is zodiac, the table name is months and it has four fields they are signs, bcolor, tcolor, stone. I figured there is no tut for this, it finite and it just simple!

I'd like to learn more about that catagorizing (spelling) .. looks like something that I'll need down the road.

Pickle and timvw; I can understand your reasoning. This is just simple stuff to get my feet wet and progressivly move into the harder material. Granted I would never try to build a pulldown menu from a million things .. a few is cool though.

Again .. thanks for your help!

Handler
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The '.' operator concatenates:

Code: Select all

$my_val = 22;
$my_var = "My age is: ".$my_val;

echo $my_val;

//would echo "My age is: 22";
You can also simplify your while loop a bit. Your 9 lines can become just:

Code: Select all

while($row = mysql_fetch_array($result))
{
     echo "<option selected value = ".$row['sign']. ">" .$row['sign']. "</option>\n";
}
if mysql_fetch_array() doesn't have any more rows, it'll return false which will exit the while loop
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply