Jump to content

Duplicate variables from array


rocky48
Go to solution Solved by rocky48,

Recommended Posts

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!

Link to comment
Share on other sites

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 by rocky48
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by rocky48
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by rocky48
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.