Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with checkbox range 1

Status
Not open for further replies.

darkstate

Technical User
Mar 17, 2009
8
GB
I don't know If I'm in the correct place but you may be able to help.
I'm trying to find some code to fill a range of checkboxes.
I have over 400 hundred checkboxes that need a tick next to them,as you can Imagine doing this is a touch on the boring side(See the below code how that was solved)
My question is lets say I only need to tick say boxes 50 to 380 Is there someway of doing this?
I have got code to tick all 400 boxes in 1 go like this.

<!-Extra Reverse selected button->
<div id="groups" class="admin">
<form name="form2" method="post" action="admin.php">
<input class="newgroup" type="button" value="Reverse selected"

onClick="inverse(this.form);">
<table border="0" cellspacing="1" cellpadding="1">
<!-Extra Reverse selected button->

I don't want a page full of code to accomplish this,Just short and sweet like the above code.
Thanks for your time In solving this problem.
 
Hi

darkstate said:
I have got code to tick all 400 boxes in 1 go like this.
If you have code, post it. Do not expect to write again from scratch what you already have.

By the way, is there something common in those checkboxes ? Like
[ul]
[li]identical [tt]name[/tt][/li]
[li]identical [tt]class[/tt][/li]
[li]similar [tt]id[/tt][/li]
[li]similar [tt]value[/tt][/li]
[li]common [tt]form[/tt][/li]
[li]common container[/li]
[/ul]


Feherke.
 
This is the whole Code, I'm sure you don't need to see all this as Its overkill.
AS I say Just a simple bit of code that can solve my problem of having to tick all the boxes that need updating within a range.
Thanks for your time .

<?php
/*******************************************************************************
*
* Copyright (C) 2007
*
*************************************************************************************/
function session_defaults() {
$_SESSION['logged'] = false;
$_SESSION['admin']=false;
$_SESSION['ip'] = '';
}

function check_login($us,$ps){
if (!isset($_SESSION['ip']) ) {session_defaults();}
if (!$_SESSION['logged']){
if(isset($_POST['user']) && isset($_POST['passwd'])){
//check login credentials
if(($_POST['user']==$us) && ($_POST['passwd']==$ps)){
$_SESSION['logged']=true;
$_SESSION['admin']=true;
$_SESSION['ip']=md5($_SERVER['REMOTE_ADDR']);
return true;
}else{
echo 'bad login <a href="admin.php">try again</a>.';
exit;
}
}else{
echo '<html><head><link href="style.css" rel="stylesheet" type="text/css"/></head>
<body><div class="header"><font color="orange">ikbin admin</font></div>
<br/>

<br/>
<div class="admin" style="width:300px;padding-right:40px"><br/>
<center><b>You must login to use this function.</b><br/>
<form name="form1" id="form1" method="post" action="admin.php">
Username: <input name="user" type="text" id="user" /><br/>
Password: <input name="passwd" type="password" id="passwd" /><br/>
<input type="submit" name="Submit" value="Login" />
</form></center></div></body></html>';
exit;
}
}else{//logged is true, check IP match
if(md5($_SERVER['REMOTE_ADDR'])==$_SESSION['ip']&&$_SESSION['admin']==true){
return true;
}else{
//something funny happened, clear the session and send by to admin.php
session_defaults();
header('Location:admin.php');
exit;
}
}
}

function addgroup($group,&$ns){
if($ns->nntp_con==false){
$message='Error opening connection: '.$ns->errorLog;
return array(false,$message);
}
$ns->changegroup($group);
if (!$ns->group['status']){
$message='Error opening group: '.$ns->errorLog;
return array(false,$message);
}
//do this
$message='Group added: '.$ns->group['name'].' First ID: '.$ns->group['firstID'].' Last ID:'.$ns->group['lastID'];
return array(true,$message);
}

function groupinfo($group,&$ns){
$ns->changegroup($group);
if (!$ns->group['status']){
$message='Error opening group: '.$ns->errorLog;
}else{
$message="Group Info - $group<br/>First header ID: ".
$ns->group['firstID'].'<br/>Last Header ID: '.$ns->group['lastID'].'<br/>';
}
return $message;
}

require_once('config.backend.php');
session_start();
if(isset($_GET['logout'])){
session_defaults();
}
$up=new Admin_login;
$log=check_login($up->user,$up->passwd);

$db=new Admin_db;
$db->connect();
$sql=new sqladmin;
$sql->db=&$db;

//add a new group
if(isset($_POST['Add'])){
//add a check when groups are added:
//make sure that there is a row in the site stats table.
$sql->checksitestatstable();


$ns=new My_nntp();
$t=$ns->startNNTPconnection();
if(strlen($_POST['newgroup'])>strlen($_POST['newgroups'])){
$m=addgroup($_POST['newgroup'],$ns);
$message=$m[1];
if($m[0]){$sql->addgroup($_POST['newgroup']);}
}else{
$grouprows=explode("\r\n",$_POST['newgroups']);
$i=0;
$gac=array();
foreach($grouprows as $group){
if(strlen($group)>0){
$g=explode(',',$group);
if(strlen($g[0])>0){
if(!is_numeric($g[1])){$g[1]=0;}
$g[0]=trim(stripslashes($g[0]),"\"");
if(isset($_POST['groupcheck'])&&($_POST['groupcheck']>0)){
$m=addgroup($g[0],$ns);
$message.=$m[1].'<br/>';
if($m[0]){
$gac[$i]['group']=$g[0];
$gac[$i]['purgeage']=$g[1];
$i++;
}
}else{
$gac[$i]['group']=$g[0];
$gac[$i]['purgeage']=$g[1];
$i++;
}
}
}
}
if($i>10){echo '<h2>If you add more than 127 groups, be sure that you increase the data size of `key` (groups table) and `group` (collections and files table) to smallint or mediumint!!</h2><br/><br/>';}
$sql->addgrouparray($gac);
$message.="<br/>$i groups added<br/>If you add more than 127 groups, be sure that you increase the data size of `key` (groups table) and `group` (collections and files table) to smallint or mediumint!!<br/>";
}
$ns->closeNNTPconnection();
}

if(isset($_POST['update'])){
$i=0;
foreach($_POST as $key => $value){
if($value=='action'){
$checked[$i]=$key;
$i++;
}
}
if($i<1){
$message="Nothing Selected!";
$_POST['action']='';//clear action so no switch
}

switch($_POST['action']){
case "Info":
$message=0;
$ns=new My_nntp();
if(!$ns->startNNTPconnection()){
$message='Error opening connection: '.$ns->errorLog;
break;
}
foreach($checked as $key=>$value){
$group=$sql->ID2GROUP($value);
$tm.=groupinfo($group,$ns);
$tm.='<br/>';
$message++;
}
$ns->closeNNTPconnection();
$message=$tm.$message.' groups queried.';

break;
case "Refresh":
$message=0;
foreach($checked as $key=>$value){
$sql->UPDATEGROUPSTATS($value);
$message++;
}
$sql->updatesitestats();
$sql->updateNFOdirinfo($GLOBALS['CONF_nfo']['folder']);
$message=$tm.$message.' groups refreshed.';
break;
case "Unlock":
$message=0;
foreach($checked as $key=>$value){
$override=$sql->toggleupdatelock(false,$value);
if($override){
$group=$sql->ID2GROUP($value);
$tm.='Lock overridden on '.$group.'.<br/>';
}
$message++;
}
$message=$tm.$message.' groups unlocked.';
break;
case "Import":
$ns=new My_nntp();

if(!$ns->startNNTPconnection()){
$message='Error opening connection: '.$ns->errorLog;
break;
}

$fill=new filler;
$fill->live=true;
$fill->sql=&$sql;
$fill->ns=&$ns;

if(isset($_POST['starthdr'])){$starthdr=$_POST['starthdr'];}else{$starthdr=0;}
if(isset($_POST['numhdr'])){$numhdr=$_POST['numhdr'];}else{$numhdr=10000;}
if(isset($_POST['all'])&&($_POST['all']==true)){$allhdr=true;}else{$allhdr=false;}

$i=0;
foreach($checked as $value){
$gi['groupid']=$value;
$gi['group']=$sql->ID2GROUP($value);
$gi['lastheaderid']=$sql->lastheaderid($value);
$r=$fill->doXOVER($gi,$numhdr,$starthdr,$allhdr);
$i++;
}

//clear out the NFO cache
$fill->processNFOcache();
$ns->closeNNTPconnection();
$sql->updatesitestats();
$sql->updateNFOdirinfo($GLOBALS['CONF_nfo']['folder']);
foreach($fill->errorLog['e'] as $key=>$value){
$message.=$value.'<br>';
}
$message.=$i.' groups imported.';
break;
case "Activate":
$message=0;
foreach($checked as $key=>$value){
$sql->setgroupactive($value,'1');
$message++;
}
$message=$message.' groups activated';
break;
case "Deactivate":
$message=0;
foreach($checked as $key=>$value){
$sql->setgroupactive($value,'0');
$message++;
}
$message=$message.' groups deactivated';
break;
case "Empty":
$message=0;
foreach($checked as $key=>$value){
$sql->flushgroup($value);
$sql->UPDATEGROUPSTATS($value);
$message++;
}
$sql->updatesitestats();
$sql->updateNFOdirinfo($GLOBALS['CONF_nfo']['folder']);
$message=$message.' groups emptied.';
break;
case "PurgeAge":
$message=0;
foreach($checked as $key=>$value){
$sql->purgegroupid($value);
$sql->UPDATEGROUPSTATS($value);
$message++;
}
$sql->updatesitestats();
$sql->updateNFOdirinfo($GLOBALS['CONF_nfo']['folder']);
$message=$message.' groups flushed to retention age.';
break;
case "Remove":
$message=0;
foreach($checked as $key=>$value){
$sql->flushgroup($value);
$sql->deletegroup($value);
$message++;
}
$sql->updatesitestats();
$message=$message.' groups removed.';
break;
case "Purge":
$message=0;
foreach($checked as $key=>$value){
$sql->setpurge($_POST['purgeage'],$value);
$message++;
}
$message="Retention set for $message groups.";
break;
case "Clean":
$sql->cleanuptables();
$sql->updatesitestats();
$message="Orphan files and headers deleted.";
break;
case "PurgeNFO":
$sql->purgeNFOdir($GLOBALS['CONF_nfo']['folder']);
break;
}
}

?>
<html>
<head>
<title>ikbin | admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function showImport(){
var ad=obj('import');
ad.style.display='block';
}

function obj(i){
return document.getElementById(i);
}

//functions added by jordy 25-Feb-07
function inverse($o) {
for ($i=0; $i < $o.elements.length; $i++) {
if ($o.elements[$i].type == "checkbox" && $o.elements[$i].name != "collection"
&& $o.elements[$i].parentNode.parentNode.style.display!='none') {
$o.elements[$i].checked = !$o.elements[$i].checked;
}
}
}
</script>

</head>

<body>
<div class="header"> <font color='orange'>ikbin admin</font> <a href="admin.php?logout=true">logout</a></div>

<center>
<a href='#Bottom'>Bottom</a>
<a name=Top></a>
</center>


<br/>
<div id="message" class="message"><center><font color="red"><?php echo $message;?></font></center></div>
<br/>

<div id="groups" class="admin">
<form name="form2" method="post" action="admin.php">
<input class="newgroup" type="button" value="Reverse selected" onClick="inverse(this.form);">
<table border="0" cellspacing="1" cellpadding="1">
<tr>
<td></td>
<td><strong>Group</strong></td>
<td><strong>Last Scan</strong></td>
<td><strong>Newest File</strong></td>
<td ><strong>Oldest File</strong></td>
<td><strong>Total Files</strong></td>
<td><strong>Last Header ID</strong></td>
<td><strong>Retention</strong></td>
<td><strong>Last Purged</strong></td>
<td><strong>Update Lock</strong></td>
</tr>
<?php
$q=$sql->groups();
$db->query($q);
//display or none
//none
if($db->num_rows()<1){
?><tr><td><font color="red">No Groups</font><td><tr><?php
}else{
//build rows
require_once('class/utilities.class.php');
$uf=new utilityfunctions;
while($db->next_record()){
if($db->Record['active']==1){$bgcolor='#66FF99';}else{$bgcolor='#FF9966';}
if($db->Record['updatelock']){$lock='On';}else{$lock='Off';}
echo'<tr bgcolor="'.$bgcolor.'">
<td><input name="'.$db->Record['key'].'" type="checkbox" value="action"></td>
<td>'.$db->Record['group'].'</td>
<td>'.$uf->fuzzyage($db->Record['lastscan']).'</td>
<td>'.$uf->fuzzyage($db->Record['newestfile']).'</td>
<td>'.$uf->fuzzyage($db->Record['oldestfile']).'</td>
<td>'.$db->Record['totalfiles'].'</td>
<td>'.$db->Record['lastheaderid'].'</td>
<td>'.$db->Record['purgeage'].'</td>
<td>'.$uf->fuzzyage($db->Record['lastpurge']).'</td>
<td>'.$lock.'</td>
</tr>';
}
}
?>
</table>
<br/>


<center>
<a name=Bottom></a>
<a href='#Top'>Top</a>
</center>

<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FF9966" width="10">&nbsp;</td>
<td width="30">Inactive Group</td>
<td bgcolor="#66FF99" width="10">&nbsp;</td>
<td width="5">Active Group</td>
</tr>
<tr>
<td colspan="4" bgcolor="">Only ACTIVE groups are included in the cron job.</td>
</tr>
</table>
<center>
<!-Extra Reverse selected button->


<div id="groups" class="admin">
<form name="form2" method="post" action="admin.php">
<input class="newgroup" type="button" value="Reverse selected" onClick="inverse(this.form);">
<table border="0" cellspacing="1" cellpadding="1">

<!-Extra Reverse selected button->
</center>
<br>

<br/>

<select name="action">
<option value="Info">Get Info</option>
<option value="Import">Import Headers</option>
<option value="Refresh">Refresh Stats</option>
<option value="PurgeAge">Remove Old</option>
<option value="Activate">Activate</option>
<option value="Deactivate">Deactivate</option>
<option value="Purge">Set Retention</option>
<option value="Unlock">Unlock Group</option>
<option value="Empty">Empty</option>
<option value="PurgeNFO">Purge NFOs</option>
<option value="Clean">Clean Orphans</option>
<option value="Remove">Remove</option>
</select>
<input name="update" type="submit" value="Update">
<br/>


<div id="import" style="display:none">
<input type="text" name="starthdr">
Start Header ID <br/>
<input value="10000" type="text" name="numhdr">
Fetch About <br/>
<input type="checkbox" name="all" value="true">
Fetch All <br/>
<input type="text" name="purgeage">
Retention (days)
</div>
<a href="#" onClick="showImport();return false;">+import/purge options.</a>
</form>
</div>
<br/>
<div id="addgroup" class="admin">
<form name="form1" method="post" action="admin.php" >

<table width="518" height="29" border="0" cellpadding="1" cellspacing="1" bordercolor="#000000">
<tr>
<td width="26%">Add group: </td>
<td width="64%"><input name="newgroup" type="text" id="newgroup" size="50"></td>
<td width="10%"><input name="Add" type="submit" id="Add" value="Add"></td>
</tr>
</table>
<div id="addmultiple" style="display:none;">
<table width="518" height="29" border="0" cellpadding="1" cellspacing="1" bordercolor="#000000">
<tr>
<td valign="top">Enter groups to add, one per line (a CSV formated list).
Optionally, enter retention in days.<br/>
<br/>
<em>group[,retention]</em><br/>
<br/>
<input name="groupcheck" type="checkbox" id="groupcheck" value="1" checked> Check that groups exist before adding.
</td>
<td><textarea name="newgroups" cols="45" rows="10" id="newgroups"></textarea></td>
<td>&nbsp;</td>
</tr>
</table>
<br/>
</div>

<a href="#" onClick="document.getElementById('addmultiple').style.display='block';return false;">+add multiple groups</a>
</form>
</div>
<br/>
<div class="footer">powered by <font color="orange">ikbin</font></div>

</body>
</html>
 
Hi

You are right, was pointless to post server-side code. But at least you posted the JavaScript code too.

Supposing that by "50 to 380" you mean the elements' order in the [tt]form[/tt] :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]inverse[/color][teal]([/teal]$o[highlight][teal],[/teal]from[teal],[/teal]to[/highlight][teal])[/teal]
[teal]{[/teal]
  [b]for[/b] [teal]([/teal]$i[teal]=[/teal][highlight][teal]([/teal]from[teal]||[/teal][/highlight][purple]0[/purple][highlight][teal])[/teal][/highlight][teal];[/teal] $i [teal]<[/teal] [highlight][teal]([/teal]to[teal]||[/teal][/highlight]$o[teal].[/teal]elements[teal].[/teal]length[highlight][teal])[/teal][/highlight][teal];[/teal] $i[teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal]$o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]type [teal]==[/teal] [green][i]"checkbox"[/i][/green] [teal]&&[/teal] $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]name [teal]!=[/teal] [green][i]"collection"[/i][/green]
    [teal]&&[/teal] $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]parentNode[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]!=[/teal][green][i]'none'[/i][/green][teal])[/teal] [teal]{[/teal]
      $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]checked [teal]=[/teal] [teal]![/teal]$o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]checked[teal];[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal]
HTML:
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"button"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Reverse 50 to 380"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"inverse(this.form[highlight],49,380[/highlight])"[/i][/green][b]>[/b]
Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
Ok you nearly got it correct.
So this is the scenario,I have 400 checkboxes and new checkboxes are getting added weekly,I have the option of pressing the reverse button and this will tick all the boxes,This i don't need,What I need is say 1 day i need to check boxes 132 to 348,then another day i need to check boxes 6 to 208,I'm presuming theres a way of checking 2 boxes in the range that I want and then pressing a button that will check all the boxes in that range no matter if its 10 boxes or 100 or 156,You get what I mean.
Hopefully that has made it a little clearer.
Thanks for your code Its appreciated.
checkboxes.jpg
 
 http://i753.photobucket.com/albums/xx179/darkstate/checkboxes.jpg
Hi

darkstate said:
What I need is say 1 day i need to check boxes 132 to 348,then another day i need to check boxes 6 to 208
And that image is example for the 132 to 348 or the 6 to 208 range ? Because I see relation between those numbers and the checkbox states.

This function does what I guessed based on the image :
[ul]
[li]the user checks the first and the last checkbox[/li]
[li]clicks the button[/li]
[li]the function reverses all checkboxes between the previously checked first and the last ( excluding the endpoints )[/li]
[/ul]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]inverse[/color][teal]([/teal]$o[teal],[/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] from[teal]=[/teal][purple]0[/purple][teal];[/teal]
  [b]var[/b] to[teal]=[/teal]$o[teal].[/teal]elements[teal].[/teal]length[teal];[/teal]

  [b]if[/b] [teal]([/teal]what[teal]==[/teal][green][i]'range'[/i][/green][teal])[/teal] [teal]{[/teal]
    from[teal]=-[/teal][purple]1[/purple][teal];[/teal]
    [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]$o[teal].[/teal]elements[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
      [b]var[/b] one[teal]=[/teal]$o[teal].[/teal]elements[teal][[/teal]i[teal]][/teal]
      [b]if[/b] [teal]([/teal]one[teal].[/teal]type[teal]==[/teal][green][i]'checkbox'[/i][/green] [teal]&&[/teal] one[teal].[/teal]name[teal]!=[/teal][green][i]'collection'[/i][/green] [teal]&&[/teal] one[teal].[/teal]parentNode[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]!=[/teal][green][i]'none'[/i][/green] [teal]&&[/teal] one[teal].[/teal]checked[teal])[/teal] [teal]{[/teal]
        [b]if[/b] [teal]([/teal]from[teal]==-[/teal][purple]1[/purple][teal])[/teal] from[teal]=[/teal]i[teal]+[/teal][purple]1[/purple]
        to[teal]=[/teal]i
      [teal]}[/teal]
    [teal]}[/teal]
    [b]if[/b] [teal]([/teal]from[teal]==-[/teal][purple]1[/purple][teal])[/teal] from[teal]=[/teal][purple]0[/purple]
  [teal]}[/teal]

  [b]for[/b] [teal]([/teal]$i[teal]=[/teal]from[teal];[/teal] $i [teal]<[/teal] to[teal];[/teal] $i[teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal]$o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]type [teal]==[/teal] [green][i]"checkbox"[/i][/green] [teal]&&[/teal] $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]name [teal]!=[/teal] [green][i]"collection"[/i][/green]
    [teal]&&[/teal] $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]parentNode[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]!=[/teal][green][i]'none'[/i][/green][teal])[/teal] [teal]{[/teal]
        $o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]checked [teal]=[/teal] [teal]![/teal]$o[teal].[/teal]elements[teal][[/teal]$i[teal]].[/teal]checked[teal];[/teal]
     [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal]
HTML:
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"button"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Reverse range"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"inverse(this.form,'range')"[/i][/green][b]>[/b]
As previously, you can still call the inverse() function with only one parameter to have it working as originally.

Feherke.
 
Wow, I'm totally Impressed,That did the job.
Short and sweet what more can a man ask for.
Thanks Feherke., I'm in your debt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top