Jump to content

JakkkeM

Recommended Posts

Hi all,

 

I'm experiencing a lot of trouble trying to create a script for my website. I'm making a database dependant menu, and I've spent a couple of days doing trial and error much to no prevail.

 

I'm using Twitter Bootstrap if anyone is familiar with it, and creating a drop down menu.

 

The code for each of these buttons is:

 

<div class="btn-group">
<a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">
WOD
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<!-- dropdown menu links -->
<li><a tabindex="-1" href="http://www.google.com.au">View the WOD</a></li>
<li><a tabindex="-1" href="#">WOD Archives</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Benchmarks</a></li>
</ul>
</div>

 

So, what I need to be able to do is call each menu that I have in the database, (I have 5), determine if it's a drop down, and if it is, echo the links for it inside of the <ul class="dropdown-menu">

 

It seems simple to me, but I cannot get it right.

 

Please help!

 

Cheers.

Link to comment
Share on other sites

If you REALLY want it:

 

 

echo "<h1>Test Page</h1>";

// Fetch MENUs
$fetch_menu = mysql_query("SELECT * FROM `menus` ORDER BY `stacking`");

// Function for LInks
function linkCreate($menu_id){
// Fetch Links
$fetch_links = mysql_query("SELECT * FROM `links` WHERE `assoc_menu` = '".$menu_id."'");
$num_links = mysql_num_rows($fetch_links);

while($LINK = mysql_fetch_array($fetch_links, MYSQL_ASSOC)){

$return_link = "<li><a tabindex=\"-1\" href=\"#\">Health Assessment and Wavier</a></li>";

}

return $num_links." ".$return_link;
}

while($MENU = mysql_fetch_array($fetch_menu, MYSQL_ASSOC)){
echo $MENU['title']."<br>";
echo linkCreate($MENU['menu_id']);
}

 

This comes close but only returns the first link... Probably because it's a function and I'm an idiot for using a function but hey.

 

 

// Do the query for all the menus
$query = mysql_query("SELECT * FROM `menus` LEFT JOIN `links` ON `menus`.`menu_id` = `links`.`assoc_menu`");
// Set some functions
function createButton($btn_type, $btn_title, $TYPE, $standard_url){
if($TYPE == "1"){
// If it's a drop down execute:
$button = "<a class=\"btn ".$btn_type." dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">".$btn_title."  <span class=\"caret\"></span></a>";
}elseif($TYPE == "0"){
// If it's a standard button execute:
$button = "<a class=\"btn ".$btn_type."\" href=\"".$standard_url."\">".$btn_title."</a>";
}else{
// If it's not specified, error:
$button = "<a class=\"btn btn-warning\" href=\"#\">Button Error</a>";
}
return $button;
}
function createDropdown(){

}

// Set the $prev_title variable to prevent repeating the button
$prev_title = "";
// Execute an array for all menus
while($MENU = mysql_fetch_array($query, MYSQL_ASSOC)){
// If this is a new button - print the button
if($MENU['title'] != $prev_title){
// If the titles are not the same, THIS IS THE BUTTON
$return_menu = $MENU['title']."<br>";

}elseif($MENU['title'] == $prev_title){
// If the titles are the same, THIS IS A LINK.
$return_menu = $MENU['link_title']."<br>";
}
// Print whatever is necessary

echo $return_menu; 

// Reset the prev_title for the next menu
$prev_title = $MENU['title']; 
}

 

This returns mostly OK, but removes the first link, and I can't put the links in the UL as required.

Link to comment
Share on other sites

Hi JakkkeM,

 

Functions can return multiple values in two ways;

 

function functionName(){
//code
return $value1, $value2; // etc - comes back in a array
}

// OR

function functionName(){
//code
$value1 = 'value1';
$value2 = 'value2';
$array = array($value1,$value2);
return $array;
}

 

When you then recieve the result by doing something like:

 

$function = functionName();

 

$function will be equal to that array returned just like it would equal a single value if you just returned a variable.

 

If you need more help feel free to ask otherwise here are two links which you might find helpful:

-
(For Arrays)

-
(For Functions)

 

W3Schools.com is a great place to learn; its where I learnt and also php.net is also helpful for learning about specific built in functions in PHP.

 

 

Hope this helps.

Timothy Arden

 

PS Sorry I couldnt indent my code examples -> kept coming up with this '

' so I just edited it and left it unidented
Edited by timothyarden
Link to comment
Share on other sites

Hi JakkkeM,

 

Functions can return multiple values in two ways;

 

function functionName(){
//code
return $value1, $value2; // etc - comes back in a array
}

// OR

function functionName(){
//code
$value1 = 'value1';
$value2 = 'value2';
$array = array($value1,$value2);
return $array;
}

 

When you then recieve the result by doing something like:

 

$function = functionName();

 

$function will be equal to that array returned just like it would equal a single value if you just returned a variable.

 

If you need more help feel free to ask otherwise here are two links which you might find helpful:

-
(For Arrays)

-
(For Functions)

 

W3Schools.com is a great place to learn; its where I learnt and also php.net is also helpful for learning about specific built in functions in PHP.

 

 

Hope this helps.

Timothy Arden

 

PS Sorry I couldnt indent my code examples -> kept coming up with this '

' so I just edited it and left it unidented

 

 

The only problem with this, is I don't have a set amount of values - in your example you used two, I have no set amount of links that could be assigned to a menu. Does that matter?
Link to comment
Share on other sites

W3schools is a crap place to learn - you just won't know that untill you you're past using it. I wonder if the admins can set the forum to auto-ban anyone that links to it on here....

 

anyway, getting back on topic after that rather random piece of promotion.

 

I really can't see what your using to link the formatting to the elements, so I added a couple comments into the code to highlight where you are likely going to need to add these. I basicly only changed your SELECT * (because it's naughty) and added in a small bit of code to refactor the results from the database into something that's more menu-maker-friendly

 

here's the code I came up with - as pretty much always : it's untested and unlikely to work as is, but should be enough to get you moving on the right track.

$sql = <<<SQL
SELECT link_id,
links.assoc_menu as assoc_menu,
link_title,
link_url,
title,
type,
btn_type,
dropdown,
stacking,
menus.assoc_menu as m_assoc_menu,
url as menu_url
FROM menus
INNER JOIN links
ON (links.assoc_menu = menus.menu_id)
WHERE link_permissions = 1
SQL;
$query = mysql_query($sql) or die(mysql_error());
$startList = mysql_fetch_assoc($query);
$refactorList = array();
foreach($startList as $line){
$refatorList[$line['assoc_menu']][] = array(
  'link_id' => $line['link_id'],
  'link_title' => $line['link_title'],
  'link_url' => $line['link_url'],
  'title' => $line['link_title'],
  'btn_type' => $line['btn_type'],
  'dropdown' => $line['dropdown'],
  'stacking' => $line['stacking'],
  'm_assoc_menu' => $line['m_assoc_menu'],
  'menu_url' => $line ['menu_url']
  );
}
foreach($refactorList as $menu){
echo '<div class="your-ui-menu-class">'.$menu['0']['title']."\n\r <ul id=\"{$menu['0']['title']}\"> \n\r";
// ^ echo to output each menu title and start the <ul>
foreach($menu as $link){
 echo "<li><a href=\"{$link['link_url']}\">{$link['link_title']}</a></li>\n\r";
 // ^ echo to output each link within the menu
}
echo "</ul> \n\r </div> \n\r";
}

Link to comment
Share on other sites

Forgot to mention; with the returned array to get the info from it you can either use

echo $array[$number]; // $number should be equal to 1 before the one you want as array indexes start at zero
//example
echo $array[0]; // echo's $value1
echo $array[1]; // echo's $value2

// or

foreach($array as $value){
echo $value;
}

 

Timothy

Link to comment
Share on other sites

Forgot to mention; with the returned array to get the info from it you can either use

echo $array[$number]; // $number should be equal to 1 before the one you want as array indexes start at zero
//example
echo $array[0]; // echo's $value1
echo $array[1]; // echo's $value2

 

Timothy

that's completly useless for an associative array

Link to comment
Share on other sites

Closer than I've ever gotten.

 

But where the hell these values are coming from I have no idea?

 

Image One - The Dropdown

Image Two - The Buttons

 

$sql = "SELECT `link_id`, `links`.`assoc_menu` as `assoc_menu`, `link_title`, `link_url`, `title`, `type`, `btn_type`, `dropdown`, `stacking`, `menus`.`assoc_menu` as `m_assoc_menu`, `url` as `menu_url` FROM `menus` INNER JOIN `links` ON (`links`.`assoc_menu` = `menus`.`menu_id`) WHERE `link_permissions` = '1'";
$query = mysql_query($sql) or die(mysql_error());


$startList = mysql_fetch_assoc($query);


$refactorList = array();


foreach($startList as $line){

$refactorList[$line['assoc_menu']][] = array(
  'link_id' => $line['link_id'],
  'link_title' => $line['link_title'],
  'link_url' => $line['link_url'],
  'title' => $line['link_title'],
  'btn_type' => $line['btn_type'],
  'dropdown' => $line['dropdown'],
  'stacking' => $line['stacking'],
  'm_assoc_menu' => $line['m_assoc_menu'],
  'menu_url' => $line ['menu_url']
  );
}


foreach($refactorList as $menu){


echo '<div class="btn-group"><a class="btn '.$menu['0']['btn_type'].'\n\r dropdown-toggle" data-toggle="dropdown" href="#">'.$menu['0']['title'].'<span class="caret"></span></a><ul class="dropdown-menu">';


// echo '<div class="your-ui-menu-class">'.$menu['0']['title']."\n\r <ul id=\"{$menu['0']['title']}\"> \n\r";
// ^ echo to output each menu title and start the <ul>


foreach($menu as $link){
 echo "<li><a href=\"{$link['link_url']}\">{$link['link_title']}</a></li>\n\r";
 // ^ echo to output each link within the menu
}
echo "</ul> \n\r </div> \n\r";
}

Link to comment
Share on other sites

Yes, Sorry looked over his code earlier and didnt refresh myself before sending that. It should be $arrayName[$reference]. Example:

$array = array('value1' => 'value', 'value 2' = 'value');
// and then
echo $array['value1'];
echo $array['value2'];

 

As Muddy Funster said my original post was wrong and above is how you would reference to a value in associative array. You can have as many values in the array as you want as I said before.

 

Hope this helps to clarify my mistake.

 

Timothy

Link to comment
Share on other sites

JakkkeM, can you post the rendered page source for those dropdowns?

 

Edt: just noticed that you have \n\r inside single quotes - they will not be parsed properly unless they are in double quotes. you can delete them, they were just to add a lttle formating to the final page source to make it slightly easier to read.

Edited by Muddy_Funster
Link to comment
Share on other sites

<div class="btn-group">
<a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">
WOD
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<!-- dropdown menu links -->
<li><a tabindex="-1" href="http://www.google.com.au">View the WOD</a></li>
<li><a tabindex="-1" href="#">WOD Archives</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Benchmarks</a></li>
</ul>
</div>

 

??

Link to comment
Share on other sites

<h1>Test Page</h1><div class="btn-toolbar"><div class="btn-group"><a class="btn btn-inverse


dropdown-toggle" data-toggle="dropdown" href="#">View the WOD<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=home">View the WOD</a></li>


<li><a href="?page=home">WOD Archives</a></li>


<li><a href="?page=home">Benchmakrs</a></li>


</ul> 


</div> 


<div class="btn-group"><a class="btn btn-inverse


dropdown-toggle" data-toggle="dropdown" href="#">Who, What, When, Where and Why?<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=home">Who, What, When, Where and Why?</a></li>


<li><a href="?page=home">Services and Pricing</a></li>


<li><a href="?page=growth-of-sport">Growth of Sport</a></li>


<li><a href="?page=what-is-crossfit">What is CrossFit?</a></li>


</ul> 


</div> 


<div class="btn-group"><a class="btn btn-inverse


dropdown-toggle" data-toggle="dropdown" href="#">What You Need To Know<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=need-to-know">What You Need To Know</a></li>


<li><a href="?page=contacting-us">Contacting Us</a></li>


<li><a href="?page=health-assessment-and-waiver">Health Assessment and Wavier</a></li>


</ul> 


</div> 


<div class="btn-group"><a class="btn btn-inverse


dropdown-toggle" data-toggle="dropdown" href="#">How it Works<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=how-it-works">How it Works</a></li>


</ul> 


</div> 


</div>Array
(
   [1] => Array
       (
           [0] => Array
               (
                   [link_id] => 1
                   [link_title] => View the WOD
                   [link_url] => ?page=home
                   [title] => View the WOD
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 1
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


           [1] => Array
               (
                   [link_id] => 2
                   [link_title] => WOD Archives
                   [link_url] => ?page=home
                   [title] => WOD Archives
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 1
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


           [2] => Array
               (
                   [link_id] => 3
                   [link_title] => Benchmakrs
                   [link_url] => ?page=home
                   [title] => Benchmakrs
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 1
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


       )


   [2] => Array
       (
           [0] => Array
               (
                   [link_id] => 4
                   [link_title] => Who, What, When, Where and Why?
                   [link_url] => ?page=home
                   [title] => Who, What, When, Where and Why?
                   [btn_type] => btn-inverse
                   [dropdown] => 0
                   [stacking] => 2
                   [m_assoc_menu] => 0
                   [menu_url] => ?page=login
               )


           [1] => Array
               (
                   [link_id] => 5
                   [link_title] => Services and Pricing
                   [link_url] => ?page=home
                   [title] => Services and Pricing
                   [btn_type] => btn-inverse
                   [dropdown] => 0
                   [stacking] => 2
                   [m_assoc_menu] => 0
                   [menu_url] => ?page=login
               )


           [2] => Array
               (
                   [link_id] => 6
                   [link_title] => Growth of Sport
                   [link_url] => ?page=growth-of-sport
                   [title] => Growth of Sport
                   [btn_type] => btn-inverse
                   [dropdown] => 0
                   [stacking] => 2
                   [m_assoc_menu] => 0
                   [menu_url] => ?page=login
               )


           [3] => Array
               (
                   [link_id] => 7
                   [link_title] => What is CrossFit?
                   [link_url] => ?page=what-is-crossfit
                   [title] => What is CrossFit?
                   [btn_type] => btn-inverse
                   [dropdown] => 0
                   [stacking] => 2
                   [m_assoc_menu] => 0
                   [menu_url] => ?page=login
               )


       )


   [3] => Array
       (
           [0] => Array
               (
                   [link_id] => 8
                   [link_title] => What You Need To Know
                   [link_url] => ?page=need-to-know
                   [title] => What You Need To Know
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 3
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


           [1] => Array
               (
                   [link_id] => 9
                   [link_title] => Contacting Us
                   [link_url] => ?page=contacting-us
                   [title] => Contacting Us
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 3
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


           [2] => Array
               (
                   [link_id] => 10
                   [link_title] => Health Assessment and Wavier
                   [link_url] => ?page=health-assessment-and-waiver
                   [title] => Health Assessment and Wavier
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 3
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


       )


   [4] => Array
       (
           [0] => Array
               (
                   [link_id] => 11
                   [link_title] => How it Works
                   [link_url] => ?page=how-it-works
                   [title] => How it Works
                   [btn_type] => btn-inverse
                   [dropdown] => 1
                   [stacking] => 4
                   [m_assoc_menu] => 0
                   [menu_url] => 
               )


       )


)

Edited by JakkkeM
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.