• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

MySql, ajax, php tree menu - extra help

I've converted the previous script to mysqli as all my scripts are, and it works perfectly, and now I'm trying to get it to link to the correct page id number as this example:


Basically, my script links to a page called page_edit.php which collects the relevant page id to display the contents.

The way I have this script at the moment, it only links to the LAST page id, no matter what link you click on, and I want each link to go to a different page id (does that make sense at all??),

Here's the modified script:

and here's the "ajax_tree.php" that the page is referring to in case that helps:

and here's the edit_page.php code:

Would really appreciate the extra help!


  • 2
1 Solution
Bruce SmithSoftware Engineer IICommented:
Yes, your while loop that starts on line 8, sets the $pid on line 9. You are not storing that $pid in variable anywhere (like in an array) so the value is ALWAYS going to be the LAST value when you call it down in line 111. You need to modify your code on the following lines to something like the this:

9: // just remove line 9 where you have: $pid = $row["id"];

10: $label_array[$counter] = array("id" => $row['id'], "parent" => $row['parent_id'], "linklabel" => $row['linklabel']);

111: $tree_data[$counter] = array(url => "edit_page.php?pid=".$label_array[$counter]["id"], parent => $parent_id, name => $label_array[$counter]["linklabel"]);
Bruce SmithSoftware Engineer IICommented:
What you have is like saying the following:

$counter = 0;
while($counter < 10):
   $pid = $counter;

echo('The Counter Variable = '.$counter);

Open in new window

That code will ALWAYS echo "The Counter Variable = 9". Same thing with your $pid variable. It will always equal the last id in your database when you call it down on line 111. Hope this explains the fix.
owennicolAuthor Commented:
Thank you so much, that's perfect.

Thank you also for explaining it so well.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now