PHP - Problem Building Menu From Mysql And Do-while
I have a table in my db with all my menus and submenues of my site, I'd like to show them in some kind of menu containing all of them
each entry in db has the columns ID,NAME,TEXT,LEVEL,DEPENDENT level has a value of 1 or 2 depending if it's an item or a subitem dependent has a reference to the id of the section it belongs to Using dreamweaver I created two recordsets based on the level, they are called row_sections (only where level = 1) and row_subsections (only where level = 2) here is the code I tried to use: <?php do { ?> <p><a href="test.php?sec=<?php echo $row_sections['id'];?>"><?php echo $row_sections['name']; ?></a></p> //prints the section text <?php do { ?> <?php if($row_subsections['dependent'] == $row_sections['id']) { echo $row_subsections['name']."<br>"; //tries to print all subitems corresponding to the actual item } ?> <?php } while ($row_subsections = mysql_fetch_assoc($subsections)); ?> <?php } while ($row_sections = mysql_fetch_assoc($sections)); ?> Here's the output: Quote section 1 title subsection 1 subsection 2 section 2 title both section 1 and 2 should have two different subitems.. where is the problem? if I add the line <?php echo $row_sections['id']; ?> right before the second <?php do{ ?> it shows the corresponding ID for BOTH sections so I can't understand the problem... Similar TutorialsHi Guys I am trying to build a menu sing the code below, I can get the main headings, the problem arises when I try and produce the sub menus. Can someone please tell me where I am going wrong, or a better way of getting the desired result Code: [Select] <?php function submenu() { $submenu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '$menu_row[menu_id]'"); if(mysql_num_rows($submenu_sql)>0) { echo('<ul>'); while ($submenu_row=mysql_fetch_assoc($submenu_sql)) { echo('<li><a href="'.$submenu_row['link'].'">'.$submenu_row['menu_display'].'</a></li>'); } echo('</ul>'); }; } $menu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '0'"); if (mysql_num_rows($menu_sql)>0) { echo('<div id="accordian">'); while ($menu_row=mysql_fetch_assoc($menu_sql)) { echo('<div class="panel_container"> <h3><a href="'.$menu_row['link'].'">'.$menu_row['menu_display'].'</a></h3> <div class="panel_body">'); submenu(); echo('</div> </div>'); } echo('</div>'); } ?> I am getting the following error Notice: Undefined variable: menu_row in /home/sites/j-slink.co.uk/public_html/includes/menu.php on line 4 The desired result is to produce a menu with the following layout Code: [Select] <div id="accordion"> <div class="panel_container" id="panel1"> <h3 id="visible">Home</h3> <div id="panel1-body" class="panel_body"> </div> </div> <div class="panel_container" id="panel2"> <h3>Music</h3> <div id="panel2-body" class="panel_body"> <div> <ul> <li>Calling Card Mixing Tape</li> </ul> </div> </div> </div> <div class="panel_container" id="panel3"> <h3>Panel 3</h3> <div id="panel3-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel4"> <h3>Panel 4</h3> <div id="panel4-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel5"> <h3>Panel 5</h3> <div id="panel5-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> </div> Thanks in advance Hey Guys, I'm building a site and I need a drop down menu created dynamicly.... This is the flat html code Code: [Select] <div class="art-nav"> <div class="l"></div> <div class="r"></div> <ul class="art-menu"> <li><a href="URL 1"><span class="l"></span><span class="r"></span><span class="t">TOP LEVEL</span></a> <ul> <li><a href="URL 1">SUB CAT 1</a></li> <li><a href="URL 2">SUB CAT 2</a></li> <li><a href="URL 3">SUB CAT 3</a></li> </ul> </li> <li class="art-menu-li-separator"><span class="art-menu-separator"></span></li> <li><a href="URL 5"><span class="l"></span><span class="r"></span><span class="t">Contact Us</span></a></li> </ul> </div> This is the code that shows a dyamic code, but not in the menu... <?php wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'), 'show_thumbnails'=> get_option('show_category_thumbnails'))); ?> <a href="<?php wpsc_print_category_url();?>" class="wpsc_category_link"><?php wpsc_print_category_name();?></a> | <?php wpsc_print_subcategory(); ?> <?php wpsc_end_category_query(); ?> hello, I am trying to create a form that will log items. Say I have a list of fruits and I want to log the fruit and its color. I don't know how many fruits I have so I need the form to start with one row first then add rows after I hit enter. I then need to insert the fruits into mysql any ideas or links? anyone? I'm a newbie and want to explore on the use of php.I want to create a transcript system but do not know how to go about it.please where should i start from? This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=351877.0 Hi, I have this script which I would like to use to build an html form from a MySQL table. The <input text................ writes successfully to file but none of the other form types are written to file when using the elseif command Also, I would like this script to carry out the file write foreach row in the table It seems complicated and i am not sure if I am going about it in the right way, but here goes Code: [Select] //// Create Forms from MySQL $result = mysql_query("SELECT * FROM forms"); while($row = mysql_fetch_assoc($result)){ $field_label = $row['field_label']; $column_name = $row['column_name']; $field_type = $row['field_type']; if ($row['field_type'] = 'Text') { $stringData = "$field_label<input type=text name=$column_name>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Text Area') { $stringData = "$field_label<input type=textarea name=$column_name></textarea>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Select Menu') { $stringData = "$field_label<select name=$column_name></select>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Checkbox') { $stringData = "$field_label<input type=checkbox name=$column_name></textarea>\n"; fwrite($fh, $stringData); } fclose($fh); As always, any help is much appreciated Hi everyone, I hope I explain my problem well enough. I have created a cms with the help of a tutorial for my website, it allows me to click on the page from a menu and shows me the results. The menu list is taken from my mysql database, so for example i have homepage and recent news in my mysql table and these are what are shown in the menu. What I want is for the menu to to be images that can be clicked to take you to the correct page. I have attached a print screen to show you what it looks like at the minute and what I want the menu to look like. I have no idea if this is even possible can someone please help me out? Hopefully I will hear a reply, I will send my code if needed when I know whether it is possible or not. Thanks in hope I am tying to make my category menus mysql based but all my sub categories end up under the last category. Here is my code. Code: [Select] <ul class="sf-menu"> <li><a href="http://www.mysite.com/index.php">Home</a></li> <?PHP do { ?> <li><a href="news.php?c=<?PHP echo $row_bodynav['id']; ?>"><?PHP echo $row_bodynav['catname']; ?></a> <?PHP if ($row_bodynav['slug'] == $row_bodynav_sub['parent']) {echo '<ul>'; do { ?><li><a href="news.php?c=<?PHP echo $row_bodynav['id'];?>&sc=<?PHP echo $row_bodynav_sub['id']; ?>"><?PHP echo $row_bodynav_sub['subcatname']; ?></a></li> <?PHP } while ($row_bodynav_sub = mysql_fetch_assoc($bodynav_sub)); echo '</ul>';} ?> </li> <?PHP } while ($row_bodynav = mysql_fetch_assoc($bodynav)); ?> </ul> i am storing my menu in the database, i want to be able to output it by priority, heres so far what i have. I have no idea were to start. Database dump: -- -- Table structure for table `menu` -- CREATE TABLE IF NOT EXISTS `menu` ( `menu_access_lvl` int(2) NOT NULL, `priority` int(11) NOT NULL, `name` varchar(200) NOT NULL, `comment` text NOT NULL, `location` text NOT NULL, `creator_id` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `menu` -- INSERT INTO `menu` (`menu_access_lvl`, `priority`, `name`, `comment`, `location`, `creator_id`) VALUES (0, 1, 'Home Page', 'Home page', 'index.php', 'admin'), (0, 3, 'Contact', 'Contact', 'index.php?PG=contact', 'admin'), (0, 2, 'Events & Meetings', 'Events & Meetings', 'index.php?PG=events', 'admin'), (0, 4, 'About', 'About', 'index.php?PG=about', 'admin'), (2, 5, 'Admin', 'Admin', 'index.php?PG=admin', 'admin'); And here is the php code displaying it //gets the role of the user if set, otherwise role = 0 if(isset($_SESSION['SESS_MEMBER_ID']))$lvl = $_SESSION['SESS_ROLE']; else $lvl = 0; // this loads the menu buttons that correspond to the users role $menuqry="SELECT * FROM menu WHERE menu_access_lvl<='$lvl'"; $menuresult=mysql_query($menuqry); while($row = mysql_fetch_array($menuresult)){ echo "<li class=\"menuitem\"><a href=\"".$row['location']."\">".$row['name']."</a></li>"; } What this currently displays: Home Contact Events & meetings About I want it to be according to priority in the menu like: Home Events & meetings Contact About Hello everyone, So what I'm trying to do is have a dropdown menu displaying a number of <options> for people to select and to update that selection to the database, easy enough right? But I want that option to be displayed as the "selected" option when the page is revisited or refreshed and I just can't figure it out!!! (Permission to bang head on desk?) It would seem like it sould be a really basic thing to do but it's got me completely and a lot of menus around the site are going to rely on this so I came to you guys for help. A simple example would be like the facebook edit profile page, the user selects whether they are Male or Female, the database gets updated and when you return the option you selected before is the one that appears as if selected="selected" had been done. I've tried everything I can think of (all be it from a learners perspective) with no joy, ive managed to get the database connection sorted, the tables done, the login with unique id $_SESSION, logout etc... so then when I got to this I thought... easy LOL yeah right. Some of this probably doesnt even make sense but I'll show you the kind of things I've tried... <select name="gender" size="1" id="gender"> <option value="male" <?php if ($gender == "male") {echo 'selected="selected"';} ;?>>Male</option> <option value="female" <?php if ($gender == "female") {echo 'selected="selected"';} ;?>>Female</option> </select> OR <select name="gender" id="gender"> <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option> <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option> </select> OR <select name="gender" size="1" id="gender"> <option selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="<?php if ($gender == "Male") {echo "selected";} else {echo "male";} ;?>">Male</option> <option value="<?php if ($gender == "Female") {echo "selected";} else {echo "female";} ;?>">Female</option> </select> OR <select name="gender" id="gender"> <option value="male"><?php if ($gender == "male") {echo "Male";} ;?></option> <option value="female"><?php if ($gender == "female") {echo "Female";} ;?></option> </select> Honestly man, I've got no idea. The other thing is, I have more than 1 dropdown menu in the same form (5 in total) and if I use 2 or more selecting different options as I go I get a blank screen. And one more, if I have selected Male and it updates the users row and I resubmit Male again it's blank screen time again, lol. Any help would be tremendous and greatly appreciated. Thanks very much, Learner P.S Man! I wonder whether someone can help me please. I've found http://www.plus2net.com/php_tutorial/ajax-listbox.php tutorial to create a drop down menu using mySQL table data, which, in turn returns a list of results on the page. Following this tutorial I've put together the tables in my database and the required scripts as shown in the tutorial with the one exception, the "z_db.php" file, which I've assumed to be: Code: [Select] <?php mysql_connect("host", "user", "password")or die(mysql_error()); mysql_select_db("database"); ?> The problem I have, is that when I try and run this, I receive the following error: Quote Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/2/d333603417/htdocs/development/catsearch.php on line 91 which is this line in the search form: echo "</head><body onload="ajaxFunction()";>";. I must admit I've guessed as to the structure of the 'z_db.php' file should look like because this is not shown so perhaps this is the problem. I just wondered wether someone could perhaps take a look at this please and let me know where I've gone wrong. Many thanks and kind regards I have a mysql table with the structure of Code: [Select] ID Menu_Name Parent_ID 1 Finance NULL 2 Business NULL 3 Investment 1 4 Trading 2 How can I create a html <ul><li> list based on the parent? Hi all, I am currently learning PHP and have the homework to produce a function that can delete a row in a MySQL database table by clicking on an item in a drop-down menu in a web page. The code I have produced up until now is this: <!DOCTYPE HTML> <html lang="de"> <head> <meta charset="utf-8" /> <title>E3_Artikel_Löschen</title> </head> <body> <form method = "GET"> <?php $anr=""; try { $pdo = new PDO ('mysql:dbname=bestelldatenbank;host=localhost;charset=utf8', 'root', ''); } catch (PDOException $error){ die ($error->getMessage()); } ?> <div> <p> <label for="artikel">Artikel: </label> <select id="artikel" name="artikel"> <?php $sqlSelect = "SELECT anr, name FROM artikel ORDER BY anr ASC"; foreach ($pdo->query($sqlSelect) as $row) { echo "<option value=$row[0]>$row[0] | $row[1]</option>\n"; $anr = $row[0]; } ?> </select> <input type = "submit" value = "Delete row" /> </p> </div> <?php function artLoeschen($anr) { echo "Function called $anr"; if(isset($_GET[$anr])) { $anr = $_GET[$anr]; $sqlDelete = $pdo->query("DELETE FROM artikel WHERE anr = :anr"); if ($stmt = $pdo->prepare($sqlDelete)) { $stmt->bindParam(':anr', $anr); $stmt->execute(); } echo "<h2><b>Artikel gelöscht!</b></h2>"; } } ?> </form> </body> </html> So, I have observed the following when I run the script in a browser: 1. The HTML works as expected and I get a drop-down list with the article number and description of each item in the affected table. 2. I can click on an item in the list and it populates the top item in the drop-down list. 3. When I click delete row, the selected item is not deleted. 4. There are no error messages returned but the function is not executed (at least not as I would like to expect).
I have obviously missed something or made a mistake in my code. I would be very grateful for any help...this is driving me mad! :) Regards, Kevin your MySQL server version: 5.1.36 Code: [Select] SELECT * FROM game_weapons Table: Picture Attached the EXPLAIN output for your query, if applicable: I wish to connect a drop down menu's selection of weapons with the correlated table information. What do I want to happen: Click on a drop down Menu and have a list of weapons, these weapons are associated with a set number in the database and passed onto the next screen. (The larger number wins, this part I have figured out). <p> <select name="weapon2" style="font-size:20px;font-family:Arial;width:275px"> <option value="power">Power</option> <option value="intelligence">Intelligence</option> <option value="speed">Speed</option> <option value="reserve">Reserve</option> </select> </p> I do have the battle code figured out! (This should be the last step). Alright so I created a MySQL database that has 5 tables each named a brand of a dirt bike. In each table their are 2 fields, one INDEX_ID and MODELS. Under that for rows I have every model bike named. A quick question before I get onto what I want to do: Can data be added underneath the rows? For example, users will be able to submit information about each bike model. If each bike model is a row, can there be a category past that row or do I need to make each field a model name and just have a ton of fields and have the rows be the information users submit. To make it easier to understand I'll post the SQL code for the brand Honda: CREATE TABLE `Honda` ( `INDEX_ID` int(3) NOT NULL auto_increment, `MODELS` varchar(20) collate latin1_general_ci NOT NULL, PRIMARY KEY (`INDEX_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=23 ; -- -- Dumping data for table `Honda` -- INSERT INTO `Honda` VALUES(1, 'CR85'); INSERT INTO `Honda` VALUES(2, 'CR125'); INSERT INTO `Honda` VALUES(3, 'CR250'); INSERT INTO `Honda` VALUES(4, 'CRF100'); INSERT INTO `Honda` VALUES(5, 'CRF150'); INSERT INTO `Honda` VALUES(6, 'CRF230'); INSERT INTO `Honda` VALUES(7, 'CRF250X'); INSERT INTO `Honda` VALUES(8, 'CRF250R'); INSERT INTO `Honda` VALUES(9, 'CRF450X'); INSERT INTO `Honda` VALUES(10, 'CRF450R'); INSERT INTO `Honda` VALUES(11, 'CRF50'); INSERT INTO `Honda` VALUES(12, 'CRF70'); INSERT INTO `Honda` VALUES(13, 'CRF80'); INSERT INTO `Honda` VALUES(14, 'XR650'); INSERT INTO `Honda` VALUES(15, 'CR500'); INSERT INTO `Honda` VALUES(16, 'XR100'); INSERT INTO `Honda` VALUES(17, 'XR200'); INSERT INTO `Honda` VALUES(18, 'XR250'); INSERT INTO `Honda` VALUES(19, 'XR400'); INSERT INTO `Honda` VALUES(20, 'XR50'); INSERT INTO `Honda` VALUES(21, 'XR70'); INSERT INTO `Honda` VALUES(22, 'XR80'); Anyway I still need to figure out how to have this under a form that a user can use to select the bike they want to submit information about. A code like this perhaps?: <? $connection = mysql_connect("localhost","user","pass"); $fields = mysql_list_fields("dbname", "table", $connection); $columns = mysql_num_fields($fields); echo "<form action=page_to_post_to.php method=POST><select name=Field>"; for ($i = 0; $i < $columns; $i++) { echo "<option value=$i>"; echo mysql_field_name($fields, $i); } echo "</select></form>"; ?> Thanks. Hi all I need to combine these two scripts: Firstly, the following decides which out of the following list is selected based on its value in the mySQL table: <select name="pack_choice"> <option value="Meters / Pack"<?php echo (($result['pack_choice']=="Meters / Pack") ? ' selected="selected"':'') ?>>Meters / Pack (m2)</option> <option value="m3"<?php echo (($result['pack_choice']=="m3") ? ' selected="selected"':'') ?>>Meters / Pack (m3)</option> <option value="Quantity"<?php echo (($result['pack_choice']=="Quantity") ? ' selected="selected"':'') ?>>Quantity</option> </select> Although this works OK, I need it also to show dynamic values like this: select name="category"> <?php $listCategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC"); while($categoryReturned=mysql_fetch_array($listCategories)) { echo "<option value=\"".$categoryReturned['name']."\">".$categoryReturned['name']."</option>"; } ?> </select> I'm not sure if this is possible? Many thanks for your help. Pete I am working on a project where I want a select form to display information from a MySQL table. The select values will be different sports (basketball,baseball,hockey,football) and the display will be various players from those sports. I have set up so far two tables in MySQL. One is called 'sports' and contains two columns. Once called 'category_id' and that is the primary key and auto increments. The other column is 'sports' and contains the various sports I mentioned. For my select menu I created the following code. <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "You did not successfully connect to the DB!" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Error connecting to the database test!"); ?> <html> <head>Display MySQL</head> <body> <form name="form2" id="form2"action="" > <select name="categoryID"> <?php $sql = "SELECT category_id, sport FROM sports ". "ORDER BY sport"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"".$row['category_id']."\">".$row['sport']."</option>\n "; } ?> </select> </form> </body> </html> this works great. I also created another table called 'players' which contains the fields 'player_id' which is the primary key and auto increments, category_id' which is the foreign key for the sports table, sport, first_name, last_name. The code I am using the query and display the desired result is as follows <html> <head> <title>Get MySQL Data</title> </head> <body> <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "Err:Db" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Err:Db"); #create the query $sql ="SELECT * FROM sports INNER JOIN players ON sports.category_id = players.category_id WHERE players.sport = 'Basketball'"; #execute the query $rs = mysql_query($sql,$conn); #write the data while( $row = mysql_fetch_array( $rs) ) { echo ("<table border='1'><tr><td>"); echo ("Caetegory ID: " . $row["category_id"] ); echo ("</td>"); echo ("<td>"); echo ( "Sport: " .$row["sport"]); echo ("</td>"); echo ("<td>"); echo ( "first_name: " .$row["first_name"]); echo ("</td>"); echo ("<td>"); echo ( "last_name: " .$row["last_name"]); echo ("</td>"); echo ("</tr></table>"); } ?> </body> </html> this also works fine. All I need to do is tie the two together so that when a particular sport is selected, the query will display below in a table. I know I need to change my WHERE clause to a variable. This is what I need help with. thanks Hey guys, I'm not exactly sure what the term used for this is, but I'll try explaining it very thoroughly. My site I'm building is this: http://r-gaming.net/urban-rp/home/ On index.php file, I would like it so you if you're logged in (I have a logged in system already scripted), you're able to post current news. Something like this: http://ls-rp.com/ Would anyone willing to help me by linking a tutorial to code this method 'latest news' tool? Would be greatly appreciated. Crayon Hi guys, I have recently been asked by a freinds to turn his web-site in to a CMS web site and i was just wondering if implementing it in php is the write way to go about it. I have never realy build anything like a CMS so if you guys have tips for me that would be spot on. I was thinking how can I make a script that will build strings incrementing through each letter starting with "a" and ending with "zzzzzz" or what ever ending length I want such as 12. So if your were to output everything, you could find the words: "dog" "spider" "superman" "hippo" etc What would be the best way to do this, so I don't have to have a loop that is 500 blocks deep to show a word that is 500 letters example Quote a ... z aa ab ac ... az ba bb bc ... ... aaa aab aac |