Multiple CheckBox delete - php

I am having a hard time on creating a multiple checkbox ajax delete. I am an ajax noob.
$sql = 'SELECT * FROM users';
$qry = mysql_query($sql);
$html = '';
$html .='<center>';
$html .= '+ Add New User';
$html .= ' || Delete User';
$html .= '<div id="div-data">';
$html .= '<table border="1" width="300px">';
$html .= '<tr>';
$html .= '<th width="5px"><input type="checkbox" id="cb-checkall"></th>'; //all
$html .= '<th> Users</th>';
//$html .= '<th>Delete</th>';
while($row = mysql_fetch_array($qry)) {
$html .= '<tr align="center">';
$html .= '<td><input type="checkbox" name="checkall" class="checkall" value="'.$row['uid'].'"/></td>';
$html .= '<td>'.$row['fname'].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
$html .= '</div>';
echo $html;
delete.php (with the use of implode)
$deleteid = implode(",",$_POST['checkall']);
$fname = mysql_escape_string($_POST['firstname']);
$sql = "DELETE from `bpo`.`users` (`uid` ,`fname`)
WHERE UID IN (".$deleteid.")";
My data doesnt go to delete.php. idk why. I need help on deleting the checked rows when I push delete button and automatically reflects to my database. The change have to appear on screen without refreshing (AJAX) and with the use of implode. Thank you.

In your form change name="checkall" to name="checkall[]" to allow for multiple values.
Keep in mind that if no options are checked $_POST['checkall'] will be undefined which will generate an Undefined index notice. Use isset or array_key_exists.
If $deleteid is empty (like array()) you query will be malformed, ie.
DELETE from `bpo`.`users` (`uid` ,`fname`) WHERE UID IN ()
// unexpected ) -- ^
This will give you an SQL error. IN clause may not be empty.

Related

Putting php arrays into html tags

I am trying to make a table that has different colors for each of its columns. There will be four columns.
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$html .= '<tr style="background-color:white;">';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
I created an array called colors that has the strings of the colors I want, but I don't know how to put that into the tag. I tried typing it in there, but since it is a string, it takes it as text instead of as code. Where it says "background-color:white;", I'd like it to call the values from the array instead. Thanks.
You can array_pop for this provided you have exactly 4 columns. You also can't apply background colours to <tr> like that, you need to apply them to the <td>
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$color = array_pop($colors);
$html .= "<tr>";
foreach($value as $key2=>$value2){
$html .= "<td style='{$color}'>" . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}

WordPress Admin Menu & Plugin page Missing

I have created a plugin and have done so many times but for some reason I can not seem to get a navigation to the plugin settings page on the admin menu.
The following code I have used is:
// create custom plugin settings menu
add_action('admin_menu', 'uubba_categories_addmenus');
function uubba_categories_addmenus() {
//create new top-level menu
add_menu_page('Check My Stats', 'CMS Page', 'manage_options', 'uubbacmspage', 'uubba_cms_players_page', '', 6);
}
I have also created a settings page but for some reason that is not echo'ing anything what so ever either. The code is as follows:
function uubba_cms_players_page(){
global $wpdb;
//get the players information needed
$getinfo = $wpdb->get_results("SELECT username FROM uubba_userstats");
$output = '<style>';
$output .= '.uubbahr{margin:25px 0px;}';
$output .= '.uubba-button-green,.uubba-button-green:visited{padding:8px 14px;background-color:#8FE485;color:white;}';
$output .= '.uubba-button-green:hover{background-color:#79DC6E;color:white;}';
$output .= '.uubba-button-red,.uubba-button-red:visited{padding:8px 14px;background-color:#F56A6A;color:white;}';
$output .= '.uubba-button-red:hover{background-color:#E34747;color:white;}';
$output .= '<style>';
$output .= '<div class="wrap">';
$output .= '<h1>Check My Stats Players</h1>';
$output .= '<hr class="uubbahr">';
$output .= '<table>';
$output .= '<tr>';
$output .= '<th scope="col"><h5 class="font12">Users Name</h5></th>';
$output .= '<th scope="col"><h5 class="font12">Actions</h5></th>';
$output .= '</tr>';
foreach($getinfo as $gi){
$output .= '<tr>';
$output .= '<td><h3>'.$gi->username.'</h3></td>';
$output .= '<td>View Statistics Delete User</td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= '</div>';
echo $output;
}
I have no idea why this would not work, I have used this method a lot in the past and for some reason it just does not work on this plugin.
If anyone could shed some light on it, I would be most appreciated indeed :)

PHP While Loop/Foreach Loops

I have a question on a different way to wright this code because its not doing what I want it to do..
I am creating a forum list that will display the section under that category. Well this is the issue as you can see that I have an If () {}Else {} statement that is being used to put the information in that list I want. The problem is that I need to add a special class to the last section under that category that will be called last-tr. Now my issue that I have is all the information is making it to the screen but the class is only applied to the last row and now the last row of all the category's which is what i'm looking for.
So something like this,
Welcome Center
Introduce Yourself
Say Hello To our Admins (last-tr)
Game
COD
BF4 (last-tr)
Code
PHP
Java
PDO (last-tr)
but what my code is giving me is
Welcome Center
Introduce Yourself
Say Hello To our Admins
Game
COD
BF4
Code
PHP
Java
PDO (last-tr) <- Just that one.
Here is the code
<?php
include '../../core/init.php';
include '../../includes/head.php';
//Getting Categories under Welcome
$db = DB::getInstance();
$user = New User();
$forum = New Forum();
$list = '';
$category = $db->query('SELECT * FROM forum_categories');
foreach ($category->results() as $category) {
$list .= '<thead>';
$list .= '<tr>';
$list .= '<th class="titles">' . $category->title . '</th>';
$list .= '<th>Last Post</th>';
$list .= '<th>Topics</th>';
$list .= '<th>Replies</th>';
$list .= '</tr>';
$list .= '</thead>';
$list .= '<tbody>';
$sections = $db->query("SELECT * FROM forum_section WHERE category_id = '$category->id'");
foreach ($sections->results() as $section) {
$x = 0;
if ($x < $sections->count()) {
$list .= '<tr>';
$list .= '<td>';
$list .= '<a href="/category.php?id='. $section->id .'">';
$list .= '<img scr="/images/icons/iconmonstr/intro.png">';
$list .= '</a>';
$list .= '' . $section->title . '';
$list .= '<span>' . $section->desc . '</span>';
$list .= '</td>';
$list .= '<td> Nasty </td>';
$list .= '<td> 0 </td>';
$list .= '<td> 0 </td>';
$list .= '</tr>';
$x++;
}else {
$list .= '<tr class="last-tr">';
$list .= '<td>';
$list .= '' . $section->title . '';
$list .= '</td>';
$list .= '<td> Nasty </td>';
$list .= '<td> 0 </td>';
$list .= '<td> 0 </td>';
$list .= '</tr>';
}
}
$list .= '</tbody>';
}
?>
<link rel="stylesheet" href="/css/fourms.css">
<title>Forums</title>
</head>
<body>
<?php include '../../includes/header.php'; ?>
<section id="main_section">
<section id="main_content">
<div id="forum-section">
<table class="forumlist">
<?php echo $list; ?>
</table>
</div>
</section>
</section>
<?php include('../../includes/footer.php'); ?>
Your if-statement should be if ($x < $sections->count()-1) because the last index of an array is always count-1 due to starting at 0.
OR
You could also fix it here by starting $x at 1, since you are using a foreach with the counter on the side, so its not like you'll get an out of index error. In other words:
foreach ($sections->results() as $section) {
$x = 1;
if ($x < $sections->count()) {
...
$x++;
}else {
...
}
}
But going with the first option is probably more straight-forward.
You are testing against your overall class not the data within the class.
$sections->count() is counting all of the sections not the entries in teh one section you are dealing with.
You need to test the count of the singular section
So if you had
$sectionsAll = array(
sectionOne = array( 1, 2, 3),
sectionTwo = array( 1, 2, 3),
sectionThree = array( 1, 2, 3)
);
You are currently testing on $sectionsAll, you need to check against sectionOne, sectionTwo, sectionThree

how do i submit a form with a default value using get if nothing is selected?

this is my form :
$html = "";
$html .= "<div id='options'>";
$html .= "<form action='filter.php' method='get'>";
$html .= "<select multiple='multiple' name='options'>";
foreach($selectValues as $option){
$html .= "<option value='$option'>$option</option>";
}
$html .= "</select><input type='submit' value='submit'></form></div>";
but if i do not select an option from the box and i submit i get the error "Undefined index: options " i want to be able to submit and have it pass a defualt value if nothing is selected
You should probably fix your code in the backend instead to do a !empty($_GET['options']) check. Secondly, your field name should be options[], not options, as it will get overwritten with the last selected value (due to the way PHP handles input). Thirdly, you might want to htmlspecialchars() your $option when you output it. This ensures that weird options won't break your HTML. Finally, if your option has the same value as its label, you don't need to specify it as the value as well.
What about this?
$html = "";
$html .= "<div id='options'>";
$html .= "<form action='filter.php' method='get'>";
$html .= "<select multiple='multiple' name='options'>";
$html .= "<option value='default'>Select An Option</option>";
foreach($selectValues as $option){
$html .= "<option value='$option'>$option</option>";
}
$html .= "</select><input type='submit' value='submit'></form></div>";
Add the selected attribute on one of the option like so:
$html = "";
$html .= "<div id='options'>";
$html .= "<form action='filter.php' method='get'>";
$html .= "<select multiple='multiple' name='options'>";
foreach($selectValues as $option){
$html .= "<option value='$option'" . ($option == 'my default option' ? ' selected="selected" : '') . ">$option</option>";
}
$html .= "</select><input type='submit' value='submit'></form></div>";
This is an immediate if that will check if the option is equal to "your default option" and add selected="selected" to the default option.
However, that will not work if the user unselects the options.
Before trying to use the $_GET['options'], you should always check if it's defined using isset.

output query in strict table formate in code-igniter

my code is below.it show the output in table format having no problems.
But when the particular tr gets long output from database then the table break.
Now how can i fixed the tr width strictly?let say i want each td cannot be more than 100px.
How can i do it?
Note: Here table means html table,not the database table.
if ($query->num_rows() > 0)
{
$output = '';
foreach ($query->result() as $function_info)
{
if ($description)
{
$output .= ''.$function_info->songName.'';
$output .= ''.$function_info->albumName.'';
$output .= ''.$function_info->artistName.'';
$output .= ''.$function_info->Code1.'';
$output .= ''.$function_info->Code2.'';
$output .= ''.$function_info->Code3.'';
$output .= ''.$function_info->Code4.'';
$output .= ''.$function_info->Code5.'';
}
else
{
$output .= ''.$function_info->songName.'';
}
}
$output .= '';
return $output;
}
else
{
return 'Result not found.';
}
thanks
riad
You can either do it in the HTML...
<td style="width: 100px">
Or you can try using wordwrap.

Categories