Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/287216-can-retrieve-the-correct-data/
Share on other sites

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();
	}

?>

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";
<?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

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.

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.