williamh69 Posted March 24, 2014 Share Posted March 24, 2014 hi guys i have this two db's menu paginas menu_id page_id name menu_id title content I populate the menu with the following code, which working fine. <?php function query($parent_id) { //function to run a query $query = mysql_query ( "SELECT * FROM menus" ); return $query; } function fetch_menu($query) { while ( $result = mysql_fetch_array ( $query ) ) { $menu_id = $result ['menu_id']; $menu_name = $result ['menu_name']; $menu_link = $result ['menu_link']; echo "<li class='has-sub '><a href='index.php?content=paginas&cat=$menu_id'><span>$menu_name</span></a>"; echo "</li>"; } } fetch_menu (query(0)); //call this function with 0 parent id ?> then I link every menu in my list with each page with this code, but appears the first entry in all the menus, I used the following code: <?php $menu_id = $_GET['cat']; $query="SELECT page_id, title, sub_title, content from paginas where menu_id=$page_id"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $page_id = $row['page_id']; $title = $row['title']; $sub_title = $row['sub_title']; $content = $row['content']; echo "<h1>$title</h1>"; echo"<br>"; echo"<h2>$sub_title</h2>"; echo"<br>"; echo"<p>$content</p>"; } ?> what i am doing wrong..... thank you for ur help Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/ Share on other sites More sharing options...
adam_bray Posted March 24, 2014 Share Posted March 24, 2014 Where is $page_id being set? Also, you can use a MySQL JOIN to cut the query count to 1 - <?php if($stmt = $mysqli->prepare(' SELECT paginas.page_id , paginas.title , paginas.sub_title , paginas.content , menus.name AS menu_name , menus.menus_link FROM paginas LEFT OUTER JOIN menus ON menus.menu_id = paginas.menu_id WHERE paginas.menu_id=? ')) { $stmt->bind_param("s", $_GET['cat']); $stmt->execute(); $result = $stmt->get_result(); while( $row = $result->fetch_assoc() ) { // Page data $page_id = $row['page_id']; $title = $row['title']; $sub_title = $row['sub_title']; $content = $row['content']; // Menu data $menu_name = $row['menu_name']; $menu_link = $row['menu_link']; } echo '<h1>'.$title.'</h1> <h2>'.$sub_title.'</h2> <p>'.$content.'</p>'; $stmt->close(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473675 Share on other sites More sharing options...
cyberRobot Posted March 24, 2014 Share Posted March 24, 2014 The GET variable from the links is being assigned to $menu_id. But your query is using a variable called $page_id. Have you tried the following: $menu_id = $_GET['cat']; $query="SELECT page_id, title, sub_title, content from paginas where menu_id=$menu_id"; Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473677 Share on other sites More sharing options...
williamh69 Posted March 24, 2014 Author Share Posted March 24, 2014 hi cyber i tried but t it gives me the same result Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473705 Share on other sites More sharing options...
williamh69 Posted March 24, 2014 Author Share Posted March 24, 2014 adam i used your code but gives me also the same result Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473713 Share on other sites More sharing options...
ginerjm Posted March 24, 2014 Share Posted March 24, 2014 Show us the NEW code that you are using. Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473738 Share on other sites More sharing options...
williamh69 Posted March 25, 2014 Author Share Posted March 25, 2014 <?php $menu_id = $_GET['cat']; $query="SELECT * from paginas WHERE menu_id=$menu_id"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $page_id = $row['page_id']; $title = $row['title']; $sub_title = $row['sub_title']; $content = $row['content']; echo "<h1>$title</h1>"; echo"<br>"; echo"<h2>$sub_title</h2>"; echo"<br>"; echo"<p>$content</p>"; } ?> here it is Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473781 Share on other sites More sharing options...
ginerjm Posted March 25, 2014 Share Posted March 25, 2014 Add a test of your query execution to be sure it's a valid query (it has to be bad and not performing and therefore none of your follow up code is even attempted). Tip - One should ALWAYS check query results and any other dramatic function call that affects the proper execution of your script. Quote Link to comment https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/#findComment-1473783 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.