Jump to content

PHP/Javascript Navigation is beating me


lil_bugga

Recommended Posts

I've been using javascript for a websites expanding navigation and its been working fine. Now I want to adapt that script to work with php and a database connection.

 

This is the orignal javascript navigation that worked.

<a href="javascript:display('Books')" class="Group">~ Books ~</a>
    <div class="hide" id="Books">
        <a href="#" class="Option">Beginners</a>
        <a href="#" class="Option">Kids</a>
        <a href="#" class="Option">Adult</a>
    </div>

 

This is my attempt of intergrating it with php

<?php
//connect to server and select database; you may need it
$mysqli = mysqli_connect("localhost", "***", "***", "test");

$display_block = "";

//show categories first
$get_cats_sql = "SELECT id, cat_title FROM store_categories ORDER BY cat_title";
$get_cats_res =  mysqli_query($mysqli, $get_cats_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_cats_res) < 1) {
   $display_block = "<p><em>Sorry there are currently no categories to browse.</em></p>";
} else {
   while ($cats = mysqli_fetch_array($get_cats_res)) {
        $cat_id  = $cats['id'];
        $cat_title = ucwords(stripslashes($cats['cat_title']));

        $display_block .= "<a href=\"javascript:display('".$cat_id."')\" class=\"Normal\">".$cat_title."</a>
					   <div class=\"hide\" id=\"".$cat_id."\">".$test."
					   </div>";

        if (isset($_GET["cat_id"])) {
		if ($_GET["cat_id"] == $cat_id) {

		   //get items
		   $get_items_sql = "SELECT id, item_title FROM store_items WHERE cat_id = '".$cat_id."' ORDER BY item_title";
		   $get_items_res = mysqli_query($mysqli, $get_items_sql) or die(mysqli_error($mysqli));

		   if (mysqli_num_rows($get_items_res) < 1) {
				$display_block = "<p><em>Sorry no items were found in this category.</em></p>";
		   } else {	
		   						
				while ($items = mysqli_fetch_array($get_items_res)) {
				   $item_id  = $items['id'];
				   $item_title = stripslashes($items['item_title']);

				   $test .= ">><<".$item_id."
				   <a href=\"showitem.php?item_id=".$item_id."\" class=\"Option\">".$item_title."</a>";	
				   }				
			}

			//free results
			mysqli_free_result($get_items_res);

		}
	}
}
}
//free results
mysqli_free_result($get_cats_res);

//close connection to MySQL
mysqli_close($mysqli);
?>

 

Where am I going wrong, I'm fairly new to both programming languages, the majority of that php is from an example from one of my books.

 

Thanks in advance for any help, I really wanna crack this so my navigation can be populated from the database rather than hard coded values.

Link to comment
https://forums.phpfreaks.com/topic/158126-phpjavascript-navigation-is-beating-me/
Share on other sites

its not actaully giving me an error, and i haven't checked the source.

 

What i'm aiming for is to get the display_block to show the main links, then display the items inside a div.

 

I can get them to display the main links but not to expand and show the next level of navigation

I've just looked at the source this is what I'm being given

 

<a href="javascript:display('3')" class="Normal">Books</a>
<a href="javascript:display('1')" class="Normal">Hats</a>
<a href="javascript:display('2')" class="Normal">Shirts</a> 

                 

This is what I'd like to get

 

<a href="javascript:display('3')" class="Normal">Books</a>
     <div class="hide" id="Books">
          <a href="#" class="Option">Childrens</a>
     </div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.