Jump to content

How do i add a checkbox to delete all games that are tiicked ?.


Davie33

Recommended Posts

Hi am new here so go easy on me please lol.I need help please.

What it is am trying to add is a checkbox to delete all games that are tiicked with the checkbox.

 

This is the code am working on.

<div id="nav_header"><font size="+1" color=red><i>Manage Games</i></font></div><br/>
<div id="tablecontent">
<?php
if(!empty($_GET['edit'])) {
$query = yasDB_select("SELECT * FROM games WHERE id = '{$_GET['edit']}'",false);
if($query->num_rows == 0) {
	echo 'You cannot edit a file that doesnt exist.';
	$query->close();

} else {
	$row = $query->fetch_array(MYSQLI_ASSOC);
	$query->close();
	$catName = array();
	$catId = array();
	$i = 0;
	$query = yasDB_select("SELECT id, name FROM categories",false);
	while($names = $query->fetch_array(MYSQLI_BOTH)) {
		$catName[$i] = $names['name'];
		$catId[$i] = $names['id'];
		$i++;
	}
	$query->close();
	?>
	<center><form name="edit" method="post" action="index.php?act=managegames">
	Title:<br/>
	<input type="text" name="title" value="<?php echo $row['title'];?>"  /><br/>
	Game Height:<br/>
	<input type="text" name="gameheight" value="<?php echo $row['height'];?>" /><br/>
	Game Width:<br/>
	<input type="text" name="gamewidth" value="<?php echo $row['width'];?>" /><br/>
	Description:<br/>
	<textarea name="description" /><?php echo $row['description'];?></textarea><br/>
	Instructions:<br/>
	<textarea name="instructions" /><?php echo $row['instructions'];?></textarea><br/>
	Keywords:<br/>
	<textarea name="keywords" /><?php echo $row['keywords'];?></textarea><br/>
	Category:<br/>
	<select name="category">
	<option value="<?php echo $row['category'];?>"><?php echo $catName[$row['category']-1];?></option>
	<?php
	$len = count($catName);
	for($i=0;$i<$len;$i++) {
	?><option value="<?php echo $catId[$i];?>"><?php echo $catName[$i];?></option>
	<?php } ?></select><br/>
	Thumbnail:<br/>
	<input type="text" name="thumbnail" value="<?php echo $row['thumbnail'];?>"  /><br /><br/>
	File:<br/>
	<select name="type">
	<option value="<?php echo $row['type'];?>"><?php echo $row['type'];?></option>
    <option value="SWF">swf</option>
    <option value="DCR">dcr</option>
    <option value="FLV">flv</option>
    <option value="WMV">wmv</option>
    <option value="AVI">avi</option>
    <option value="MPG">mpg</option>
    <option value="MOV">mov</option>
    <option value="IMAGE">image</option>
    <option value="YOUTUBE">youtube</option>
    </select><br/>
    <input type="text" name="file" value="<?php echo $row['file'];?>"/><br/>
    </select><br/>
	<input type="hidden" name="id" value="<?php echo $row['id'];?>"/>
	<input type="hidden" name="m" value="<?php echo $_GET['m'];?>"/>
	<input type="submit" name="edit" value="Edit!"/> 
	<input type="reset" name="reset" value="Reset"/>
	</form></center>
<?php			
}

} elseif(isset($_POST['edit'])) {
if(empty($_POST['title']) || empty($_POST['thumbnail'])) {
	echo 'One or more fields was left empty.<br />';
	echo '<a href="index.php?act=managegames&edit=' . $_POST['id'] . '">Click here to go back</a>';
} else {
	yasDB_update("UPDATE games SET title = '{$_POST['title']}', description = '{$_POST['description']}', instructions = '{$_POST['instructions']}',keywords = '{$_POST['keywords']}', category = '{$_POST['category']}', height = '{$_POST['gameheight']}', width = '{$_POST['gamewidth']}', type = '{$_POST['type']}', thumbnail = '{$_POST['thumbnail']}', file = '{$_POST['file']}' where id = '{$_POST['id']}'",false);
	echo '<p align="center">File Successfully edited!<br />';
	if (!empty($_POST['m'])) {
		echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
	} else {
		echo '<a href="index.php?act=brokenfiles">Click here to proceed</a></p>';
	}
}
} elseif(!empty($_GET['delete'])) {
$query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '{$_GET['delete']}'",false);
if($query->num_rows == 0) {
	echo 'You cannot delete a game that does not exist!<br />';
	echo '<a href="index.php?act=managegames">Click here to go back</a>';
} else {
	$row = $query->fetch_array(MYSQLI_ASSOC);
	@unlink('../' . $row['file']);
	@unlink('../' . $row['thumbnail']);
	$query->close();

	yasDB_delete("delete from games where id = '{$_GET['delete']}'",false);
	echo '<p style="text-align:center;">Game successfully deleted.<br />';
	echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
}
} else {
if (isset($_GET['page'])) {
   $pageno = $_GET['page'];
} else {
   $pageno = 1;
} 
$result = yasDB_select("SELECT count(id) FROM games");
$query_data = $result->fetch_array(MYSQLI_NUM);;
$numrows = $query_data[0];
$result->close();

$rows_per_page = 20;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($lastpage < 1) {
	$lastpage = 1;
}
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} 
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
echo '<table width="885">
<tr>
<td>#</td>
<td><font color=red><i>ID</i></font></td>
<td></td>
<td><font color=red><i>Title</i></font></td>
<td><font color=red><i>Description</i></font></td>
<td><font color=red><i>Category</i></font></td>
<td></td>
<td></td>
</tr>';
$query_cats = yasDB_select("SELECT name FROM categories");	
$cats = array();
$i = 0;
while ($catRow = $query_cats->fetch_array(MYSQLI_ASSOC)) {;
	$cats[$i] = $catRow['name'];
	$i++;
}
$query_cats->close();
$query = yasDB_select("SELECT * FROM games ORDER BY `id` DESC $limit");
if($query->num_rows==0) {
    } else { 

	while($row = $query->fetch_array(MYSQLI_ASSOC)) {
		echo '<tr>
			<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"></td>
			<td>' . $row['id'] . '</td>
			<td><img src="' . $siteurl . $row['thumbnail'] . '" style="width: 60px; height: 50px;" /></td>
			<td>' . $row['title'] . '</td>
			<td>' . $row['description'] . '</td>
			<td>' . $cats[$row['category']-1] . '</td>
			<td><font color=red><i><a href="index.php?act=managegames&edit=' . $row['id'] . '&m=1">Edit</a></i></font></td>
			<td><font color=red><i><a href="index.php?act=managegames&delete=' . $row['id'] . '" onclick="return confirm(\'Are you sure you want to delete this game?\')">Delete</a></i></font></td>
			</tr>';
	}
}
unset ($cats);
$query->close();

echo '<tr>
<td colspan="8" align="center" ><font color=red><i><a href="index.php?act=managegames" onclick="return confirm(\'Are you sure you want to delete these games?\')"><input type="submit" id="' . $row['id'] . '' . $_GET['delete'] .'" value="Delete"></a></i></font></td>
</tr></table>
<br /><div style="text-align:center;padding-bottom:10px;">';
				if ($pageno == 1) {
   echo ' FIRST PREV ';
} else {
   echo ' <a href="index.php?act=managegames&page=1">FIRST</a> ';
   $prevpage = $pageno-1;
   echo ' <a href="index.php?act=managegames&page=' . $prevpage . '">PREV</a> ';
} 
echo ' ( Page ' . $pageno . ' of ' . $lastpage . ' ) ';
if ($pageno == $lastpage) {
   echo ' NEXT LAST ';
} else {
   $nextpage = $pageno+1;
   echo ' <a href="index.php?act=managegames&page=' . $nextpage . '">NEXT</a> ';
   echo ' <a href="index.php?act=managegames&page=' . $lastpage . '">LAST</a> ';
}
echo '<div style="clear:both;"></div></div>';
}
?>
</div>

Link to comment
Share on other sites

I'm not going to read through all of that code to try and figure out the exact changes to make. Not to be rude, but it is very disorganized. Think about separating the "logic" of the page (the PHP code) from the presentation (the HTML).

 

Anyway, the solution is very straight-forward. Just create your checkboxes named as an array variable with the values as the IDs of the records to be deleted. Example:

<input type="checkbox" name="deleteIDs[]" value="1"> Record 1
<input type="checkbox" name="deleteIDs[]" value="2"> Record 2
<input type="checkbox" name="deleteIDs[]" value="3"> Record 3

 

Then in the processing page, simply use the array values in a query to delete all the recorsd

if(isset($_POST['deleteIDs'])
{
    $idList = implode(', ', $_POST['deleteIDs']);
    $query = "DELETE FROM table WHERE id IN ({$idList})";
    $result = mysql_query($query);

}

Link to comment
Share on other sites

Hi thanks for the reply so fast.All i know is that a 'for loop' has to be added but dont know how to add it in where  elseif(!empty($_GET['delete'])) {.

If thats any help.

 

here is my delete code

elseif(!empty($_GET['delete'])) {
$query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '{$_GET['delete']}'",false);
if($query->num_rows == 0) {
	echo 'You cannot delete a game that does not exist!<br />';
	echo '<a href="index.php?act=managegames">Click here to go back</a>';
} else {
	$row = $query->fetch_array(MYSQLI_ASSOC);
	@unlink('../' . $row['file']);
	@unlink('../' . $row['thumbnail']);
	$query->close();

	yasDB_delete("delete from games where id = '{$_GET['delete']}'",false);
	echo '<p style="text-align:center;">Game successfully deleted.<br />';
	echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
}

 

this is the checkbox

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '">
<input name="delete" type="submit" id="delete" value="Delete">

Link to comment
Share on other sites

All i know is that a 'for loop' has to be added but don't know how to add it in where 

 

Not necessarily. Although it looks like you are doing more than deleting the DB records - you are also deleting some files. Below is some "expanded" sample code. By the way, giving your checkboxes the name of "checkbox[]" is just dumb, give your objects meaningful names. I'll let you replace with your custom DB functions

//Check if ANY of the delete checkboxes were checked
// If any were checked then $_POST['deleteIDs'] will be in the post data
if(!isset($_POST['checkbox'])
{
    //No records were selected for deletion
    echo 'No games selected for deletion.<br />';
    echo '<a href="index.php?act=managegames">Click here to go back</a>';
}
else
{
    //Create a string of the IDs which is comma separated
    $idList = implode(', ', $_POST['checkbox']);

    //Create/run a query to select all the records seelcted for deletion
    $query = "SELECT file, thumbnail FROM games WHERE id IN ({$idList})";
    $result = mysql_query($query);

    //Delete files associated with each record in DB result
    while($record = mysql_fetch_assoc($result))
    {
        @unlink('../' . $row['file']);
        @unlink('../' . $row['thumbnail']);
    }

    //Create/run a query that will delete all the records within the comma separated list
    $query = "DELETE FROM games WHERE id IN ({$idList})";
    $result = mysql_query($query);

    echo '<p style="text-align:center;">Game(s) successfully deleted.<br />';
    echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
}

Link to comment
Share on other sites

Thanks for the help on this code :).I kinda get what your saying mate but i dont know much about php still learning it.

 

Well, I added plenty of comments to explain what was happening at each step. What do you need clarification on?

Link to comment
Share on other sites

Thanks for the help on this code :).I kinda get what your saying mate but i dont know much about php still learning it.

 

Well, I added plenty of comments to explain what was happening at each step. What do you need clarification on?

 

Hi if u can get the time too look at the file u will see what the code does.Am trying to find out what line i should start the code at.AS u will see from the images.

I have add the delete button at the bottom of the page and the check box in the main table now i already have {DELETE} in the main table at the right hand side.So its getting to add the code to that delete code i already have if this make any sense to u.

Untitled-19.png

Untitled1.png

 

This is my manage_game.php file,I also redone the checkbox that had this [] as u said.

 

<div id="nav_header"><font size="+1" color=red><i>Manage Games</i></font></div><br/>
<div id="tablecontent">
<?php
if(!empty($_GET['edit'])) {
$query = yasDB_select("SELECT * FROM games WHERE id = '{$_GET['edit']}'",false);
if($query->num_rows == 0) {
	echo 'You cannot edit a file that doesnt exist.';
	$query->close();

} else {
	$row = $query->fetch_array(MYSQLI_ASSOC);
	$query->close();
	$catName = array();
	$catId = array();
	$i = 0;
	$query = yasDB_select("SELECT id, name FROM categories",false);
	while($names = $query->fetch_array(MYSQLI_BOTH)) {
		$catName[$i] = $names['name'];
		$catId[$i] = $names['id'];
		$i++;
	}
	$query->close();
	?>
	<center><form name="edit" method="post" action="index.php?act=managegames">
	Title:<br/>
	<input type="text" name="title" value="<?php echo $row['title'];?>"  /><br/>
	Game Height:<br/>
	<input type="text" name="gameheight" value="<?php echo $row['height'];?>" /><br/>
	Game Width:<br/>
	<input type="text" name="gamewidth" value="<?php echo $row['width'];?>" /><br/>
	Description:<br/>
	<textarea name="description" /><?php echo $row['description'];?></textarea><br/>
	Instructions:<br/>
	<textarea name="instructions" /><?php echo $row['instructions'];?></textarea><br/>
	Keywords:<br/>
	<textarea name="keywords" /><?php echo $row['keywords'];?></textarea><br/>
	Category:<br/>
	<select name="category">
	<option value="<?php echo $row['category'];?>"><?php echo $catName[$row['category']-1];?></option>
	<?php
	$len = count($catName);
	for($i=0;$i<$len;$i++) {
	?><option value="<?php echo $catId[$i];?>"><?php echo $catName[$i];?></option>
	<?php } ?></select><br/>
	Thumbnail:<br/>
	<input type="text" name="thumbnail" value="<?php echo $row['thumbnail'];?>"  /><br /><br/>
	File:<br/>
	<select name="type">
	<option value="<?php echo $row['type'];?>"><?php echo $row['type'];?></option>
    <option value="SWF">swf</option>
    <option value="DCR">dcr</option>
    <option value="FLV">flv</option>
    <option value="WMV">wmv</option>
    <option value="AVI">avi</option>
    <option value="MPG">mpg</option>
    <option value="MOV">mov</option>
    <option value="IMAGE">image</option>
    <option value="YOUTUBE">youtube</option>
    </select><br/>
    <input type="text" name="file" value="<?php echo $row['file'];?>"/><br/>
    </select><br/>
	<input type="hidden" name="id" value="<?php echo $row['id'];?>"/>
	<input type="hidden" name="m" value="<?php echo $_GET['m'];?>"/>
	<input type="submit" name="edit" value="Edit!"/> 
	<input type="reset" name="reset" value="Reset"/>
	</form></center>
<?php			
}

} elseif(isset($_POST['edit'])) {
if(empty($_POST['title']) || empty($_POST['thumbnail'])) {
	echo 'One or more fields was left empty.<br />';
	echo '<a href="index.php?act=managegames&edit=' . $_POST['id'] . '">Click here to go back</a>';
} else {
	yasDB_update("UPDATE games SET title = '{$_POST['title']}', description = '{$_POST['description']}', instructions = '{$_POST['instructions']}',keywords = '{$_POST['keywords']}', category = '{$_POST['category']}', height = '{$_POST['gameheight']}', width = '{$_POST['gamewidth']}', type = '{$_POST['type']}', thumbnail = '{$_POST['thumbnail']}', file = '{$_POST['file']}' where id = '{$_POST['id']}'",false);
	echo '<p align="center">File Successfully edited!<br />';
	if (!empty($_POST['m'])) {
		echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
	} else {
		echo '<a href="index.php?act=brokenfiles">Click here to proceed</a></p>';
	}
}
} elseif(!empty($_GET['delete'])) {
$query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '{$_GET['delete']}'",false);
if($query->num_rows == 0) {
	echo 'You cannot delete a game that does not exist!<br />';
	echo '<a href="index.php?act=managegames">Click here to go back</a>';
} else {
	$row = $query->fetch_array(MYSQLI_ASSOC);
	@unlink('../' . $row['file']);
	@unlink('../' . $row['thumbnail']);
	$query->close();

	yasDB_delete("DELETE FROM games WHERE id = '{$_GET['delete']}'",false);
	echo '<p style="text-align:center;">Game successfully deleted.<br />';
	echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';
}

} else {
if (isset($_GET['page'])) {
   $pageno = $_GET['page'];
} else {
   $pageno = 1;
} 
$result = yasDB_select("SELECT count(id) FROM games");
$query_data = $result->fetch_array(MYSQLI_NUM);;
$numrows = $query_data[0];
$result->close();

$rows_per_page = 20;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($lastpage < 1) {
	$lastpage = 1;
}
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} 
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
echo '<form name="checkbox" method="post" action="">
<table width="885">
<tr>
<td>#</td>
<td><font color=red><i>ID</i></font></td>
<td></td>
<td><font color=red><i>Title</i></font></td>
<td><font color=red><i>Description</i></font></td>
<td><font color=red><i>Category</i></font></td>
<td></td>
<td></td>
</tr>';
$query_cats = yasDB_select("SELECT name FROM categories");	
$cats = array();
$i = 0;
while ($catRow = $query_cats->fetch_array(MYSQLI_ASSOC)) {;
	$cats[$i] = $catRow['name'];
	$i++;
}
$query_cats->close();
$query = yasDB_select("SELECT * FROM games ORDER BY `id` DESC $limit");
if($query->num_rows==0) {
    } else { 

	while($row = $query->fetch_array(MYSQLI_ASSOC)) {
		echo '<tr>
			<td><input name="checkbox" type="checkbox" id="checkbox" value="' . $row['id'] . '"></td>
			<td>' . $row['id'] . '</td>
			<td><img src="' . $siteurl . $row['thumbnail'] . '" style="width: 60px; height: 50px;" /></td>
			<td>' . $row['title'] . '</td>
			<td>' . $row['description'] . '</td>
			<td>' . $cats[$row['category']-1] . '</td>
			<td><font color=red><i><a href="index.php?act=managegames&edit=' . $row['id'] . '&m=1">Edit</a></i></font></td>
			<td><font color=red><i><a href="index.php?act=managegames&delete=' . $row['id'] . '" onclick="return confirm(\'Are you sure you want to delete this game?\')">Delete</a></i></font></td>
			</tr>';
	}
}
unset ($cats);
$query->close();

echo '<tr>
<td colspan="8" align="center"><input name="delete" type="submit" id="delete" value="Delete" onclick="return confirm(\'Are you sure you want to delete these game?\')"></td>
</tr></table></form>
<br /><div style="text-align:center;padding-bottom:10px;">';
				if ($pageno == 1) {
   echo ' FIRST PREV ';
} else {
   echo ' <a href="index.php?act=managegames&page=1">FIRST</a> ';
   $prevpage = $pageno-1;
   echo ' <a href="index.php?act=managegames&page=' . $prevpage . '">PREV</a> ';
} 
echo ' ( Page ' . $pageno . ' of ' . $lastpage . ' ) ';
if ($pageno == $lastpage) {
   echo ' NEXT LAST ';
} else {
   $nextpage = $pageno+1;
   echo ' <a href="index.php?act=managegames&page=' . $nextpage . '">NEXT</a> ';
   echo ' <a href="index.php?act=managegames&page=' . $lastpage . '">LAST</a> ';
}
echo '<div style="clear:both;"></div></div>';
}
?>
</div>

Link to comment
Share on other sites

You haven't stated what problems you are having. I'm not going to try and read through all of your code and try to guess what your problem is. You need to: state what is happening, what you expect to happen, and provide the relevant code that is producing the unwanted code. You do that, by debugging the code. Put some echo statements in at key points to determine what "branches" the logic is going through: e.g. a different echo within the IF and ELSE blocks of code. If there are key variables being interpreted, you should be echoing those at key points in the logic as well.

Link to comment
Share on other sites

I think the big issue here is he's trying to modify code he doesn't understand.

Hi that is true i don't understand how to modify it i have been working on this script for the past 2 years now with someone else that knows more about coding than me but he is very busy like everyone else and everything i add is new to me so i am trying to understand it but some times i do get abit confused.

What mjdamato said about the code he gave as a sample is cool but working out how to add it in my code am confused @.

Link to comment
Share on other sites

Hi mjdamato sry i just noticed your post.

 

if u look back at the pics u will see a delete link ? they links are with this code here.

elseif(!empty($_GET['delete'])) {



$query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '{$_GET['delete']}'",false);



if($query->num_rows == 0) {





echo 'You cannot delete a game that does not exist!<br />';





echo '<a href="index.php?act=managegames">Click here to go back</a>';



} else {





$row = $query->fetch_array(MYSQLI_ASSOC);





@unlink('../' . $row['file']);





@unlink('../' . $row['thumbnail']);





$query->close();










yasDB_delete("delete from games where id = '{$_GET['delete']}'",false);





echo '<p style="text-align:center;">Game successfully deleted.<br />';





echo '<a href="index.php?act=managegames">Click here to proceed</a></p>';



}

so now i have added form for check box to it as u can see the checkbox in the row and added a delete button down at the bottom of the page ?

what the problem is me not knowing how to make the rest of the code and what line i should start at.

sry for being a pain.

Link to comment
Share on other sites

hi again we have managed to sort the checkbox out but one thing is that when i hit the cancel button it still deletes all selected.

 

this is the javascript for it.

 

elseif(isset($_POST['deletechecked'])) {
    $count = count($_POST['checkbox']);
    ?>
    <script type = "text/javascript">
        var response = confirm("Are you sure you want to delete <?php echo $count;?> game(s)?");
        if (!response) {
            window.location.href = '<?php echo $siteurl;?>admin/index.php?act=managegames&remove=notta';
            
        }
    </script>

 

this is the checkbox delete code

 

<?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '$box'",false);
        if($query->num_rows == 0) {
            echo 'You cannot delete a game that does not exist!<br />';
            echo '<a href="index.php?act=managegames">Click here to go back</a>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                @unlink('../' . $row['file']);
                @unlink('../' . $row['thumbnail']);
                $query->close();            
                yasDB_delete("delete from games where id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $siteurl;?>admin/index.php?act=managegames" />
                <?php
            }
        }

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.