Please help -- Checkboxes -- So close, yet so far...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

Please help -- Checkboxes -- So close, yet so far...

Post by jjfletch »

Newbie here....

I have a form that has a number of checkboxes. Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}
r of checkboxes. Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';n, the values of the checked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($arrayvariable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in databas are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "S
I have a form that has a number of checkboxes.  Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$item' value='1' $chk&gt; $item&amp
I have a form that has a number of checkboxes.  Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}
, 'Bread', 'Lettuce' , 'Cookies');

$query = "SELECT * FROM qtable WHERE id='$did'";
$result = @mysql_query ($query);

while($row=mysql_fetch_array($result)) {
$item=$row['items'];
$brd=$row['brand'];
$st=$row['store'];
}

foreach ($array as $item) {

if ( $item is in database) // THIS IS WHERE I'M STUCK
$chk = 'checked';
else
$chk = '';
echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}

w['brand'];
$st=$row['store'];
}

foreach ($array as $item) {

if ( $item is in database) // THIS IS WHERE I'M STUCK
$chk = 'checked';
else
$chk = '';
echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}

on, the values of the checked boxes are stored as one variable via "implode" and placed into my database.

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_arrhecked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetchckboxes.  Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$itert to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$item' value='1' $chk&a are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) m that has a number of checkboxes.  Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.  

In an effort to make the form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}
lect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$item' value='1' $chk&gt; $item&lt;br&gt;&quote;;
}abase.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}
l_fetch_array($result)) {
$item=$row['items'];
$brd=$row['brand'];
$st=$row['store'];
}

foreach ($array as $item) {

if ( $item is in database) // THIS IS WHERE I'M STUCK
$chk = 'checked';
else
$chk = '';
echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}

he form editable (that is, a user can select/deselect boxes as they wish) , I've been trying to place "checked" inside the input tag for the values that are actually in the database.

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row&#1111;'items']; 
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
 I've been trying to place "checked" inside the input tag for the values that are actually in the database.  

But, I can't figure out how to check to see if that particular item is in the database... I've pasted the code below and indicated where I'm having a problem.

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

   

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
 
   while($row=mysql_fetch_array($result)) {
   $item=$row['items']; 
   $brd=$row['brand'];
   $st=$row['store'];
   } 

foreach ($array as $item) {

    if ( $item is in database)  // THIS IS WHERE I'M STUCK
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$item' value='1' $chk> $item<br>";
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

for starters, you are resetting $item with each iteration of your while loop. Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
teration of your while loop. Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item&#1111;]=$row&#1111;'items']; //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';uld be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item&#1111;]=$row&#1111;'items']; //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
m to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item&#1111;]=$row&#1111;'items']; //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if d then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach are resetting $item with each iteration of your while loop.  Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item&#1111;]=$row&#1111;'items']; //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$items' value='1' $chk&gt; $items&aare resetting $item with each iteration of your while loop.  Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
111;'brand'];
$st=$row['store'];
}

foreach ($array as $items) {

if ( array_search($items,$item))
$chk = 'checked';
else
$chk = '';
echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
name='$items' value='1' $chk> $items<br>";
}
ould be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk =an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($resul of your while loop.  Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item&#1111;]=$row&#1111;'items']; //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$items' value='1' $chk&ry = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>&quot $item with each iteration of your while loop.  Proably would be best to create an array and then add a new item to the array on each iteration.

then you can use array_search to see if the value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
 FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
$chk = '';
echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
e value of your foreach loop is in the array..

ex:

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item[]=$row['items']; //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

Almost it think big B.

I think you meant to suggest a explode()

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode("whatEverYouUsedToSeperateTheData" ,$row['items']); //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
ggest a explode()

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode(&quote;whatEverYouUsedToSeperateTheData&quote; ,$row&#1111;'items']); //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode(&quote;whatEverYouUsedToSeperateTheData" ,$row['items']); //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode("whatEverYouUsedToSeperateTheData&quote; ,$row&#1111;'items']); //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode("whatEverYouUsedToSeperateTheData" ,$row['items']); //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "&ag B.

I think you meant to suggest a explode()

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = &quote;SELECT * FROM qtable WHERE id='$did'&quote;;
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode(&quote;whatEverYouUsedToSeperateTheData&quote; ,$row&#1111;'items']); //this is now an array
   $brd=$row&#1111;'brand'];
   $st=$row&#1111;'store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo &quote;&lt;input type='checkbox' name='$items' value='1' $chk&gt; $items&ag B.

I think you meant to suggest a explode()

Code: Select all

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');
 
   $query = "SELECT * FROM qtable WHERE id='$did'";
   $result = @mysql_query ($query);
   
   $item = array();
   while($row=mysql_fetch_array($result)) {
   $item =explode("whatEverYouUsedToSeperateTheData" ,$row['items']); //this is now an array
   $brd=$row['brand'];
   $st=$row['store'];
   } 
 
foreach ($array as $items) {
 
    if ( array_search($items,$item))  
         $chk = 'checked';
    else
         $chk = '';
    echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
d';
else
$chk = '';
echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}

$array = array('Milk', 'Eggs', 'Bread', 'Lettuce' , 'Cookies');

$query = "SELECT * FROM qtable WHERE id='$did'";
$result = @mysql_query ($query);

$item = array();
while($row=mysql_fetch_array($result)) {
$item =explode("whatEverYouUsedToSeperateTheData" ,$row['items']); //this is now an array
$brd=$row['brand'];
$st=$row['store'];
}

foreach ($array as $items) {

if ( array_search($items,$item))
$chk = 'checked';
else
$chk = '';
echo "<input type='checkbox' name='$items' value='1' $chk> $items<br>";
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

naw, I meant it the way I had it. Exploding it will try to make it create a new array for each iteration of the loop whereas the way I had it will only add a new element to the array. He doesn't have the whole string built to explode it which is why I did it the way I did. He's looping over query results and just adding to the array with each iteration of the loop...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

the reason I modified your code was because he stats
Upon initial submission, the values of the checked boxes are stored as one variable via "implode" and placed into my database.
therefore an explode(or some method) is needed to seperate the values back out.

Or maybe I'm missing something.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

he/she :P
Post Reply