Hello everybody,
I want to write php script with classes, but I haven't enought knowledges. I want to delete multiple rows from mysql with chekbox, but I am wrong. I will be very happy if somebody responds me. This is my code

function showNews(&$sHtml)
        {    

           $sHtml.='<input type="submit" name="btnDel" value="delete"/>';  

           $aNews = array();

           // if ( isset($_POST['btnDel']) )
        //    {
               //  $sHtml.='admIndex.php?cmd=delNews&news_id='.$aNews[$i]['news_id'].';
        //    }



            $oDbQ = new DatabaseQuery();
            $sQuery = "SELECT * FROM news ORDER BY date DESC";
            $iDBres = $oDbQ->readQuery($sQuery);

            while($row = mysqli_fetch_assoc($iDBres))
            {
                $aNews[] = array( 'news_id' => $row['news_id']
                                    , 'news_title' => $row['news_title']
                                    , 'content' => $row['content']
                                    , 'date' => $row['date']

                                  );
            }
            mysqli_free_result($iDBres);
            $oDbQ->connClose();





            if ( count($aNews) > 0 )
            {

                $sHtml.='<div class="scrollbar">'; //
                $sHtml .= '<table width="50%">';
                $sHtml .= '<tr style="background-color: #c1c1c1">';
                $sHtml.='<th></th>';   
                $sHtml .= '<th>title</th>';  
                $sHtml .= '<th>news</th>';           
                $sHtml .= '<th>date</th>'; 



                for($i=0; $i<count($aNews); $i++)
                {
                     $class = '';  
                     if ( ($i%2) != 0 )
                     $class = ' style="background-color: #c1c1c1" ';
                     $sHtml .= '<tr'.$class.'>';
                     $sHtml.='<td><input name="chek" type="checkbox" value="<?php echo              

$aNews[$i][news_id];?>"></td>';   



                    // $sHtml .= '<td>'.$aNews[$i]['news_id'].'</td>';
                     $sHtml .= '<td>'.$aNews[$i]['news_title'].'</td>';     
                     $sHtml .= '<td>'.$aNews[$i]['content'].'</td>';
                     $sHtml .= '<td>'.$aNews[$i]['date'].'</td>';



                     $sHtml .= '</tr>';


                }

               $sHtml.='</table>';



            }

                 unset($aNews);

Hai,
You can achieve this through the following steps.This is the logic,just work it out.
1. Pass all the selected chekbox values(id or name or anything) to an hidden field
eg:

<input type="hidden" name="deleteList" value="1,2,3">

2. Now explode the variable "deleteList" and perform the delete operation for each of the values inside an for loop.

thank you :)

Another way, which I actually believe to be the PHP recommended method, is to explicitly name your checkbox fields like this instead:

<input name="chek[]" type="checkbox" value="x"/>

Notice the added brackets; these trigger the data into an array, and this is actually how you are supposed to walk through check-boxes in PHP. You don't and shouldn't ever need to use a hidden field or anything like that. Just make sure you declare the checkbox with [] and the end of the name and it will be an array of values.

Then in PHP you could just walk through the array like this:

foreach ($_POST as $c)
{

}

Another way, which I actually believe to be the PHP recommended method, is to explicitly name your checkbox fields like this instead:

<input name="chek[]" type="checkbox" value="x"/>

Notice the added brackets; these trigger the data into an array, and this is actually how you are supposed to walk through check-boxes in PHP. You don't and shouldn't ever need to use a hidden field or anything like that. Just make sure you declare the checkbox with [] and the end of the name and it will be an array of values.

Then in PHP you could just walk through the array like this:

foreach ($_POST as $c)
{

}

It's work! :)

Hi, I have another question: How to add a new line in this code
$sHtml.='<input type="text" name="title"/><br/><br/>';
$sHtml.='<textarea name="area" rows="5" cols="30">';
$sHtml.='</textarea><br/>';
When I open my php file in the browser and then select "View Page Source" all html code is located on one line. I would like code to be readble.
I try with \n, but dosen't work

The problem is you are quoting your sHtml data within single quotes.. Anything placed within single quotes in PHP is interpretted as literal, so you need to change it to double quotes, which means either escaping your quotes around HTML parameter names or using single quotes. Here is an example:

$sHtml.="<input type=\"text\" name=\"title\"/><br/><br/>\n";

The problem is you are quoting your sHtml data within single quotes.. Anything placed within single quotes in PHP is interpretted as literal, so you need to change it to double quotes, which means either escaping your quotes around HTML parameter names or using single quotes. Here is an example:

$sHtml.="<input type=\"text\" name=\"title\"/><br/><br/>\n";

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.