[UnResolved]CheckBox array Problem
this script is a modification of a solution i was given for a similiar problem
http://vbforums.com/showpost.php?p=2323616&postcount=7
What im trying to do now... is list a table. The table is full of Links and menu's.
This is how im trying to have it outputted.
Navigation minRank: 0
Home minRank: 0
Members minRank: 0
Console minRank: 1
Forums minRank: 0
Downloads minRank: 1
All minRank: 1
Images minRank: 1
Exe minRank: 2
Zip minRank: 1
This is the script as it is currently. This is also confusing the ***** outa me and i dont know why.
PHP Code:
$sql = "SELECT * FROM navigation_links WHERE menu_id = -1";
$result = mysql_query($sql) or die(mysql_error());
$menus = array();
while($row = mysql_fetch_array($result)) {
$menus[] = $row;
}
//Tryed this to see if it'd work.
foreach($menus as $menu){
$sql1 = "SELECT * FROM navigation_links WHERE menu_id = '".$menu['link_id']."'";
$result = mysql_query($sql1) or die(mysql_error());
$links = array();
while($row1 = mysql_fetch_array($result)) {
$links[$row1['menu_id']][] = array('id' => $row1['link_id'],
'name' => $row1['link_name'],
'url' => $row1['link_url'],
'minRank' => $row1['minRank']);
}
}
foreach($menus as $menu) {
echo'
<div class="forum">
<span class="name"><font color="orange">Menu :'. $menu['link_name'].'</font></span>
<span class="description"> minRank:'.$menu['minRank'].'</span><br />
<span class="mods">Menu Links:';
//*
foreach($links[$menu['id']] as $link) {
echo 'Mod:'.$link['link_name'].' : '.$link['link_url']. ' : '.$link['minRank'];
}
//*/
echo'</span></div><br />';
}
Also this is my database table:
PHP Code:
CREATE TABLE `navigation_links` (
`link_id` tinyint(3) NOT NULL auto_increment,
`link_url` varchar(50) NOT NULL default '',
`link_name` varchar(50) NOT NULL default '',
`menu_id` tinyint(21) NOT NULL default '0',
`minRank` int(11) NOT NULL default '0',
`disabled` int(11) NOT NULL default '0',
PRIMARY KEY (`link_id`)
) TYPE=MyISAM AUTO_INCREMENT=34 ;
The problems i had... Were either all links and nav's would show.Or just nav's would show. Depending on what i changed in the query.
Help is much needed and apperciated.
EDIT:: RESOLVED
Solution::
PHP Code:
$sql = 'SELECT * FROM navigation_links WHERE menu_id= -1';
$result = mysql_query($sql) or die(mysql_error());
$forums = array();
while($row = mysql_fetch_array($result)) {
$forums[] = $row;
}
$sql1 = "SELECT nav.*, nav2.* FROM navigation_links nav JOIN navigation_links nav2 ON nav2.menu_id = nav.link_id";
$result = mysql_query($sql1) or die(mysql_error());
$moderators = array();
while($row1 = mysql_fetch_array($result)) {
$moderators[$row1['menu_id']][] = array('url' => $row1['link_url'], 'name' => $row1['link_name']);
}
foreach($forums as $forum) {
?>
<div class="forum">
<span class="name"><?php echo $forum['link_name']; ?></span>
<span class="description"><?php echo $forum['link_url']; ?></span>
<span class="mods">Moderators:
<?php
foreach($moderators[$forum['link_id']] as $mod) {
?>
<a href="userinfo.php?id=<?php echo $mod['url']; ?>"><?php echo $mod['name']; ?></a>
<?php
}
?></span>
<?
}
it was this query that made it happen.
PHP Code:
$sql1 = "SELECT nav.*, nav2.* FROM navigation_links nav JOIN navigation_links nav2 ON nav2.menu_id = nav.link_id";
Check That...maybe NOt.
Re: [RESOLVED] Foreach Problem.
hmmm.... Well It works now... but i have another problem.
I need to be able to disable/enable the links.... I was thinking of using a checkbox. Where checked would be disabled. And enabled would be unchecked.... But the problem with this is... if its already enabled... it will be disabled. And Vise Versa.
Or a radio button system...But the snag there was... I click 1 radio button for enabled on 1 link.... and thats it... only does it for 1 link.
This is the code i use now.
PHP Code:
<?php
function disabled_menus_links(){
if(isset($_POST['update_submit'])){
if(is_array($_POST['navigation'])){
foreach($_POST['navigation'] as $id){
echo $id;
$query = mysql_query( "SELECT * FROM navigation_links WHERE link_id = $id" );
while ( $row = mysql_fetch_array( $query ) ) {
if ( $row['disabled'] == 0 ) {
mysql_query( "UPDATE navigation_links SET disabled = 1 WHERE link_id = $id" );
echo'Disabled';
echo $id . ' == ' . $row['link_id'];
}
if ( $row['disabled'] == 1 ) {
mysql_query( "UPDATE navigation_links SET disabled = 0 WHERE link_id = $id" );
echo'Undisabled';
echo $id . ' == ' . $row['link_id'];
}
}
}
}
}else{
$sql = 'SELECT * FROM navigation_links WHERE menu_id= -1';
$result = mysql_query( $sql ) or die( mysql_error() );
$menus = array();
while ( $row = mysql_fetch_array( $result ) ) {
$menus[] = $row;
}
$sql1 = "SELECT nav.*, nav2.* FROM navigation_links nav JOIN navigation_links nav2 ON nav2.menu_id = nav.link_id";
$result = mysql_query( $sql1 ) or die( mysql_error() );
$links = array();
while ( $row1 = mysql_fetch_array( $result ) ) {
$links[$row1['menu_id']][] = array( 'id' => $row1['link_id'], 'url' => $row1['link_url'], 'name' => $row1['link_name'], 'disabled' => $row1['disabled'] );
}
echo'<form name="form1" method="post" action="' . $_SERVER['REQUEST_URI'] . '">
<table cellspacing="1" cellpadding="0" id="TABLE" class="table_width">
<tr id="TRHEADER">';
echo'
<td colspan="2"><div align="center"><font size="5">Disable/Enable Links or Menus </font></div></td>
';
echo'</tr>
<tr id="TRHEADER">
<td colspan="2">When you click to disable a "menu", ALL links to that menu should be selected for disablement.Same goes for enabling a "menu".<br />
</td>
</tr>
<tr id="TRHEADER">
<td width="85%"><div align="left">Menu/Link Name </div></td>
<td width="15%"><div align="center">Enable/Disable</div></td>
</tr>';
foreach( $menus as $menu ) {
echo'
<tr id="TRHEADER">
<td width="85%">' . $menu['link_name'] . '</td>
<td><div align="center">
<input type="checkbox" name="navigation[]" value="' . $menu['link_id'] . '" ' . ( $menu['disabled'] == 1 ? 'checked':'' ) . '>
</div></td>
</tr>
';
foreach( $links[$menu['link_id']] as $link ) {
echo'
<tr>
<td width="85%" ID="TDROW1">' . $link['name'] . '</td>
<td ID="TDROW2"><div align="center">
<input type="checkbox" name="navigation[]" value="' . $link['id'] . '" ' .
( ( $link['disabled'] == 1 || $menu['disabled'] == 1 ) ? 'checked ':'' ) .
( ( $menu['disabled'] == 1 ) ? ' disabled':'' ) . '>
</div></td>
</tr>
';
}
}
echo'
<tr id="TRFOOTER">';
echo'
<td height="25" colspan="2"><div align="center">
<input name="update_submit" type=submit id="menuadded" value=\'Update Nagigation\' class="buttonlink">
</div></td>
</tr>
</table>
</form>';
}
}
?>
Re: [UnResolved]CheckBox array Problem
How big are your tabs? :sick:
Also, don't use echo() to output HTML. Output is what PHP does anyway. Just close the php tags ?>.
Now, I'm not fully understanding your second problem. When something's enabled, something's disabled... what? can you draw a bad picture of how you want it in Paint or something?
Re: [UnResolved]CheckBox array Problem
Re: [UnResolved]CheckBox array Problem
Quote:
Originally Posted by visualAd
I knew you would post that! :D
Re: [UnResolved]CheckBox array Problem
Quote:
Originally Posted by penagate
I knew you would post that! :D
Doesn't my predictability sicken you? It does me. :D
1 Attachment(s)
Re: [UnResolved]CheckBox array Problem
I knew he'd post it too.
Quote:
Originally Posted by penagate
How big are your tabs? :sick:
Also, don't use echo() to output HTML. Output is what PHP does anyway. Just close the php tags ?>.
Now, I'm not fully understanding your second problem. When something's enabled, something's disabled... what? can you draw a bad picture of how you want it in Paint or something?
Thats PHPEdits Code Beautifier for you. :)
Also i dont care about what the inside looks like right now. I'll be going thru it and fixing things up when i finish the few problems im still having...
Not to mention...That no one will be seeing the inside of the script anyways.Or so we are hoping. Im also slowly learning to indent properly.
My Second Problem is like this.
This problem is occuring in the "$_POST['update_submit'] IF Statement" at the top
PHP Code:
if(is_array($_POST['navigation'])){
foreach($_POST['navigation'] as $id){
echo $id;
$query = mysql_query( "SELECT * FROM navigation_links WHERE link_id = $id" );
while ( $row = mysql_fetch_array( $query ) ) {
if ( $row['disabled'] == 0 ) {
mysql_query( "UPDATE navigation_links SET disabled = 1 WHERE link_id = $id" );
echo'Disabled';
echo $id . ' == ' . $row['link_id'];
}
if ( $row['disabled'] == 1 ) {
mysql_query( "UPDATE navigation_links SET disabled = 0 WHERE link_id = $id" );
echo'Undisabled';
echo $id . ' == ' . $row['link_id'];
}
}
}
}
http://vbforums.com/attachment.php?a...id=48388&stc=1
Like the image states.If i disable a link.An enabled Link will be disabled..But not all the time...Sometimes in order for the link to enable.Sumtin must be disabled....
Im trying to find a solution.That in the end.Will allow me to enable and disable links.One at a time.And it needs to know whether im disabling or enabling a link or many links.
Like i said...This is confusing.
Re: [UnResolved]CheckBox array Problem
There's gotta be a solution...
Fixed up Code.However I still will be echo'ing the HTML.For Now.
Pointless...this way seems...yet i've seen it done.Cant Remember where...tho.
I'll try a different way...for now.
What would be my best solution to doing what im trying to do? Instead of using checkbox's what else could i do....
Idk why but... i cant seem to think.