I'm having a hard time getting this menu to work properly.
function writeMenu(){
echo "<div id=\"menu\">" <ul id=\"top-link\">";
m("top", "n"); echo "</ul></div>"; (sorry, it wouldn't format properly)
function m($parent,$issub){
$parentQ = "select * from cdi_menu";//gets menu items from menu table
$parentResult = mysql_query($parentQ); //runs menu item query and obtains result
while ($link = mysql_fetch_assoc($parentResult)) {//for each line in the result do the folowing:
if($parent==$link['PARENT']){//if the next link belongs to this menu item
echo "\n <li>".$link['DISPLAY']."</li>";
if($issub=="n" && $link['HASCHILD']=="y"){//if this menu item is a top menu item
echo "\n <li id=\"sub-link\"><ul>";
m($link['ID'], $links, "y");
echo "\n </ul></li>";
}
}
}
}
echo writeMenu();
What I'm trying to do is make it where I can hide the 'sub-link' IDs (I would use classes, but javascript doesn't seem to edit class styles, just IDs). The sub-link items would show when over a parent item.
top refers to the top elements, and ID refers to the unique id in database.
Thanks, sorry if it's confusing.
Your function has only 2 parameters but You call it with 3 inside
m($link['ID'], $links, "y");
$links is unnecessary.
It would be better if You modify query to look like this
$parentQ = "select * from cdi_menu WHERE parent='$parent'";
so You don't need first if statement and You will not fetching all rows multiple times for each menu/submenu.
Related
I have the following code to create category and subcategories from a
database
function displayChild($parent)
{
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc())
{
echo '<li>'.mb_strtoupper($rows['catName']).'</li>';
displayChild($rows['catId']);
}
echo '</ul></div>';
}
displayChild('0');
The CSS bits should be as follows
#charset "utf-8";
.ul0{}
.ul1{}
.ul2{}
.
.
.
.uln{}
since the first tag appears outside the while loop it forms a really weird list when i reference it.putting it inside the while() loop is not an option either. please help
I'm not exactly sure what you mean by " it forms a really weird list when i reference it." However, it sounds like maybe you are getting odd html. If that's the case, you get better results if you put the nested function call inside the li:
function displayChild($parent){
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc()){
echo '<li>'.mb_strtoupper($rows['catName']).'';
displayChild($rows['catId']); // note this is now inside the list item
echo '</li>';
}
echo '</ul></div>';
}
I want to write something like this :
<?php
$comment = mysql_query("SELECT * FROM `communication` WHERE `ID`='$index' order by `Date_add` desc");
echo "<div class=\"row\">";
while ($com = mysql_fetch_assoc($comment)) {
$side = mysql_query("SELECT Type FROM `client` WHERE `ID`='$comtype'");
if ($side[0]==2) {
echo "<div class=\"left\">"; // and i want to execute this line only when the next value of $side is equal to 1 or 9
echo "<div class=\"inside1\">"
...
echo "</div>";
echo "</div>"; // same as above, close div only, when the next value of $side is equal to 1 or 9
} else if ($side[0]==1 || $side[0]==9) {
echo "<div class=\"right\">"; // Same here i want to execute this line only when the next value of $side is equal to 2
echo "<div class=\"inside2\">";
...
echo "</div>";
echo "</div>"; // same as above, close div only, when the next value of $side is equal to 2
}
}
echo "</div>";
I need to execute whole code, but i have div inside div, and i want and execute when value of $side[0] is different. For example:
Loop step 1:
$side[0]=2
so i want to execute: <div class=left> and everything in this div.
Loop step 2:
$side[0]=2 again
so i want to execute all in <div class=left> but i dont want to create another <div class=left>
Lopp step 3:
$side[0]=1
so previously $side[0] was equal 2, so now i want to create <div class=left> and everything in this div
Lopp step 4:
$side[0]=1 again
so i want to execute all in <div class=right> but i dont want to create another <div class=right>
etc...
Anyone know how to achive effect like this ? Thanks for help in advice.
Understanding your problem, you have two main din inside your while loop and inside those div you want to display result according to a particular column value. The way I can suggest you make to separate array based on the value from query. Then iterate through individual array and display it on the let/right div accordingly.
Basically I want to be able to create a multi-level navigation (many sub navs). Obviously I know this will be done through creating lists with in each other but I am pretty stuck on the logic of displaying it correctly.
I have seen stuff regarding parent/children relationships but can't find anything that is efficient and easy to udnerstand.
I don't need to know how the HTML is built. Just how the php/mysql can generate the lists.
Hope you can help.
A
Here is code I used. It builds unordered list with unlimited level of subitems.
/*
* Table has 3 fields: `ID`, `PARENTID` and `NAME`
* `ID` is unique, `PARENTID` showing his parent node id.
* This function will go through it and build unordered list and call itself when needed to build subitems.
* $level argument used to define wich node's subitems to build. Default is 0 which is top level.
*/
function showMenu($level = 0) {
$result = mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$level);
echo "<ul>";
while ($node = mysql_fetch_array($result)) {
echo "<li>".$node['NAME'];
$hasChild = mysql_fetch_array(mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$node['ID'])) != null;
IF ($hasChild) {
showMenu($node['ID']);
}
echo "</li>";
}
echo "</ul>";
}
Hope that helps.
I think the most efficient would be to get all records in one go from the database and then build the hierarchical structure again in php.
So you would have a structure similar to this in your database:
id parent_id menu_item
Then you can get all items and use a recursive function to build a hierarchical array which you can loop through to get your menu, sub-menu, sub-sub-menu, etc. items. See this question and the top-two answers on how to re-build the structure.
If you mean the HTML it's like this:
<ul>
<li>
Title
<ul>
<li>Title</li>
<li>Title</li>
<li>Title</li>
</ul>
</li>
<li>Title</li>
<li>Title</li>
<li>Title</li>
</ul>
assuming you know how to create filled with the content of a mysql table
assuming you have the following tables : Universes > Categories > Markets > Segments
1) list the content of 'Universes' in a select. when the user picks, call another .php script and send it the id of the chosen Universe (using GET or POST)
2) list the content of 'Categories', WHERE idUniverses = the id you sent to the second script.
3) same for the Markets...
It's easier with AJAX.
need the code ?
So, I'm working on a WordPress theme that uses a custom taxonomy to create a handy web form.
Right now it prints Grandparent and child, but I need it to print:
Grandparent -> Parent -> Child
I've been been able to get the ID of the Parent, but cannot print that parent's name, no matter what I try.
Here's what I have to get the id:
$adCategory = get_term_by('id',$_POST['cat'],'ad_cat');
$_POST['catname'] = $adCategory->name;
$mainCategory = get_term_by('id',$_POST['main_cat'],'ad_cat');
$mainCat = $mainCategory->name;
$categoryParent = get_term_by('id',$_POST['cat'],'ad_cat');
$catParents = $categoryParent->parent;
(first group prints child, second prints grandparent, third prints id
and here is how I'm able to print them all on the page:
<?php echo $catParents; ?>
<?php echo $mainCat; ?>
<?php echo $_POST['catname']; ?>
I've tried get_cat_name, but it won't work, just returns empty. Any ideas?
If you already have an ID you can at least make a direct query
$wpdb->get_var('SELECT name FROM '.$wpdb->terms.' WHERE term_id = '.$term_ID);
Does anyone know how to bring in all of a mysql tables' results, only show the first X, (say 10), and then hide the rest using jquery? Basically, as I've already got the jquery, I just need to know how to show only the first X results in one div, then the rest in a seperate div.
My aim is to only show the first 10 results, but provide a link at the bottom of the page allowing the user to show all of the results. Was thinking the hyperlink could just re-execute the query but thought it would be easier to show/hide using jquery.
Many thanks in advance. S
Thought I'd add the code I'm using below
$query = "SELECT * FROM ispress WHERE active = '1' ORDER BY YEAR(date) DESC, MONTH(date) DESC LIMIT 0, 7";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet))
{
$newsArray = array();
while ($newsResult = mysql_fetch_array($resultSet))
{
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$bFirstTime = true;
if (!isset($newsArray[$timePeriod]))
{
$newsArray[$timePeriod] = array();
}
$newsArray[$timePeriod][] = $newsResult;
}
foreach ($newsArray as $timePeriod => $newsItems)
{
echo '<div class="date">' . $timePeriod . '</div>' . PHP_EOL;
echo '<ul class="press">' . PHP_EOL;
foreach ($newsItems as $item)
{
if ($bFirstTime) {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['imgWidth'].'" height="'.$item['imgHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,244).'...</p>
<p>Read more</p>
';
echo '</li>' . PHP_EOL;
$bFirstTime = false;
} else {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['tnWidth'].'" height="'.$item['tnHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,100).'...</p>
<p>Read more</p>
';
echo '<div class="clear"></div>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
}
echo '</ul>' . PHP_EOL;
}
echo '<p>Older posts...</p>'. PHP_EOL;
echo '<div id="slickbox">This is the box that will be shown and display the rest of the news results. :)</div>'. PHP_EOL;
}
else
{
echo 'We currently have no press releases available';
}
This will hide the first 10 children. How are you planning on showing the other results? Buttons, fields, jqueryui widgets?
You will just need to add a click event which calls this function.
function limit_results(start, end) {
$('#things > .thing').each(index) {
if(index < end && index >= start) {
$(this).hide();
}
}
}
limit_results(1,10);
If you have your elements in a jQuery object already (say, $('#sql-results') holds all of your results), you can always do this: $('#sql-results:lt(10)') to work with the first ten elements, and $('#sql-results:gt(9)') to work with the rest of the elements.
You have to decide yourself how efficient your approach is for this amount of data you're processing.
Right, so for your specific markup structure, you can add this to your JS:
// Obviously this is untested and probably not bug-/typo-free
(
function($) {
var $slickbox = $('#slickbox').hide();
$('<ul></ul>')
.appendTo($slickbox)
.append('ul.press li:gt(9)');
$('#slick-toggle')
.bind(
'click',
function(){
$slickbox.toggle();
}
);
}
)(jQuery);
This would involve a lot of rewriting but jquery has a datatables plugin that will display the data. To use it you need to do something like
echo '<table id="news-table">'
echo '<thead>';//Datatables needs a thead with the correct number of columns. However you don't need to fill them in.
echo '<th>Date</th>';
echo '<th>Time Period</th>'
echo '</thead><tbody>';
while ($data = my_sql_fetch_array($result)) {
echo '<td>Whatever</td>';
echo '<td>Another Field</td>';
}
echo '</tbody></table>';
The jquery is then
$('#news-table').dataTable();
I'm not sure how it would do custom no data messages and I know that with the code you have written this may not be any good to you right now but I'm posting it because it could be useful for somebody looking for pagination info or for you if you want to do something similar again. Datatables is also useful because the user can choose the number of results they want to show, what column they want to sort by and what direction to sort in.
in your query
limit 0,10
for the rest
limit 11,xxx
When you print out each row's data count each iteration by incrementing a counter. When you get to 11 start a new div that has a different id to that of your 1st div that you already defined an id for. Now using jQuery you can hide and show the 2nd div with the remaining results as you please.
Divide the return values in your php file with a character
ex:
echo "this is first value +";
echo "this is second value +";
echo "this is third value +";
use javascript to separate the return values
ex:
var ajaxArray = ajaxValues.split("+");
now all three values are placed in ajaxArray and you may use anyone you want
ex:
ajaxArray[0] = this is first value