Page 1 of 1

[SOLVED] Passing multiple values via checkbox woes

Posted: Tue Jul 06, 2004 2:33 pm
by decoy1
Hello,

I am having a small problem I thought someone could comment on. I simply have a function which pulls a number of links from the database and displays them with a checkbox next to them. The ones that get checked will eventually get deleted...

Code: Select all

function display_edit_cat()
{
  $sql = "SELECT * FROM link_cat order by catid asc"; 
  $result = mysql_query($sql); 
  if(!$result)
  {
    echo("ERROR: " .mysql_error(). "\n$sql\n");
    exit();
  }
  else
  {
    $ecf = "<p><center><table border=0 cellpadding=4>\n"; 
          
    if(@mysql_num_rows($result) == 0)
    {
      genMsg('no_edit', 'Categories');
    }
    else
    {
      $row = mysql_fetch_array($result);
      if($row)
      {
        $ecf .= '<form type=''get'' action=''admin_process.php''><input type=''hidden'' name=''action'' value=''delete_link_cat''>';
        do
        {
          $linkname = $row["cat_name"]; 
          $catid = $row["catid"];          
          $checkbox = '<input type="checkbox" name="catid[]" value="'. $catid .'">&nbsp;&nbsp;'. $linkname;
          $ecf .= $checkbox. '<br>';
        }
        while($row = mysql_fetch_array($result));
      }
      else
      {
        $ecf .= genMsg('general', 'Currently there are no categories.');
      }
/*    
      while($myrow = mysql_fetch_array($result))
      { 
        $linkname = $myrow["cat_name"]; 
        $catid = $myrow["catid"]; 
        $ecf .=
        "<tr><td class=medium>$linkname</td>\n<td><A HREF="admin_forms.php?catid=$catid">Edit</a></td>
        <td><A HREF="admin_process.php?catid=$catid&action=delete_link_cat">Delete</a></td>
        </tr>"; 
      } 
      //$ecf .= "</table></td></tr></table></td><td align=center valign=top>"; 
*/      
      $ecf .= "<input type="submit" name="submit" value="Submit"><input type="reset" value="Reset"></form></table></td></tr></table></center>&nbsp;&nbsp;&nbsp;&nbsp;";               
    }
  }
  return $ecf;
}
You can see that the variable '$checkbox' is the relevant one. Now, I am attempting to pass the 'catid' of all the links that are checked by the user to another function which basically confirms the links checked and gives the user another chance to not delete them.

The problem is that I'm not sure how to display all the link names together. In other words when the 'delete_confirmation' function is called, I want to say...'are you sure you want to delete the following links?' and then have all the ones that were clicked listed below.

Here is a stripped down version of the confirmation function...

Code: Select all

function admin_delete_confirm($id, $type) <--$id is catid passed from other function
{
  if($type == 'link_cat')
  { 
    foreach($id as $catid)
    {  
      $sql = "SELECT cat_name FROM link_cat WHERE catid=" .$catid;  
    }      
    $result = mysql_query($sql);  
    if(!$result)
    {
      $addel = genMsg('sql', '');  
    }
    else
    {
      //while($row = mysql_fetch_array($result))
      //{
        //$catname = $row["cat_name"];      
        //echo $catname. '<br>';
      //}       
      //$addel .= 'Are you sure you want to delete the category  - ' .$catname. '?<p>';
      //$addel .= '<a href=admin_forms.php?action=admin_delete&var=link_cat&id=' .$id. '>Yes, delete</A>';        
    }
  }
  return $addel;
}
I can not get it to list all the links, just the last one checked. I don't understand why

Code: Select all

while($row = mysql_fetch_array($result))
{
  $catname = $row["cat_name"];      
  echo $catname. '<br>';
}
doesn't work. I have to be missing something very easy, but I need a nudge in the right direction.

Thanks for any help :)

Posted: Tue Jul 06, 2004 5:04 pm
by scorphus
It is quite simple. First let me show you what is wrong in your code:

Code: Select all

foreach($id as $catid)
    {
        $sql = "SELECT cat_name FROM link_cat WHERE catid=" .$catid;
    }
    $result = mysql_query($sql);
Here you are querying the db only once, with the last $sql defined in the last foreach loop, that's why only the last row gets showed.

I think you can now fix it.

Stand by while I think/manage one solution...

-- Scorphus.

Posted: Tue Jul 06, 2004 5:12 pm
by scorphus
Well, I suggest you to remove that foreach and mount the query string using the [php_man]implode[/php_man]() function. Something like this:

Code: Select all

$values = implode(', ' $id);
$sql = "SELECT cat_name FROM link_cat WHERE catid IN ($values)";
Then query the DB normaly and fetch rows with a while loop.

I hope that helps. I'm looking forward hearing your feedback.

-- Scorphus

Posted: Tue Jul 06, 2004 6:14 pm
by decoy1
Hi scorphus,

That worked great. I understand why it is now working, but I'm a little confused as to why almost identical code will work to display all the links in the first place...

Code: Select all

function display_cat()
{
  $sql = "SELECT * FROM link_cat order by catid asc"; 
  $result = mysql_query($sql);
  while($row = mysql_fetch_array($result))
  {
    $linkname = $row["cat_name"]; 
    $catid = $row["catid"];          
    $checkbox = '<input type="checkbox" name="catid[]" value="'. $catid .'">&nbsp;&nbsp;'. $linkname;
    $box = $checkbox. '<br>';
  }
  return $box;
}
Thanks for the help, much appreciated :)

Posted: Tue Jul 06, 2004 6:46 pm
by scorphus
That's because the query tell db to select all fileds from the table (SELECT * FROM link_cat) without filtering them (there is no WHERE clause in the query). Then all the table rows will be returned. As far as if you filter it by adding an WHERE clause, only the filtered rows will be returned.

-- Scorphus

Posted: Tue Jul 06, 2004 7:03 pm
by decoy1
Cool... I got it now. Just needed a little slap in the back of the head..hehe. Thanks again.