Mysql query problem

i am new in programming and creating my first dynamic website. and problem i am facing is that when i parse the query in the php for menu of my website it gives only one menu link and hides the other pages created in the database.if i create a new page in mysql the parse code hides the previous page link in the menu and shows the newly created page link. i want to make that every page created in database link is shown in menu. and also guide me what changes i have to do if i want to make menu with sub menu option.

here are the msql code

CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pagetitle` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `linklabel` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `pagebody` text COLLATE latin1_general_ci NOT NULL,
  `pageorder` int(11) NOT NULL,
  `showing` enum('0','1') COLLATE latin1_general_ci NOT NULL DEFAULT '1',
  `keywords` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `description` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `lastmodified` date NOT NULL,
  `extra` varchar(255) COLLATE latin1_general_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;[/code] 

for parsing

[code]$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) { 
    $pid = $row["id"];
    $linklabel = $row["linklabel"];
	
	$menuDisplay = '<li><a href="index.php?pid=' . $pid . '">' . $linklabel . '</a></li>';

and to display menu php code i am using is

<?php echo $menuDisplay; ?>

That’s because you keep overwriting the variable:

[php]$menuDisplay = '

  • <a href[/php]

    Therefore the last iteration of the loop will set the variable to contain the last row of data, then exit, leaving you with just the last row. You need to append the data, rather than overwrite. Simply change that line to:

    [php]$menuDisplay .= '

  • <a href[/php]
  • Thanks a lot Smokey Php this work for me. any suggestion for drop down menu?? :slight_smile:

    one more problem related to above table is that i am trying to add dynamically page title, keywords,description for pages created in database. and for this i used a query and this gives me only title of first page created in database for all pages.

    query i used for it is

    [code]$sqlCommand = “SELECT pagetitle FROM pages WHERE showing=‘1’ LIMIT 1”;
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
    while ($row = mysqli_fetch_array($query)) {
    $title = $row[“pagetitle”];

    }
    mysqli_free_result($query);[/code]

    and php code for title i am using is

    <title><?php echo $title; ?></title>

    thanks a lot for support. problem resolved i used pageid to do title dynamic and it work for me.

    Sponsor our Newsletter | Privacy Policy | Terms of Service