rocky48 Posted August 31, 2015 Share Posted August 31, 2015 I am trying to write some code to extract values from a mysql database to form a menu. my existing site uses an unordered list and places them in the tabs at the top of the page. (see my website www.1066cards4u.co.uk). I have tried using while(list($key, $value) = each($a)) type code to extract the url's. In this case I have excluded $key as I only want $value which is the full url including the <li> tags. when I run this code it repeats each $value twice. Here is the section of the code that produces the menu: <?php include ("connect_Menu_LOCAL.php"); $conn = get_db_conn_Menu(); $menu_sql = "SELECT Url FROM dtop"; $menu_res = mysqli_query($conn, $menu_sql) or die(mysqli_error($conn)); if (mysqli_num_rows($menu_res) < 1) { //this URL does not exist $display_block = "<p><em>Menu error.</em></p>"; echo $display_block; }else { while($menu_items = mysqli_fetch_array($menu_res)){ while(list(, $value) =each($menu_items)) { echo "$value" . '<br />'; } } } ?> I think it is caused by the fact that there are 2 whiles, but I am not experience enough to know what should go there. I have tried removing one of the whiles, but of course it does not work. I feel that I have almost solved this if I can lick this problem. Your help will be appreciated! Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 31, 2015 Share Posted August 31, 2015 You don't need the internal loop. Just use $menu_items['Url'] in the outer while() loop. Quote Link to comment Share on other sites More sharing options...
rocky48 Posted August 31, 2015 Author Share Posted August 31, 2015 (edited) Thanks it works BUT! It works on the index.php (Home) and the next tab Links.html, but all subsequent tabs are going through the error coding that detects more than 0 rows are in the query. The select query obviously runs on the new page, so it because the query runs from the output of another query? It seems ilogical that it runs in the second tab, I would have expected that it would only work in the index.php tab! Here is the modified code: <?php include ("connect_Menu_LOCAL.php"); $conn = get_db_conn_Menu(); $menu_items['Url']=""; $menu_sql = "SELECT Url FROM dtop"; $menu_res = mysqli_query($conn, $menu_sql) or die(mysqli_error($conn)); if (mysqli_num_rows($menu_res) < 1) { //this URL does not exist $display_block = "<p><em>Menu error.</em></p>"; echo $display_block; }else { while($menu_items = mysqli_fetch_array($menu_res)){ $item=$menu_items['Url']; echo $item; } } //free results mysqli_free_result($menu_res); //close connection to MySQL mysqli_close($conn); ?> Your help will be appreciated! Edited August 31, 2015 by rocky48 Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 31, 2015 Share Posted August 31, 2015 I'm sorry - I must need more coffee, because I have no idea what you're trying to describe. If "but all subsequent tabs are going through the error coding that detects more than 0 rows are in the query" means the mysqli_num_rows() check on line 46; you're not actually limiting your select statement to any specific or specified Url, so as long as there's a record in the database table 'dtop' that chunk will never run. As for "because the query runs from the output of another query", do you mean you're running the code quoted above while inside another while loop pulling database records? If so, don't - rewrite your query to use a JOIN. Quote Link to comment Share on other sites More sharing options...
rocky48 Posted August 31, 2015 Author Share Posted August 31, 2015 (edited) If "but all subsequent tabs are going through the error coding that detects more than 0 rows are in the query" means the mysqli_num_rows() check on line 46; you're not actually limiting your select statement to any specific or specified Url, so as long as there's a record in the database table 'dtop' that chunk will never run.The point is that the above code works as I said in the index.php file and also if I select the next tab 'Links.html' on the index page (see my webpage quoted above), but subsequent tabs DOES run the check on line 46 including all lines after the 'Menu error' message to the end of the code are printed to the screen. The data on my database is still the same so why is it not finding any records on the 3rd and subsequent tabs?To clarify, all these tests have been done from the index.php menu tabs. As for your second statement, the code I am running is as shown above. This is substituted for the normal unordered list in all of the files that have this menu. I am not running another while loop! If you examine the code on the live site it may become clearer. I'm running the code I am developing on Localhost. Edited August 31, 2015 by rocky48 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 1, 2015 Share Posted September 1, 2015 Im struggling to understand too. I have gone to your website but I do not get the "Menu error." shown on any tab? If you examine the code on the live site it may become clearer. I'm running the code I am developing on Localhost. If you are developing locally how are we supposed to see the error on your live site? Quote Link to comment Share on other sites More sharing options...
rocky48 Posted September 1, 2015 Author Share Posted September 1, 2015 (edited) I am also struggling to explain what is happening. I can't duplicate the problem on my live site as it is only running PHP v 5.2, but I will try when I have time to transfer the database there. Having said that it works on the second tab, that is no longer true, as when I booted up my computer today, only the index.php file worked. The following is what is printed on the sreen: Menu error."; echo $display_block; }else { while($menu_items = mysqli_fetch_array($menu_res)){ $item=$menu_items['Url']; echo $item; } //free results mysqli_free_result($menu_res); //close connection to MySQL mysqli_close($conn); ?> You say that for this code to be run there must be no data rows in the table, but as the select query is the same for each tab/page. when a query is run does the system lock the table, so that if it is run again it can't find any data? I have cleared the result and closed the table, is that not sufficient? I have update the database to test url is: www.1066cards4u.co.uk/test Edited September 1, 2015 by rocky48 Quote Link to comment Share on other sites More sharing options...
rocky48 Posted September 1, 2015 Author Share Posted September 1, 2015 I meant to have said only index (Home) and Links have been changed using database menu. Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 1, 2015 Share Posted September 1, 2015 It's probably because you're linking to a .html page. You can't run PHP from an .html page. Rename to .php and you should be good to go. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 1, 2015 Share Posted September 1, 2015 I think this is a poor idea to do mysql fetches for menu navigation. You can do a simple if/else or a switch and include scripts. Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 2, 2015 Share Posted September 2, 2015 What does the data in table 'dtop' look like? It is weird that it's outputting all the php after the error - as scootstah suggested, you are using .php as your file extension, right? Also, if it's running on the live server and not on your local, that sounds like a configuration error - what version of PHP are you using locally? Compare the PHP and MySQL versions between the two systems, then update the local server to be at least equal to the live server. The select query you're running simply pulls all the rows from the table 'dtop', so as long as there is any data at all in that table, mysqli_num_rows() will be greater than 0, meaning that the error block should never run. The only other thing that I can think of is that the query is failing somehow. Try switching if (mysqli_num_rows($menu_res) < 1) { //this URL does not exist << What URL? There's no WHERE clause in the query... echo "<p><em>Menu error.</em></p>"; } to if (mysqli_num_rows($menu_res) === false) { echo "<p>".mysqli_error($menu_res)."</p>"; } and see what that has to say. @QOC - Unless I'm misunderstanding what you're saying, I disagree. How can you have an extensible, flexible site without storing the navigation in the database? Caching will take care of any egregious database calls, and I can't remember the last time I didn't use - or see used - dynamic navigation in a site. Quote Link to comment Share on other sites More sharing options...
Solution rocky48 Posted September 2, 2015 Author Solution Share Posted September 2, 2015 I feel a right idiot! I should have realised that if a file has any PHP in it it should have a php extension. The revealed code should have made me realise this! Now the file has the correct extension all is revealed! Thanks for pointing me in the right direction! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.