Page 1 of 1

while query problem

Posted: Mon Jan 12, 2004 5:48 pm
by hyper_st8
Hello!
When I try the following script, only the value in the last field gets ticked:

Code: Select all

<?
$nameID = "2";

$db = mysql_connect("localhost", "", "") or die("unable to connect to the database");
	 mysql_select_db("test", $db) or die("unable to select the database");

$sqlA = "SELECT * FROM Anames";
$objSQLA = mysql_query ($sqlA) or die ("Query 1 Failed: ".mysql_error());

$sqlB= "SELECT Enames FROM Bnames WHERE namesID ='$nameID'";
$objSQLB = mysql_query ($sqlB) or die ("Query 2 Failed: ".mysql_error());
$rowB = mysql_fetch_assoc($objSQLB);

while ($names = mysql_fetch_array($objSQLA)) {
	echo $names["names"];
	echo "<input type="checkbox" name="name"";
if($names["names"] == $rowB["Enames"]) {
	echo "checked";
}
echo ">";
}

?>
any ideas?! Thank you in advance! :D

Posted: Mon Jan 12, 2004 6:25 pm
by Straterra
I think A doesn't work because you are selecting every cell..I thought you could only retrieve one row, then use the fetch array function to tell what collumn in that row you wanted...

Posted: Tue Jan 13, 2004 3:05 am
by hyper_st8
The idea is to have a list of all names and the ones that are already associated with that ID is checked... Can't understand why it doesn't work because it went from working to just selecting the last value in the field...

Posted: Tue Jan 13, 2004 8:53 am
by Bill H
You have a bunch of checkboxes all with the same name.
If you do that you need to make them an array:

Code: Select all

<?php
echo "<input type="checkbox" name="name[]""; 

?>

Posted: Tue Jan 13, 2004 10:43 am
by infolock
Try this and see if it works... It's untested as i'm at school right now, but i think it might.

Code: Select all

<? 
$nameID = "2"; 

//******** ADDED ARRAYS *********
$Bnames = array();
$ANames = array();
$chkboxname = array();
//###############################

mysql_connect("localhost", "", "") or die("unable to connect to the database"); 
mysql_select_db("test", $db) or die("unable to select the database"); 

$sqlA = "SELECT * FROM Anames"; 
$objSQLA = mysql_query ($sqlA) or die ("Query 1 Failed: ".mysql_error()); 
while ($names = mysql_fetch_array($objSQLA)) 
{ 
	$Anames[] = $names['name'];
} 


$sqlB= "SELECT Enames FROM Bnames WHERE namesID ='".$nameID."'"; 
$objSQLB = mysql_query ($sqlB) or die ("Query 2 Failed: ".mysql_error()); 

while ($rowB = mysql_fetch_assoc($objSQLB))
{
	$Bnames[] = $rowB['Enames'];  //you had $rowB['name'] before.. Shouldn't this be Ename (since that's what you used in your query)??
}


for($i=0; $i<count($Anames); $i++)
{
                $chkboxname[$i] = 'name'.$i;
	echo $Anames[$i]; 
	echo '<input type="checkbox" name="'.$chkboxname[$i].'"'; 
	if($Anames[$i] == $Bnames[$i]) 
	{ 
   	   echo 'checked'; 
	} 
	echo ">"; // wtf is this for? 
}


?>

Posted: Wed Jan 14, 2004 4:09 am
by hyper_st8
Yeah good point Bill H! I needed to make it into an array.
Thanks infolock! That works great! as for that the last echo, it was used to close the check input, which is nice!