Jump to content

[SOLVED] Help with making a menu from a database table.


parodys

Recommended Posts

Hey guys,

 

I kinda new to PHP and mySQL, ive mastered HTML ( big woop eh ) now its time to get into proper coding etc and so ive been pointed to this forum to get some help!

 

Here goes, i have a database that has table such as this :

 

id, title, article, cat, created_at

 

what i am trying to do is make a menu on the left side of my webpage, that will have a make a list of the all the cat, and once clicked will go to a page that lists all the articles under that cat. Would it be easier to create a relational database to correspond with the cat ?

 

or make a function that will scan the cat table and out put a list of single cats?

 

Im not sure if ive gone about this the right way, but ive made the structure like this.

 

 

index.php

<?php
include('dbfuncs.php');	

$controller = 'articles';

$view = empty($_GET['view']) ? 'index' : $_GET['view'];

switch ($view) {

	case "index":
		$layout = 'home';
	break;

	case "article":
		$layout = 'articles';
	break;

	case "archive":
		echo "this is the archive page";
	break;

	case "new":
		echo "this is the NEW page";
	break;
				}

if(isset($layout))
	{
include ($_SERVER['DOCUMENT_ROOT'].'/codedad/views/layouts/'.$layout.'.php');	
	}
	else
	{
include ($_SERVER['DOCUMENT_ROOT'].'/codedad/views/layouts/'.$controller.'.php');
	}

?>

 

which is pretty much my templating system and web site layout. So if i type in wxw.codedad.com/?view=article it will take me to the the article page... and in that template ill have calls to functions that will display the data. It works well so far.

 

but yeah im have trouble creating a menu for the cats.

 

Obviously i have a lot of learning to do.. haha

 

Any ideas or linkage to some info would be oh so appreciated !!

 

 

 

 

 

 

 

Link to comment
Share on other sites

since u mastered html

why don't u create demo page..

 

* what should u see in 1st page

* what should u see after click one of the menu

* what should u see after click another in menu

 

for me, i were begin the new app with modul

ex: u want to show all cat on menu..

* i create modul to show all cat and it show only text only no link and no graph in it (GUI)

* i update with link

* i put on the HTML etc

Link to comment
Share on other sites

yeah i have created a basic layout demo, thats what ive been working off. Just having trouble creating a menu for all the categories under cat from my database.

 

In the database i have over 200 pages listed under 8 categories.

 

so i need the menu to go like this.

 

table.jpg

 

The trouble im having is making the actual menu from the CAT table in the database and then forwarding it to the archive page (paginated of cause) where it will list all the articles under the cat and once clicked on will go again to the actual article page.

 

it probably simple stuff i just cant seem to get my head around it.

 

Link to comment
Share on other sites

hmm.. allright.. u already built the  page

but view your table.. i was think u need learn much about database, table etc

well, there's a long road u must follow.. but don't worry.. you will pass it.

 

let me honest to you.

 

create only one table to this site is good..

but create more table with link betwen.. IS BETTER ^^

perhaps u should read about "Normalitation"

 

sry.. i can help you.. but i try to give u a lure than give you the fish

*do you understand?

Link to comment
Share on other sites

ok haha ive looked it up.

 

I created a new table

 

table name : cat

 

id, cat

 

i joined them but when i query it i get a list of all the articles cats..

 

php

php

php

php

 

and so on.

 


$query = "SELECT articles.cat, cat.cat FROM articles, cat WHERE articles.cat = cat.cat"; 
 	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_array($result)){
		echo $row['cat'];
	}

 

hmmm

Link to comment
Share on other sites

ehem.. u said in top to build menu ?

 

<?php
...... 
/*just header etc*/
$query = "SELECT cat.cat FROM cat order by cat";
       $result = mysql_query($query) or die(mysql_error());

      while($row = mysql_fetch_array($result)){
         echo $row['cat'];
      }


?>

this menu you wish ^^

hehehe.. rokie mistake..

 

well.. before leave..

try to give a better name for field ex:

table Category:

field: catid, catname, catdesc

 

this will give you a better understanding about the table/field u use.. btw..

cat can mean an animal cat ^^

Link to comment
Share on other sites

haha yeah good point ive made the changes to the database and have been thinking about it.

 

If i do a query for the categories to be listed in an unordered list, with links that will pass the value of the category to the archive page and in turn will it list the articles under that category ?

 

maybe using a $_get ? and isset ? haha

 

am i on the right track ?

Link to comment
Share on other sites

ok so ive made some progress, im not sure if ive done this the optium way, but its getting there slowly!

 

<?php
// displays catogories and makes a list with links to archive page
db_connect();

$query = "SELECT catname FROM category"; 
$result = mysql_query($query) or die(mysql_error());

// make the catogories list

$entry = '<ul>';

  	  while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
	{
		list($catname) = $row;
   	    	$entry .= "<li><a href=\"$catname\">$catname</a></li>\r\n";
  	  	}

	$entry .= '</ul>';
// show the catogories list
echo $entry
?>

 

It lists the catogories now all i have to do is figure how to pass the the cat id along :D

 

thanks for your help so far !

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.