Page 1 of 1

Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 4:52 pm
by gilly79
Hello Im really sorry i need help

Im trying to put a checkbox in a returned query from a database i.e echo row with a ceckbox so i can delete the row...

Its just not working, tried messing aroun with the parms and nothing works

below is the code could someone please help its driving me insaine!!

Code: Select all

 
<?php 
    // Start the session
    require_once('startsession.php');
 
    // Insert the page header
    $page_title = 'Inbox';
    require_once('header.php');
 
// ========== SCOTT ==============
 
 
//Include the connection details, open $connection and select database 
//include ("connection.php"); 
require_once('connectvars.php');
 
//vars
$user_id = $_SESSION['user_id'];
echo $user_id;
 
//Prepare query 
$query = "SELECT * FROM MESSAGES WHERE MessageTo='$user_id' ";
          
 
// execute query  
$result = mysqli_query($dbc,$query) or die ("Error in query: $query. ".mysql_error());  
 
// see if any rows were returned 
if (mysqli_num_rows($result)>0) { 
    echo "<table border=1>\n<tr>" . 
              "<th>MessageNo</th>" . 
              "<th>MessageFrom</th>" . 
            "<th>MessageTo</th>" . 
              "<th>Subject</th>" .
              "<th>Message</th>".  
            "<th>Reply</th>". 
            "<th>Delete</th>"; 
 
    while ($row = @ mysqli_fetch_array($result)) {
    //while($row = mysql_fetch_row($result)) { 
      echo "<tr>"; 
        echo "<td >".$row["MessageNo"]."</td>"; 
        echo "<td>".$row["MessageFrom"]."</td>";
        echo "<td>".$row["MessageTo"]."</td>";
        echo "<td>".$row["Subject"]."</td>";
        echo "<td>".$row["Message"]."</td>"; 
        echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$row['MessageNo']\" />";
        echo "<td class=BorderMeRed>".$row["MessageNo"]."</td>";
        echo "</tr>"; 
 
} 
    echo "</table>"; 
}  
else {  
    // no  
    // print status message  
    print "No rows found!";  
}  
 
?>
 
<?php  
  // Include the formatted error message 
  /* if (isset($_SESSION['message'])) 
    echo  
      "<h3><font color=red>".$_SESSION['message']."</font></h3>"; */
  ?>
<p>Click<a href="tester.php">Here</a>to send a message<p/>
<?php
    require_once('footer.php');
?>

Re: Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 5:15 pm
by dzelenika
your checkbox tag should be in <td> </td>, instead between two td tags

Re: Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 5:29 pm
by gilly79
like so??

Code: Select all

    echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"$row['MessageNo']\" /> </td>";
still an error

Re: Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 5:42 pm
by dzelenika
gilly79 wrote:like so??

echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"$row['MessageNo']\" /> </td>";

still an error
what error?

Re: Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 5:54 pm
by gilly79
im using text textmate and the messageno is being highlighted - there is a definate error as my page wont load.... im not sure if the td is an error to be honest as i know you can do checkboxes without them...

Re: Trying to add a checkbox to an echo string... argh help

Posted: Mon Feb 22, 2010 8:50 pm
by Benjamin
Use

Code: Select all

tags when posting code in the forums.

The solution to this issue is relatively simple.  Rather than tell you, I will direct you.

Please see: http://www.php.net/manual/en/language.t ... ng.complex

Re: Trying to add a checkbox to an echo string... argh help

Posted: Tue Feb 23, 2010 1:29 am
by realnsleo
Try this:

Code: Select all

 
if (mysqli_num_rows($result)>0)  { // Or $result->num_rows() > 0
    echo "<table border=1>".
                "<tr>" .
                        "<th>MessageNo</th>" .
                        "<th>MessageFrom</th>" .
                        "<th>MessageTo</th>" .
                        "<th>Subject</th>" .
                        "<th>Message</th>".  
                        "<th>Reply</th>".
                        "<th>Delete</th>".
                "</tr>"; // you forgot to add this ending tag
  
     while ($row = @ mysqli_fetch_array($result)) { // or $result->fetch_assoc()
         echo '<tr>'.
                       '<td>'. $row["MessageNo"]. '</td>';
                       '<td>'. $row["MessageFrom"]. '</td>';
                       '<td>'. $row["MessageTo"]. '</td>';
                       '<td>'. $row["Subject"]. '</td>';
                       '<td>'. $row["Message"]. '</td>';
                       '<td class="BorderMeRed">' .$row["MessageNo"]. '</td>';
                       '<td><input type="checkbox" name="delete[]" value="'. $row['MessageNo']. '" />';
                '</tr>';
  
}
     echo "</table>";
 }  
 else {  
     // no  
     // print status message  
     print "No rows found!";  
}