Page 1 of 1

Dynamic Pulldown Menu question

Posted: Wed Oct 13, 2004 9:15 pm
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

Posted: Wed Oct 13, 2004 9:31 pm
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.

Posted: Thu Oct 14, 2004 4:15 am
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

Posted: Thu Oct 14, 2004 6:34 am
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

Posted: Thu Oct 14, 2004 9:54 am
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?

Posted: Thu Oct 14, 2004 10:00 am
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 ;)

Posted: Thu Oct 14, 2004 11:28 am
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

Posted: Thu Oct 14, 2004 2:53 pm
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