Jump to content

Recommended Posts

Posted (edited)

Got the code below

 




  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 '';
echo ''.$value.'';
echo '';
}
$count++; 

}

//'mysql_tablename($tables,$count).'';
//echo mysql_tablename($tables,$count)."
";
    

?>

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 tag

Edited by mac_shinobi
Posted

You want your select tags to be outside your loop so they're not repeated:

echo '';
foreach ($category as $key => $value) {
   echo ''.$value.'';
}
echo '';

 

Change your tag to a tag and you should be golden.

Posted (edited)
You want your select tags to be outside your loop so they're not repeated:

echo '';
foreach ($category as $key => $value) {
   echo ''.$value.'';
}
echo '';

Change your tag to a tag and you should be golden.

 

tried that and still doing the same - copied and pasted the code that you posted

Edited by mac_shinobi
Posted (edited)

Ahh... Is it because your foreach loop is happening inside your while loop? Methinks it probably is!

Does this serve you any better?

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 '';
foreach ($category as $key => $value) {
   echo ''.$value.'';
}
echo '';

//'mysql_tablename($tables,$count).'';
//echo mysql_tablename($tables,$count)."
";
?>

Edited by dayzd
Small syntax error
  • Thanks 1
Posted
Ahh... Is it because your foreach loop is happening inside your while loop? Methinks it probably is!

Does this serve you any better?

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 '';
foreach ($category as $key => $value) {
   echo ''.$value.'';
}
echo '';

//'mysql_tablename($tables,$count).'';
//echo mysql_tablename($tables,$count)."
";
?>

 

 

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

Posted
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 '';
foreach ($category as $key => $value) {
echo ''.$value.'';
}
echo '';
?>

  • Thanks 1
Posted

Try this

mysql_connect("localhost","backdoor","(*****)")

$tables = mysql_query("SHOW TABLES from dbase");

echo '';
while ($row = mysql_fetch_row($tables)) {
   echo ''.$row[0].'';
}
echo '';

//'mysql_tablename($tables,$count).'';
//echo mysql_tablename($tables,$count)."
";
?>

  • Thanks 1
Posted
Try this

mysql_connect("localhost","backdoor","(*****)")

$tables = mysql_query("SHOW TABLES from dbase");

echo '';
while ($row = mysql_fetch_row($tables)) {
   echo ''.$row[0].'';
}
echo '';

//'mysql_tablename($tables,$count).'';
//echo mysql_tablename($tables,$count)."
";
?>

 

I get

 

 

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

Posted
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()).

Posted
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 ').
Posted
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 '';
foreach ($category as $key => $value) {
   echo ''.$value.'';
}
echo '';
?>

 

That did the trick - thanks

 

any chance at some point you can explain the code ?

Posted

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 ''; // Start select dropdown
foreach ($category as $key => $value) { // Loop through all $category values
   echo ''.$value.''; // Add option to select dropdown
}
echo ''; // Close select dropdown

 

For the record, CESIL's way is more concise and efficient. I just didn't think of it.

Posted
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 ''; // Start select dropdown
foreach ($category as $key => $value) { // Loop through all $category values
   echo ''.$value.''; // Add option to select dropdown
}
echo ''; // 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 ?

Posted

I've had a quick poke at CESIL's code (couldn't help myself!):

mysql_connect("localhost","backdoor","(*****)");

$tables = mysql_query("SHOW TABLES from dbase");

echo '';
while ($row = mysql_fetch_array($tables, MYSQL_NUM)) {
   echo ''.$row[0].'';
}
echo '';
?>

 

It's the while ($row = mysql_fetch_array($tables, MYSQL_NUM)) { line that needed changing.

 

Now you have two options!

Posted
I've had a quick poke at CESIL's code (couldn't help myself!):

 

 

It's the while ($row = mysql_fetch_array($tables, MYSQL_NUM)) { line that needed changing.

 

Now you have two options!

 

changing to what ?

 

changing MYSQL_NUM to a value ie 1, 2 or w/e or what exactly ?

Posted

It was previously "while ($row = mysql_fetch_row($tables)) {".

 

Change it to "while ($row = mysql_fetch_array($tables, MYSQL_NUM)) {" and it'll work.

 

Or at least it does for me...!

 

NB: MYSQL_NUM is an option of the 'mysql_fetch_array' command, not something you need to change. The other options are MYSQL_ASSOC and MYSQL_BOTH

PHP: mysql_fetch_array - Manual

  • Thanks 1
Posted
It was previously "while ($row = mysql_fetch_row($tables)) {".

 

Change it to "while ($row = mysql_fetch_array($tables, MYSQL_NUM)) {" and it'll work.

 

Or at least it does for me...!

 

NB: MYSQL_NUM is an option of the 'mysql_fetch_array' command, not something you need to change. The other options are MYSQL_ASSOC and MYSQL_BOTH

PHP: mysql_fetch_array - Manual

 

Are there any other good php / sql how to's as ideally am trying to put something together here whereby my db has a list of tables which include something along the lines of

 

1. Models of Devices ie photocopiers, printers etc

 

2. Relevant List of error codes and what they mean and what to do to resolve each error code with the ability to add extra info to the how to resolve field

 

3. Relevant List of service modes and what they do

 

With regards to item 2 above for arguements sake lets say I had error code

 

235 and currently error code for 235 said to get a new network cable and I wanted to add more possible things to try then I could ammend it and say add check network patch panel is patched in and that you can ping the device via its ip address and some other things to do.

 

Also in order to get into this system I need an extra table to store login details for each user who is allowed to get into this system so as to make changes and view all of this info.

 

Clearly I have a long way to go but am just trying to piece the system together first and then worry about how to do a valid_users table and then do the authentication before they are allowed to access the online system

 

Hope that makes sense

 

Also with the database side I need to ensure that the database is setup correctly so im not replicating data etc ( there is a word for it but cant remember it off the top of my head, doh ! )

Posted

Normalisation ;) That sounds like a fairly involved system to build from scratch. Have you looked into pre-existing solutions - there's bound to be some available.

 

Not too sure about online references (I pick snippets up from all over the place, as I end up doing a lot of Googling when I build things), but if you're up for some hard-copy, I learnt a lot from the second edition of this book (now on ed3): PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide.

You can currently [ame=http://www.amazon.co.uk/PHP-MySQL-Dynamic-Web-Sites/dp/032152599X/]get it from Amazon.co.uk[/ame] for a princely £15.

  • Thanks 1
Posted

How about something simpler mac_shinobi? How about an Expert System.

 

This type of system asks you questions and gives you answers, which lead you to further questions - narrowing down the choices until you have got what you want or not.

 

So, say, on 'Node 1' you'd have the question 'What type of device is it?' with 'Printer, Copier' etc... then each link is to a new node 'Node 2' is Printer, with the question of 'Which model is it?' and a list of models. And so on and so forth?

 

An example of such a system (a simple one I will be honest, as it has no other additions) is robopebble. Robopebble | The Programming Pebble

 

If not, that system you are talking of should be relatively easy. You can do the login system using the PEAR::Auth package (save you reinventing the wheel), and the listing of data is fairly simple too.

 

Will error codes be shared between devices?

  • Thanks 1
Posted (edited)
How about something simpler mac_shinobi? How about an Expert System.

 

This type of system asks you questions and gives you answers, which lead you to further questions - narrowing down the choices until you have got what you want or not.

 

So, say, on 'Node 1' you'd have the question 'What type of device is it?' with 'Printer, Copier' etc... then each link is to a new node 'Node 2' is Printer, with the question of 'Which model is it?' and a list of models. And so on and so forth?

 

An example of such a system (a simple one I will be honest, as it has no other additions) is robopebble. Robopebble | The Programming Pebble

 

If not, that system you are talking of should be relatively easy. You can do the login system using the PEAR::Auth package (save you reinventing the wheel), and the listing of data is fairly simple too.

 

Will error codes be shared between devices?

 

error codes vary between devices and can give those a try so thanks for the suggestion but would still be good to do something in php / sql myself so I can customize it a bit more. Also would be nice to learn php / sql at the same time

 

Back to the php side of things is it possible to get a select names value in php so when I am changing between the values in the select name I can change the querry ie

 

I select tblMachines and it querries tbl machines or if i select tbl service codes it querries tblservicecodes etc etc

Edited by mac_shinobi
Posted

Do you want this to happen automatically when you change the Select dropdown's value, or when you click a submit button?

 

Using a submit button in a form is easier as you should get all variables inside the $_GET or $_POST global (i.e. $_GET['category']) when you submit the form to PHP to handle.

 

If you want it to happen automatically as you change the value, you're gonna have to attach a Javascript function to the onChange() event handler that reads the value and posts it off to the server (php), then handles whatever comes next - navigating to a new page, or inserting new content into the existing one.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...