Jump to content

menu links


jstgermain

Recommended Posts

ok, i will provide a few things so that someone may be able to help with what i am trying to do.  here is my MySQL setup for the table that contains the info that I am trying to retrieve.


--
-- Table structure for table `Menu`
--

CREATE TABLE `Menu` (
  `id` int(25) NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  `l_name` varchar(100) default NULL,
  `content` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `Menu`
--

INSERT INTO `Menu` VALUES (1, 'Home', 'Home', 'Home page info here.);
INSERT INTO `Menu` VALUES (2, 'Hosting', 'Hosting', 'Put hosting info here');
INSERT INTO `Menu` VALUES (3, 'Contact Us', 'ContactUs', 'Put contact info here');

here is the php i am currently using.
[code]
<?php #menu.php

//Link 1 query
$query1 = "SELECT * FROM Menu WHERE id=1";
$link1 =  mysql_query ($query1);
$l1 = mysql_fetch_array ($link1, MYSQL_ASSOC);

//Link 2 query
$query2 = "SELECT * FROM Menu WHERE id=2";
$link2 =  mysql_query ($query2);
$l2 = mysql_fetch_array ($link2, MYSQL_ASSOC);

//Link 3 query
$query3 = "SELECT * FROM Menu WHERE id=3";
$link3 =  mysql_query ($query3);
$l3 = mysql_fetch_array ($link3, MYSQL_ASSOC);

?>

<!--// This is where I display the links //-->
<a href="index.php?page=<?php echo $l1['l_name']; ?>"><?php echo $l1['name']; ?></a>
<a href="index.php?page=<?php echo $l2['l_name']; ?>"><?php echo $l2['name']; ?></a>
<a href="index.php?page=<?php echo $l3['l_name']; ?>"><?php echo $l3['name']; ?></a>
[/code]

as you can see, i am having to query the database 3 differnt times to make each link url and name display, and that is sort of a pain in the butt, and i am sure there is a better and easier way to do this.  hopefully someone can help me shorten this task.  keep in mind that there will be more than 3 links.  there are just 3 right now since i am still in the testing phase of everything.  any help will be greatly appreciated.  thanks in advance.
Link to comment
https://forums.phpfreaks.com/topic/33642-menu-links/
Share on other sites

[code]$query = "SELECT * FROM Menu WHERE id=1 OR id=2 OR id=3";
$link =  mysql_query ($query3);
$l= mysql_fetch_array ($link);

<a href="index.php?page=<?php echo $l['l_name'][0]; ?>"><?php echo $l['name'][0]; ?></a>
<a href="index.php?page=<?php echo $l['l_name'][1]; ?>"><?php echo $l['name'][1]; ?></a>
<a href="index.php?page=<?php echo $l['l_name'][2]; ?>"><?php echo $l['name'][2]; ?></a>
[/code]

does that help?
Link to comment
https://forums.phpfreaks.com/topic/33642-menu-links/#findComment-157634
Share on other sites

no, didnt work.  just displays the first 3 letters od the first row in the link name.  :S  the link name and name of the riste row is "Home", so the first like shoed up as "H", link two showed as"o", and link 3 showed as "m".  :S  so, the wuery itself looks ok, but the way you are calling the link with the [[b]0[/b]], [[b]1[/b]], and [[b]2[/b]] isnt correct.  need another way to tell it the row.  let me know if you think of something else.  thanks
Link to comment
https://forums.phpfreaks.com/topic/33642-menu-links/#findComment-157667
Share on other sites

nvm everyone.  got it working.  got help from a friend.  here is the code in case anyone else is ever needing to do something similar

[code]
<?php
#menu.php

$sql = "SELECT * FROM `Menu`"; 
$result = mysql_query($sql); 
while ($row = mysql_fetch_assoc($result)) {
    $id = $row['id']; // get the row id
    $link[$id]['name']      = $row['name']; // put in an array
    $link[$id]['l_name']    = $row['l_name']; // put in an array
    $link[$id]['content']  = $row['content']; // put in an array
}

?>

<!--// Links code //-->
<a href="index.php?page=<?php echo $link['1']['l_name']; ?>"><?php echo $link['1']['name']; ?></a>
<a href="index.php?page=<?php echo $link['2']['l_name']; ?>"><?php echo $link['2']['name']; ?></a>
<a href="index.php?page=<?php echo $link['3']['l_name']; ?>"><?php echo $link['3']['name']; ?></a>
[/code]

Just add as many links as needed.
Link to comment
https://forums.phpfreaks.com/topic/33642-menu-links/#findComment-158036
Share on other sites

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.