/    Sign up×
Community /Pin to ProfileBookmark

PHP Hide row if field equals a value?

Hi,

Below I have a script and have altered it a little to serve my purposes. What id like to do is if my “nav_hide” field equals “hide” then it hides that row.

I have tried adding this “if($design_online_connection[‘nav_hide’] != “”) { } into my script but I can’t seem to get it to work at all. Can anyone advise on how to do this please as I’m pulling my hair out trying to do it 😮

Below is the menu code:

[CODE]class MenuBuilder
{
var $conn;
var $items = array();
var $html = array();
function MenuBuilder(&$db)
{
$this->conn = $db;

}
function fetch_assoc_all( $sql )
{
$result = mysql_query( $sql, $this->conn );

if ( !$result )
return false;
$assoc_all = array();
while( $fetch = mysql_fetch_assoc( $result ) )
$assoc_all[] = $fetch;
mysql_free_result( $result );
return $assoc_all;
}
function get_menu_items()
{
$sql = ‘SELECT id, parent_id, title, link, position, nav_hide FROM menu_item ORDER BY parent_id, position;’;
return $this->fetch_assoc_all( $sql );
}
function get_menu_html( $root_id = 0 )
{
$this->html = array();
$this->items = $this->get_menu_items();
foreach ( $this->items as $item )
$children[$item[‘parent_id’]][] = $item;
$loop = !empty( $children[$root_id] );
$parent = $root_id;
$parent_stack = array();
$this->html[] = ‘<ul>’;
while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
{
if ( $option === false )
{
$parent = array_pop( $parent_stack );
$this->html[] = str_repeat( “t”, ( count( $parent_stack ) + 1 ) * 2 ) . ‘</ul>’;
$this->html[] = str_repeat( “t”, ( count( $parent_stack ) + 1 ) * 2 – 1 ) . ‘</li>’;
}
elseif ( !empty( $children[$option[‘value’][‘id’]] ) )

{
$tab = str_repeat( “t”, ( count( $parent_stack ) + 1 ) * 2 – 1 );

// HTML for menu item containing childrens (open)
$this->html[] = sprintf(

‘%1$s<li class=”has-sub” id=”%3$s”><a href=”/page.php?ID=%2$s”>%3$s</a>’,
$tab, // %1$s = tabulation
$option[‘value’][‘id’], // %2$s = link (URL)
$option[‘value’][‘title’], // %3$s = title
$option[‘value’][‘nav_hide’] // %4$s = nav hide
);
$this->html[] = $tab . “t” . ‘<ul class=”submenu”>’;

array_push( $parent_stack, $option[‘value’][‘parent_id’] );
$parent = $option[‘value’][‘id’];
}
else

// HTML for menu item with no children (aka “leaf”)
$this->html[] = sprintf(
‘%1$s<li id=”%3$s”><a href=”/page.php?ID=%2$s”>%3$s</a></li>’,
str_repeat( “t”, ( count( $parent_stack ) + 1 ) * 2 – 1 ), // %1$s = tabulation
$option[‘value’][‘id’], // %2$s = link (URL)
$option[‘value’][‘title’] , // %3$s = title
$option[‘value’][‘nav_hide’] // %4$s = nav hide
);
}

// HTML wrapper for the menu (close)
$this->html[] = ‘</ul>’;

return implode( “rn”, $this->html );
}
}[/CODE]

Kind Regards
Joe

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NoEffinWayOct 19.2015 — Why not go a step back and just not even select the row from the database. That way you can reduce the script execution time. Something like:

[code=php]
function get_menu_items($getHidden = true)
{
if(!$getHidden){
$where = "WHERE nav_hide != 'hide'";
}else{
$where = "";
}
$sql = "SELECT id, parent_id, title, link, position, nav_hide FROM menu_item {$where} ORDER BY parent_id, position;";
return $this->fetch_assoc_all( $sql );
}
[/code]
Copy linkTweet thisAlerts:
@jmd87authorOct 19.2015 — Hi,

Thanks alot for the reply ?

I have tried something similar to this (although didn't think of not even selecting the row" and also just tried your script and it doesn't work. I haven't a clue whats going on its obviously my error somewhere!

Kind regards

Joe
Copy linkTweet thisAlerts:
@NoEffinWayOct 19.2015 — The script changes I made will still grab all rows by default. You have to make sure to pass it a false value to hide them.

So change this:
[code=php]
$this->items = $this->get_menu_items();
[/code]

to this:
[code=php]
$this->items = $this->get_menu_items(false);
[/code]
Copy linkTweet thisAlerts:
@jmd87authorOct 19.2015 — Hi Thanks for the reply ?

really appreciate your help. I have tried that and it still didn't work I have attached the code where I think it should go. Have I placed it in the right area or am i being stupid?


class MenuBuilder

{

var $conn;

var $items = array();

var $html = array();

function MenuBuilder(&$db)

{

$this->conn = $db;


}
function fetch_assoc_all( $sql )
{
$result = mysql_query( $sql, $this->conn );

if ( !$result )
return false;
$assoc_all = array();
while( $fetch = mysql_fetch_assoc( $result ) )
$assoc_all[] = $fetch;
mysql_free_result( $result );
return $assoc_all;
}
function get_menu_items($getHidden = true)
{
if(!$getHidden){
$where = "WHERE nav_hide != 'none'";
}else{
$where = "";
}
$sql = "SELECT id, parent_id, title, meta_title, link, position, nav_hide FROM menu_item {$where} ORDER BY parent_id, position;";
return $this->fetch_assoc_all( $sql );
}
function get_menu_html( $root_id = 0 )
{
$this->html = array();
$this->items = $this->get_menu_items(false);
foreach ( $this->items as $item )
$children[$item['parent_id']][] = $item;
$loop = !empty( $children[$root_id] );
$parent = $root_id;
$parent_stack = array();
$this->html[] = '<ul>';
while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
{
if ( $option === false )
{
$parent = array_pop( $parent_stack );
$this->html[] = str_repeat( "t", ( count( $parent_stack ) + 1 ) * 2 ) . '</ul>';
$this->html[] = str_repeat( "t", ( count( $parent_stack ) + 1 ) * 2 - 1 ) . '</li>';
}
elseif ( !empty( $children[$option['value']['id']] ) )
{
$tab = str_repeat( "t", ( count( $parent_stack ) + 1 ) * 2 - 1 );

// HTML for menu item containing childrens (open)
$this->html[] = sprintf(
'%1$s<li class="has-sub" id="%3$s" style="display:%4$s"><a href="%5$s">%3$s</a>',
$tab, // %1$s = tabulation
$option['value']['id'], // %2$s = link (URL)
$option['value']['title'], // %3$s = title
$option['value']['nav_hide'], // %4$s = nav hide
$option['value']['meta_title'] // %5$s = meta_title
);
$this->html[] = $tab . "t" . '<ul class="submenu">';

array_push( $parent_stack, $option['value']['parent_id'] );
$parent = $option['value']['id'];
}
else
// HTML for menu item with no children (aka "leaf")
$this->html[] = sprintf(
'%1$s<li id="%3$s" style="display:%4$s"><a href="%5$s">%3$s</a></li>',
str_repeat( "t", ( count( $parent_stack ) + 1 ) * 2 - 1 ), // %1$s = tabulation
$option['value']['id'], // %2$s = link (URL)
$option['value']['title'] , // %3$s = title
$option['value']['nav_hide'], // %4$s = nav hide
$option['value']['meta_title'] // %5$s = meta_title
);
}

// HTML wrapper for the menu (close)
$this->html[] = '</ul>';

return implode( "rn", $this->html );
}

}[/QUOTE]



Kind Regards

Joe
Copy linkTweet thisAlerts:
@NoEffinWayOct 19.2015 — That appears to be correct. If it is not functioning, you need to check you database. Also, if the only option for the column nav_hide is either "" or "hide" you might want to consider changing it to a tinyint field and using 1 or 0. Why use 4 bytes when you can use one?
Copy linkTweet thisAlerts:
@jmd87authorOct 20.2015 — Hi,

Thanks for the reply and help. I shall investigate my database and also change it from none to 1's and 0's (This was originally done as "none" because I had it writing into a CSS Display: script)

Kind Regards

Joe
×

Success!

Help @jmd87 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...