+ Post New Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
Web Development Thread, PHP ? in Coding and Web Development; Got the code below Code: <?php mysql_connect("localhost","backdoor","(*****)"); $tables = mysql_list_tables("dbase"); $count = 0; while ($count < mysql_numrows($tables)) { $category = ...
  1. #1

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361

    PHP ?

    Got the code below

    Code:
    <?php
    
    
    
       mysql_connect("localhost","backdoor","(*****)");
    
       $tables = mysql_list_tables("dbase");
       $count = 0;
    
       while ($count < mysql_numrows($tables)) {
         
    
    $category = array(mysql_tablename($tables,$count).",");
    
    
    $category = str_replace(",", "", $category);
    
    
    foreach ($category as $key => $value)
    {
    echo '<SELECT name=category>';
    echo '<OPTION value='.$value.'>'.$value.'</value>';
    echo '</select>';
    }
     $count++; 
    
    }
    
     //'mysql_tablename($tables,$count).'';
     //echo mysql_tablename($tables,$count)."<br />";
         
    
    ?>
    It seems to put 3 seperate drop down menus and I can't figure out the exact coding for the drop down menu as I want all items in one drop down menu but for some reason its ignoring the </value> tag
    Last edited by mac_shinobi; 14th January 2010 at 08:58 AM.

  2. #2
    dayzd's Avatar
    Join Date
    Nov 2009
    Location
    In front of computer
    Posts
    403
    Thank Post
    74
    Thanked 59 Times in 48 Posts
    Rep Power
    24
    You want your select tags to be outside your loop so they're not repeated:
    Code:
    echo '<SELECT name=category>';
    foreach ($category as $key => $value) {
        echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    Change your </value> tag to a </option> tag and you should be golden.

  3. #3

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by abullett View Post
    You want your select tags to be outside your loop so they're not repeated:
    Code:
    echo '<SELECT name=category>';
    foreach ($category as $key => $value) {
        echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    Change your </value> tag to a </option> tag and you should be golden.
    tried that and still doing the same - copied and pasted the code that you posted
    Last edited by mac_shinobi; 14th January 2010 at 08:59 AM.

  4. #4
    dayzd's Avatar
    Join Date
    Nov 2009
    Location
    In front of computer
    Posts
    403
    Thank Post
    74
    Thanked 59 Times in 48 Posts
    Rep Power
    24
    Ahh... Is it because your foreach loop is happening inside your while loop? Methinks it probably is!
    Does this serve you any better?
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)");
    
    $tables = mysql_list_tables("dbase");
    $count = 0;
    
    while ($count < mysql_numrows($tables)) {
        $category = array(mysql_tablename($tables,$count).",");
        $category = str_replace(",", "", $category);
        $count++;
    }
    
    echo '<select name=category>';
    foreach ($category as $key => $value) {
        echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    
    //'mysql_tablename($tables,$count).'';
    //echo mysql_tablename($tables,$count)."<br />";
    ?>
    Last edited by dayzd; 14th January 2010 at 09:05 AM. Reason: Small syntax error

  5. Thanks to dayzd from:

    mac_shinobi (14th January 2010)

  6. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by abullett View Post
    Ahh... Is it because your foreach loop is happening inside your while loop? Methinks it probably is!
    Does this serve you any better?
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)");
    
    $tables = mysql_list_tables("dbase");
    $count = 0;
    
    while ($count < mysql_numrows($tables)) {
        $category = array(mysql_tablename($tables,$count).",");
        $category = str_replace(",", "", $category);
        $count++;
    }
    
    echo '<select name=category>';
    foreach ($category as $key => $value) {
        echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    
    //'mysql_tablename($tables,$count).'';
    //echo mysql_tablename($tables,$count)."<br />";
    ?>

    That fixed the first issue but now it is only showing the first table in the drop down menu so I only get one item in the drop down as apposed to all 3 table names

  7. #6
    dayzd's Avatar
    Join Date
    Nov 2009
    Location
    In front of computer
    Posts
    403
    Thank Post
    74
    Thanked 59 Times in 48 Posts
    Rep Power
    24
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)");
    
    $tables = mysql_list_tables("dbase");
    $count = 0; $category = Array();
    
    while ($count < mysql_numrows($tables)) {
    	$category[] = mysql_tablename($tables,$count);
    	$count++;
    }
    
    echo '<select name=category>';
    foreach ($category as $key => $value) {
    	echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    ?>

  8. Thanks to dayzd from:

    mac_shinobi (14th January 2010)

  9. #7

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,403
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Try this
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)")
    
    $tables = mysql_query("SHOW TABLES from dbase");
    
    echo '<select name=category>';
    while ($row = mysql_fetch_row($tables)) {
        echo '<option value='.$row[0].'>'.$row[0].'</option>';
    }
    echo '</select>';
    
    //'mysql_tablename($tables,$count).'';
    //echo mysql_tablename($tables,$count)."<br />";
    ?>

  10. Thanks to CESIL from:

    mac_shinobi (14th January 2010)

  11. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by CESIL View Post
    Try this
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)")
    
    $tables = mysql_query("SHOW TABLES from dbase");
    
    echo '<select name=category>';
    while ($row = mysql_fetch_row($tables)) {
        echo '<option value='.$row[0].'>'.$row[0].'</option>';
    }
    echo '</select>';
    
    //'mysql_tablename($tables,$count).'';
    //echo mysql_tablename($tables,$count)."<br />";
    ?>
    I get


    Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\xampp\htdocs\xampp\dbasetables.php on line 3

  12. #9

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,400
    Thank Post
    636
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319
    Quote Originally Posted by mac_shinobi View Post
    Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\xampp\htdocs\xampp\dbasetables.php on line 3
    Add a ; (semicolon) at the end of the first line (mysql_connect()).

  13. #10

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,529
    Thank Post
    513
    Thanked 2,406 Times in 1,862 Posts
    Blog Entries
    24
    Rep Power
    822
    Just as a minor thing the 'select name=category' should be 'select name="category"' and the 'option value='blah'> should be 'option value="'blah'">' - This is a HTML thing (ie. all attributes should be contained in " or ').

  14. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by abullett View Post
    Code:
    <?php
    mysql_connect("localhost","backdoor","(*****)");
    
    $tables = mysql_list_tables("dbase");
    $count = 0; $category = Array();
    
    while ($count < mysql_numrows($tables)) {
        $category[] = mysql_tablename($tables,$count);
        $count++;
    }
    
    echo '<select name=category>';
    foreach ($category as $key => $value) {
        echo '<option value='.$value.'>'.$value.'</option>';
    }
    echo '</select>';
    ?>
    That did the trick - thanks

    any chance at some point you can explain the code ?

  15. #12
    dayzd's Avatar
    Join Date
    Nov 2009
    Location
    In front of computer
    Posts
    403
    Thank Post
    74
    Thanked 59 Times in 48 Posts
    Rep Power
    24
    Code:
    while ($count < mysql_numrows($tables)) { // Loop through all rows in $tables result
        $category[] = mysql_tablename($tables,$count); // Add table name from selected row to $category array
        $count++; // Increment counter
    }
    
    echo '<select name=category>'; // Start select dropdown
    foreach ($category as $key => $value) { // Loop through all $category values
        echo '<option value='.$value.'>'.$value.'</option>'; // Add option to select dropdown
    }
    echo '</select>'; // Close select dropdown
    For the record, CESIL's way is more concise and efficient. I just didn't think of it.

  16. #13

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by abullett View Post
    Code:
    while ($count < mysql_numrows($tables)) { // Loop through all rows in $tables result
        $category[] = mysql_tablename($tables,$count); // Add table name from selected row to $category array
        $count++; // Increment counter
    }
    
    echo '<select name=category>'; // Start select dropdown
    foreach ($category as $key => $value) { // Loop through all $category values
        echo '<option value='.$value.'>'.$value.'</option>'; // Add option to select dropdown
    }
    echo '</select>'; // Close select dropdown
    For the record, CESIL's way is more concise and efficient. I just didn't think of it.
    How do you get cecils to work then ?

  17. #14
    dayzd's Avatar
    Join Date
    Nov 2009
    Location
    In front of computer
    Posts
    403
    Thank Post
    74
    Thanked 59 Times in 48 Posts
    Rep Power
    24
    Oh, I haven't tested it...! :P I'm just assuming it does!

  18. #15

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,680
    Thank Post
    3,210
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by abullett View Post
    Oh, I haven't tested it...! :P I'm just assuming it does!
    where does PHP lesson 101 begin ? Thinks me needs more practise and lessons

    Also thanks for the help !!!

SHARE:
+ Post New Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. PHP
    By faza in forum How do you do....it?
    Replies: 4
    Last Post: 3rd December 2008, 01:56 PM
  2. PHP ID
    By Jackd in forum Coding
    Replies: 4
    Last Post: 30th November 2008, 01:08 PM
  3. PHP Help!
    By Cravon in forum Coding
    Replies: 2
    Last Post: 20th November 2007, 05:05 PM
  4. Help with PHP
    By Gatt in forum Web Development
    Replies: 1
    Last Post: 14th November 2006, 09:01 PM
  5. php help
    By beeswax in forum Windows
    Replies: 33
    Last Post: 14th October 2005, 04:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •